forked from LazzaAU/skill_Broadcast
-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Broadcast.py
370 lines (298 loc) · 12.3 KB
/
Broadcast.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
from pathlib import Path
from typing import Optional
from core.base.model.AliceSkill import AliceSkill
from core.device.model.DeviceAbility import DeviceAbility
from core.dialog.model.DialogSession import DialogSession
from core.util.Decorators import IntentHandler
from core.device.model.Device import Device
class Broadcast(AliceSkill):
"""
Author: Lazza
Description: Broadcast voice or normal messages to active satellites
"""
def __init__(self):
super().__init__()
self._preChecksDone: bool = False
self._deviceQuantity: int = 0
self._broadcastMessage: str = ''
self._playbackDevice: Optional[Device] = None
self._selectedSat: Optional[Device] = None
self._listOfAllDevices = list()
self._sendingDevice: Optional[Device] = None
self._userSpeech = self.AudioServer.LAST_USER_SPEECH
self._answerReplayNow: bool = False
self._waveFile = Path(f'{self.getResource("sounds")}/delayedSound.wav')
self._previousReplyDevice: Optional[Device] = None
# NOTE: _selectedSat is same as playbackdevice but is used to allow reference to who i'm having a conversation with
######################## INTENT HANDLERS ############################
@IntentHandler('AddBroadcast')
def addNewBroadcast(self, session: DialogSession, **_kwargs):
# If useVoiceRecording is enabled then turn on Alice's voice recording feature
if self.getConfig('useVoiceRecording') and not self.getAliceConfig('recordAudioAfterWakeword'):
self.updateAliceConfig(key='recordAudioAfterWakeword', value=True)
self.logWarning(f'BroadCast skill has just enabled Alice\'s built in Record Audio After Wakeword feature')
# This resets values in case of previous broadcast and user initiates another broadcast from a different device
if self._broadcastMessage:
self.resetValues()
# Do prelimanary checks IE: Set the message and or the satellite room
self.doStatusCheck(session)
# used for replying to last known device that sent a broadcast
@IntentHandler('BroadcastReply')
def reply2LastBroadcast(self, session: DialogSession):
if not self._previousReplyDevice:
self.endDialog(
sessionId=session.sessionId,
text=self.randomTalk('previousMessageError'),
deviceUid=session.deviceUid
)
else:
if self._previousReplyDevice.uid == session.deviceUid:
self.endDialog(
sessionId=session.sessionId,
text=self.randomTalk(text='replySelf'),
deviceUid=session.deviceUid
)
return
self._selectedSat = self._playbackDevice = self._previousReplyDevice
self._sendingDevice = self.DeviceManager.getDevice(uid=session.deviceUid)
self.continueDialog(
sessionId=session.sessionId,
text=self.randomTalk(text='message4previous', replace=[self._previousReplyDevice]),
intentFilter=['UserRandomAnswer'],
currentDialogState='send2previous',
probabilityThreshold=0.1
)
# If user has choosen a room to play on in a multi satellite scenario then do this
@IntentHandler(intent='BroadcastRoom', requiredState='askingWhatRoomToPlayOn')
def userChoosingRoom(self, session: DialogSession):
self.chooseLocation(session)
self.doStatusCheck(session)
# Ask user for yes or no responce to playback message now or later (only for users with no available satellites)
@IntentHandler(intent='AnswerYesOrNo', requiredState='askingIfPlaybackShouldBeNow')
def yesOrNoReply(self, session: DialogSession):
if self.Commons.isYes(session):
self._answerReplayNow = True
self.requestMessage(session)
else:
self._answerReplayNow = False
if self._waveFile.exists():
self.endDialog(
sessionId=session.sessionId,
text=self.randomTalk('delayError'),
deviceUid=session.deviceUid
)
else:
self.continueDialog(
sessionId=session.sessionId,
text=self.randomTalk('playbackTime'),
intentFilter=['BroadcastTime'],
currentDialogState='UserWantsToDelayBroadcast',
probabilityThreshold=0.1
)
# If the user has requested to delay the responce (main unit systems only) then do this
@IntentHandler(intent='BroadcastTime', requiredState='UserWantsToDelayBroadcast')
def delayingBroadcast(self, session: DialogSession):
# If a duration was specified set a timer
if 'Duration' in session.slots:
self._playbackDevice = self.DeviceManager.getDevice(uid=session.deviceUid)
self.continueDialog(
sessionId=session.sessionId,
text=self.randomTalk('addAMessage'),
intentFilter=['UserRandomAnswer'],
currentDialogState='requestingBroadcastMessage',
probabilityThreshold=0.1
)
else: # if no duration specified ask user again to repeat
self.continueDialog(
sessionId=session.sessionId,
text=self.randomTalk('noConfirmationHeard'),
intentFilter=['BroadcastTime'],
currentDialogState='UserWantsToDelayBroadcast',
probabilityThreshold=0.1
)
# Add the broadcast message now that choosing location has been satisfied, and playback to the device
@IntentHandler(intent='UserRandomAnswer', requiredState='requestingBroadcastMessage')
def ProcessFirstInputMessage(self, session: DialogSession):
self._broadcastMessage = session.payload['input']
# SONAR IGNORE
# self.logInfo(self._broadcastMessage)
# self.logInfo(self._deviceQuantity)
if self._deviceQuantity == 1:
delayedRecording = Path(self._userSpeech.format(session.user, session.deviceUid))
# Copy lastUserSpeech.wav to the sounds folder
if delayedRecording:
self.Commons.runSystemCommand(['cp', str(delayedRecording), str(self._waveFile)])
# if user wants to playback now (no satellite senario)
if self._answerReplayNow:
self.playBroadcastMessage(session)
else:
delayedInterval: float = self.Commons.getDuration(session)
self.ThreadManager.doLater(
interval=delayedInterval,
func=self.delayedSoundPlaying
)
self.endDialog(
sessionId=session.sessionId,
text=self.randomTalk('durationConfirmation')
)
# if user has at least one active satellite then do this
elif self._deviceQuantity >= 2 and self._broadcastMessage and self._playbackDevice:
self.playBroadcastMessage(session)
# Below runs when a reply to the initial broadcast is recieved
@IntentHandler(intent='UserRandomAnswer', requiredState='UserIsReplying')
def InputReply(self, session: DialogSession):
self._playbackDevice = self._sendingDevice
self._sendingDevice = self.DeviceManager.getDevice(uid=session.deviceUid)
self._broadcastMessage = session.payload['input']
self.playBroadcastMessage(session)
# Below runs when a reply has been asked by the user
@IntentHandler(intent='UserRandomAnswer', requiredState='send2previous')
def ReplyToLastBroadcastDevice(self, session: DialogSession):
self._broadcastMessage = session.payload['input']
self.playBroadcastMessage(session)
######### THE CONFIGURATION GROUP ##############
# This method is used when a user chooses a location
def chooseLocation(self, session: DialogSession):
# if user has specified the location in the initial intent do this
if 'Location' in session.slotsAsObjects:
location = self.LocationManager.getLocationByName(session.slotValue('Location'))
if location:
self._selectedSat = self.DeviceManager.getDevicesByLocation(locationId=location.id, abilities=[DeviceAbility.PLAY_SOUND])
return
if not self._playbackDevice:
self.continueDialog(
sessionId=session.sessionId,
text=self.randomTalk('chooseAnotherLocation'),
intentFilter=['BroadcastRoom'],
currentDialogState='askingWhatRoomToPlayOn',
slot='Location',
probabilityThreshold=0.1
)
elif 'GetBase' in session.slots:
self._selectedSat = self._playbackDevice = self.DeviceManager.getMainDevice()
else:
self.continueDialog(
sessionId=session.sessionId,
text=self.randomTalk('chooseARoom'),
intentFilter=['BroadcastRoom'],
currentDialogState='askingWhatRoomToPlayOn',
slot='Location',
probabilityThreshold=0.1
)
def setTheActiveDevices(self, session: DialogSession):
# incomming request was from:
self._sendingDevice = self.DeviceManager.getDevice(uid=session.deviceUid)
# if we are at the only device, we send it to our selves
if self._deviceQuantity == 1:
self._selectedSat = self._playbackDevice = self._sendingDevice
# there are exactly two devices, get the other one
elif self._deviceQuantity == 2:
self._selectedSat = self._playbackDevice = [device for device in self._listOfAllDevices if not device == self._sendingDevice][0]
# there are more devices available - choose by 'Location' slot
else:
self.chooseLocation(session)
# Do pre broadcasting checks
def doStatusCheck(self, session: DialogSession):
if not self._preChecksDone:
self.getAvailableDevices() # make a list of available devices and get quantity
if not self._selectedSat:
self.setTheActiveDevices(session)
if self._selectedSat and 'UserRandomAnswer' not in session.slots and self._deviceQuantity >= 2:
self.requestMessage(session)
elif self._selectedSat and 'UserRandomAnswer' not in session.slots and self._deviceQuantity == 1:
self.continueDialog(
sessionId=session.sessionId,
text=self.randomTalk('playbackRequest'),
intentFilter=['AnswerYesOrNo'],
currentDialogState='askingIfPlaybackShouldBeNow'
)
# method for listing all available (active) satellites
def getAvailableDevices(self):
# "offline sats" allows users with alpha branches and/or no heartbeat to play to sats
self._listOfAllDevices = self.DeviceManager.getDevicesWithAbilities(abilities=[DeviceAbility.PLAY_SOUND], connectedOnly=self.getConfig('onlineSatsOnly'))
self._preChecksDone = True
if self._listOfAllDevices:
self._deviceQuantity = len(self._listOfAllDevices)
else:
self.logDebug(f'Seems you have no available devices at the moment')
self._deviceQuantity = 0
def delayReplyRequest(self):
self.ask(
text=self.randomTalk('replyRequest'),
deviceUid=self._playbackDevice.uid,
intentFilter=['UserRandomAnswer'],
currentDialogState='UserIsReplying',
canBeEnqueued=False,
probabilityThreshold=0.2
)
# request adding a message
def requestMessage(self, session: DialogSession):
self.continueDialog(
sessionId=session.sessionId,
text=self.randomTalk('addAMessage'),
intentFilter=['UserRandomAnswer'],
currentDialogState='requestingBroadcastMessage',
probabilityThreshold=0.1
)
# Play the broadcast
def playBroadcastMessage(self, session: DialogSession):
# self.logInfo(self._sendingDevice)
self._previousReplyDevice = self._sendingDevice
if not self._playbackDevice:
self.logWarning('Was asked to play broadcast message but no playback device')
return
self.playBroadcastSound()
# if user has selected to play voice message broadcasts then do this
if self.getConfig('useVoiceRecording'):
lastRecording = Path(self._userSpeech.format(session.user, session.deviceUid))
self.playSound(lastRecording.stem, location=lastRecording.parent, deviceUid=self._playbackDevice.uid)
self.endSession(sessionId=session.sessionId)
if self._deviceQuantity == 1:
return
# If user also has choosen to allow replies then do this
if self.getConfig('allowReplies') and self._deviceQuantity >= 2:
self.ThreadManager.doLater(
interval=5.0,
func=self.delayReplyRequest
)
elif not self.getConfig('useVoiceRecording'):
if self.getConfig('allowReplies') and self._deviceQuantity >= 2:
self.endDialog(
sessionId=session.sessionId,
deviceUid=self._playbackDevice.uid,
text=self._broadcastMessage
)
# add this delay when asking for a reply, for smoother transition
self.ThreadManager.doLater(
interval=5.0,
func=self.delayReplyRequest
)
# If no options enabled just end the dialog and play the message
else:
self.endDialog(
sessionId=session.sessionId,
text=self._broadcastMessage,
deviceUid=self._playbackDevice.uid
)
def playBroadcastSound(self):
self.playSound(
soundFilename='broadcastNotification',
location=self.getResource('sounds'),
sessionId='BroadcastAlert',
deviceUid=self._playbackDevice.uid
)
def resetValues(self):
self._broadcastMessage: str = ''
self._selectedSat = None
self._playbackDevice = None
self._sendingDevice = None
self._answerReplayNow: bool = False
# the delayed sound file to play
def delayedSoundPlaying(self):
self.playSound(
soundFilename='delayedSound',
location=self.getResource('sounds'),
sessionId='DelayedBroadcastAlert',
deviceUid=self._playbackDevice.uid
)
self.Commons.runSystemCommand(['rm', str(self._waveFile)])