def onMessage(self, intent: str, session: DialogSession) -> bool: sessionId = session.sessionId slots = session.slots if not self._minigame or not self._minigame.started: if intent == self._INTENT_PLAY_GAME or ( intent == self._INTENT_ANSWER_MINI_GAME and session.previousIntent == self._INTENT_PLAY_GAME): if 'WhichGame' not in slots.keys(): self.continueDialog( sessionId=sessionId, intentFilter=[self._INTENT_ANSWER_MINI_GAME], text=self.TalkManager.randomTalk('whichGame'), previousIntent=self._INTENT_PLAY_GAME) elif session.slotValue( 'WhichGame') not in self._SUPPORTED_GAMES: self.continueDialog( sessionId=sessionId, intentFilter=[ self._INTENT_ANSWER_MINI_GAME, self._INTENT_ANSWER_YES_OR_NO ], text=self.TalkManager.randomTalk('unknownGame'), previousIntent=self._INTENT_PLAY_GAME) else: game = session.slotValue('WhichGame') self._minigame = self._minigames[game] self._minigame.start(session) elif intent == self._INTENT_ANSWER_YES_OR_NO: if not commons.isYes(session): self.endDialog(sessionId=sessionId, text=self.randomTalk('endPlaying')) else: self.continueDialog( sessionId=sessionId, intentFilter=[self._INTENT_ANSWER_MINI_GAME], text=self.TalkManager.randomTalk('whichGame'), previousIntent=self._INTENT_PLAY_GAME) elif self._minigame is not None: if intent == self._INTENT_ANSWER_YES_OR_NO and session.customData and 'askRetry' in session.customData.keys( ): if commons.isYes(session): self._minigame.start(session) else: self._minigame = None self.endDialog(sessionId=sessionId, text=self.randomTalk('endPlaying')) else: self._minigame.onMessage(intent, session) return True
def onMessage(self, intent: str, session: DialogSession) -> bool: """handle all incoming messages""" if intent == self._INTENT_ADD_ITEM or (intent in {self._INTENT_ANSWER_SHOP, self._INTENT_SPELL_WORD} and session.previousIntent == self._INTENT_ADD_ITEM): #Add item to list self.editList(session, intent, 'add', self._addItemInt) return True elif intent == self._INTENT_DEL_ITEM or (intent in {self._INTENT_ANSWER_SHOP, self._INTENT_SPELL_WORD} and session.previousIntent == self._INTENT_DEL_ITEM): #Delete items from list self.editList(session, intent, 'rem', self._deleteItemInt) return True elif intent == self._INTENT_READ_LIST: self.readList(session) return True elif intent == self._INTENT_CHECK_LIST or (intent in {self._INTENT_ANSWER_SHOP, self._INTENT_SPELL_WORD} and session.previousIntent == self._INTENT_CHECK_LIST): #check if item is in list self.editList(session, intent, 'chk', self._checkListInt) return True elif intent == self._INTENT_DEL_LIST: self.continueDialog( sessionId=session.sessionId, text=self.randomTalk('chk_del_all'), intentFilter=[self._INTENT_CONF_DEL], previousIntent=self._INTENT_DEL_LIST) return True elif session.previousIntent == self._INTENT_DEL_LIST and intent == self._INTENT_CONF_DEL: if commons.isYes(session): self.endDialog(session.sessionId, text=self._deleteCompleteList()) else: self.endDialog(session.sessionId, text=self.randomTalk('nodel_all')) return True return False
def onMessage(self, intent: str, session: DialogSession) -> bool: if intent == self._INTENT_GLOBAL_STOP: self.endDialog(sessionId=session.sessionId, text=self.randomTalk('confirmGlobalStop'), siteId=session.siteId) return True if not self.filterIntent(intent, session): return False siteId = session.siteId slots = session.slots slotsObj = session.slotsAsObjects sessionId = session.sessionId customData = session.customData payload = session.payload if self._INTENT_ADD_DEVICE in {intent, session.previousIntent}: if self.DeviceManager.isBusy(): self.endDialog(sessionId=sessionId, text=self.randomTalk('busy'), siteId=siteId) return True if 'Hardware' not in slots: self.continueDialog(sessionId=sessionId, text=self.randomTalk('whatHardware'), intentFilter=[ self._INTENT_ANSWER_HARDWARE_TYPE, self._INTENT_ANSWER_ESP_TYPE ], previousIntent=self._INTENT_ADD_DEVICE) return True elif slotsObj['Hardware'][0].value[ 'value'] == 'esp' and 'EspType' not in slots: self.continueDialog(sessionId=sessionId, text=self.randomTalk('whatESP'), intentFilter=[ self._INTENT_ANSWER_HARDWARE_TYPE, self._INTENT_ANSWER_ESP_TYPE ], previousIntent=self._INTENT_ADD_DEVICE) return True elif 'Room' not in slots: self.continueDialog(sessionId=sessionId, text=self.randomTalk('whichRoom'), intentFilter=[self._INTENT_ANSWER_ROOM], previousIntent=self._INTENT_ADD_DEVICE) return True hardware = slotsObj['Hardware'][0].value['value'] if hardware == 'esp': if not self.ModuleManager.isModuleActive('Tasmota'): self.endDialog( sessionId=sessionId, text=self.randomTalk('requireTasmotaModule')) return True if self.DeviceManager.isBusy(): self.endDialog(sessionId=sessionId, text=self.randomTalk('busy')) return True if not self.DeviceManager.startTasmotaFlashingProcess( commons.cleanRoomNameToSiteId(slots['Room']), slotsObj['EspType'][0].value['value'], session): self.endDialog(sessionId=sessionId, text=self.randomTalk('espFailed')) elif hardware == 'satellite': if self.DeviceManager.startBroadcastingForNewDevice( commons.cleanRoomNameToSiteId(slots['Room']), siteId): self.endDialog( sessionId=sessionId, text=self.randomTalk('confirmDeviceAddingMode')) else: self.endDialog(sessionId=sessionId, text=self.randomTalk('busy')) else: self.continueDialog( sessionId=sessionId, text=self.randomTalk('unknownHardware'), intentFilter=[self._INTENT_ANSWER_HARDWARE_TYPE], previousIntent=self._INTENT_ADD_DEVICE) return True elif intent == self._INTENT_MODULE_GREETING: if 'uid' not in payload or 'siteId' not in payload: self._logger.warning( 'A device tried to connect but is missing informations in the payload, refused' ) self.publish(topic='projectalice/devices/connectionRefused', payload={'siteId': payload['siteId']}) return True device = self.DeviceManager.deviceConnecting(uid=payload['uid']) if device: self._logger.info( 'Device with uid {} of type {} in room {} connected'. format(device.uid, device.deviceType, device.room)) self.publish(topic='projectalice/devices/connectionAccepted', payload={ 'siteId': payload['siteId'], 'uid': payload['uid'] }) else: self.publish(topic='projectalice/devices/connectionRefused', payload={ 'siteId': payload['siteId'], 'uid': payload['uid'] }) return True elif intent == self._INTENT_ANSWER_YES_OR_NO: if session.previousIntent == self._INTENT_REBOOT: if 'step' in customData: if customData['step'] == 1: if commons.isYes(session): self.continueDialog( sessionId=sessionId, text=self.randomTalk('askRebootModules'), intentFilter=[self._INTENT_ANSWER_YES_OR_NO], previousIntent=self._INTENT_REBOOT, customData={ 'module': self.name, 'step': 2 }) else: self.endDialog(sessionId, self.randomTalk('abortReboot')) else: value = 'greet' if commons.isYes(session): value = 'greetAndRebootModules' self.ConfigManager.updateAliceConfiguration( 'onReboot', value) self.endDialog(sessionId, self.randomTalk('confirmRebooting')) self.ThreadManager.doLater(interval=5, func=self.restart) else: self.endDialog(sessionId) self._logger.warn( '[{}] Asked to reboot, but missing params'.format( self.name)) elif session.previousIntent == self._INTENT_DUMMY_ADD_USER: if commons.isYes(session): self.UserManager.addNewUser(customData['name'], AccessLevel.ADMIN.name.lower()) self.continueDialog( sessionId=sessionId, text=self.randomTalk('addUserWakeword', replace=[customData['name']]), intentFilter=[self._INTENT_ANSWER_YES_OR_NO], previousIntent=self._INTENT_DUMMY_ADD_WAKEWORD) else: self.continueDialog( sessionId=sessionId, text=self.randomTalk('soWhatsTheName'), intentFilter=[ self._INTENT_ANSWER_NAME, self._INTENT_SPELL_WORD ], previousIntent=self._INTENT_DUMMY_ADD_USER) elif session.previousIntent == self._INTENT_DUMMY_ADD_WAKEWORD: if commons.isYes(session): self.WakewordManager.newWakeword( username=customData['name']) self.ThreadManager.newLock('AddingWakeword').set() self.continueDialog( sessionId=sessionId, text=self.randomTalk('addWakewordAccepted'), intentFilter=[self._INTENT_WAKEWORD], previousIntent=self._INTENT_DUMMY_WAKEWORD_INSTRUCTION) else: if self.delayed: self.delayed = False self.ThreadManager.doLater(interval=2, func=self.onStart) self.endDialog(sessionId=sessionId, text=self.randomTalk('addWakewordDenied')) elif session.previousIntent == self._INTENT_ADD_USER: if commons.isYes(session): self.UserManager.addNewUser(customData['username'], slots['UserAccessLevel']) self.continueDialog( sessionId=sessionId, text=self.randomTalk( 'addUserWakeword', replace=[slots['Name'], slots['UserAccessLevel']]), intentFilter=[self._INTENT_ANSWER_YES_OR_NO], previousIntent=self._INTENT_DUMMY_ADD_USER_WAKEWORD) else: self.continueDialog(sessionId=sessionId, text=self.randomTalk('soWhatsTheName'), intentFilter=[ self._INTENT_ANSWER_NAME, self._INTENT_SPELL_WORD ], previousIntent=self._INTENT_ADD_USER) elif session.previousIntent == self._INTENT_DUMMY_ADD_USER_WAKEWORD: if commons.isYes(session): # TODO return True else: self.endSession(sessionId=sessionId) elif session.previousIntent == self._INTENT_DUMMY_WAKEWORD_FAILED: if commons.isYes(session): self.WakewordManager.tryCaptureFix() self.continueDialog( sessionId=sessionId, text=self.randomTalk('addWakewordAccepted'), intentFilter=[self._INTENT_WAKEWORD], previousIntent=self._INTENT_DUMMY_WAKEWORD_INSTRUCTION) else: if self.delayed: self.delayed = False self.ThreadManager.getLock('AddingWakeword').clear() self.ThreadManager.doLater(interval=2, func=self.onStart) self.endDialog( sessionId=sessionId, text=self.randomTalk('cancellingWakewordCapture')) else: return False elif intent == self._INTENT_WAKEWORD and session.previousIntent == self._INTENT_DUMMY_WAKEWORD_INSTRUCTION: i = 0 # Failsafe... while self.WakewordManager.state != WakewordManagerState.CONFIRMING: i += 1 if i > 15: self.continueDialog( sessionId=sessionId, text=self.randomTalk('wakewordCaptureTooNoisy'), intentFilter=[self._INTENT_ANSWER_YES_OR_NO], previousIntent=self._INTENT_DUMMY_WAKEWORD_FAILED) return True time.sleep(0.5) filepath = Path( tempfile.gettempdir(), str(self.WakewordManager.getLastSampleNumber())).with_suffix( '.wav') if not filepath.exists(): self.continueDialog( sessionId=sessionId, text=self.randomTalk('wakewordCaptureFailed'), intentFilter=[self._INTENT_ANSWER_YES_OR_NO], previousIntent=self._INTENT_DUMMY_WAKEWORD_FAILED) else: self.playSound(soundFile=str(filepath), sessionId='checking-wakeword', siteId=session.siteId, absolutePath=True) text = 'howWasTheCapture' if self.WakewordManager.getLastSampleNumber( ) == 1 else 'howWasThisCapture' self.continueDialog( sessionId=sessionId, text=self.randomTalk(text), intentFilter=[self._INTENT_ANSWER_WAKEWORD_CUTTING], slot='WakewordCaptureResult', previousIntent=self._INTENT_WAKEWORD) elif intent == self._INTENT_ANSWER_WAKEWORD_CUTTING: if session.slotValue('WakewordCaptureResult') == 'more': self.WakewordManager.trimMore() elif session.slotValue('WakewordCaptureResult') == 'less': self.WakewordManager.trimLess() elif session.slotValue('WakewordCaptureResult') == 'restart': self.WakewordManager.state = WakewordManagerState.IDLE self.WakewordManager.removeSample() self.continueDialog( sessionId=sessionId, text=self.randomTalk( 'restartSample', replace=[ 3 - self.WakewordManager.getLastSampleNumber() ]), intentFilter=[self._INTENT_WAKEWORD], previousIntent=self._INTENT_DUMMY_WAKEWORD_INSTRUCTION) elif session.slotValue('WakewordCaptureResult') == 'ok': if self.WakewordManager.getLastSampleNumber() < 3: self.WakewordManager.state = WakewordManagerState.IDLE self.continueDialog( sessionId=sessionId, text=self.randomTalk( 'sampleOk', replace=[ 3 - self.WakewordManager.getLastSampleNumber() ]), intentFilter=[self._INTENT_WAKEWORD], previousIntent=self._INTENT_DUMMY_WAKEWORD_INSTRUCTION) else: self.ThreadManager.getLock('AddingWakeword').clear() if self.delayed: self.delayed = False self.ThreadManager.doLater(interval=2, func=self.onStart) self.WakewordManager.finalizeWakeword() self.endDialog(sessionId=sessionId, text=self.randomTalk('wakewordCaptureDone')) return True i = 0 # Failsafe while self.WakewordManager.state != WakewordManagerState.CONFIRMING: i += 1 if i > 15: self.continueDialog( sessionId=sessionId, text=self.randomTalk('wakewordCaptureTooNoisy'), intentFilter=[self._INTENT_ANSWER_YES_OR_NO], previousIntent=self._INTENT_DUMMY_WAKEWORD_FAILED) return True time.sleep(0.5) filepath = Path( tempfile.gettempdir(), str(self.WakewordManager.getLastSampleNumber())).with_suffix( '.wav') self.playSound(soundFile=str(filepath), sessionId='checking-wakeword', siteId=session.siteId, absolutePath=True) self.continueDialog( sessionId=sessionId, text=self.randomTalk('howWasTheCaptureNow'), intentFilter=[self._INTENT_ANSWER_WAKEWORD_CUTTING], slot='WakewordCaptureResult', previousIntent=self._INTENT_WAKEWORD) elif intent == self._INTENT_SWITCH_LANGUAGE: self.publish(topic='hermes/asr/textCaptured', payload={'siteId': siteId}) if 'ToLang' not in slots: self.endDialog(text=self.randomTalk('noDestinationLanguage')) return True try: self.LanguageManager.changeActiveLanguage(slots['ToLang']) self.ThreadManager.doLater( interval=3, func=self.langSwitch, args=[slots['ToLang'], siteId, False]) except LanguageManagerLangNotSupported: self.endDialog(text=self.randomTalk(text='langNotSupported', replace=[slots['ToLang']])) except ConfigurationUpdateFailed: self.endDialog(text=self.randomTalk('langSwitchFailed')) elif intent == self._INTENT_UPDATE_ALICE: if not self.InternetManager.online: self.endDialog( sessionId=sessionId, text=self.randomTalk('noAssistantUpdateOffline')) return True self.publish('hermes/leds/systemUpdate') if 'WhatToUpdate' not in slots: update = 1 elif slots['WhatToUpdate'] == 'alice': update = 2 elif slots['WhatToUpdate'] == 'assistant': update = 3 elif slots['WhatToUpdate'] == 'modules': update = 4 else: update = 5 if update in {1, 5}: # All or system self._logger.info('[{}] Updating system'.format(self.name)) self.endDialog(sessionId=sessionId, text=self.randomTalk('confirmAssistantUpdate')) def systemUpdate(): subprocess.run(['sudo', 'apt-get', 'update']) subprocess.run(['sudo', 'apt-get', 'dist-upgrade', '-y']) subprocess.run(['git', 'stash']) subprocess.run(['git', 'pull']) subprocess.run(['git', 'stash', 'clear']) SuperManager.getInstance().threadManager.doLater( interval=2, func=subprocess.run, args=['sudo', 'systemctl', 'restart', 'ProjectAlice']) self.ThreadManager.doLater(interval=2, func=systemUpdate) if update in {1, 4}: # All or modules self._logger.info('[{}] Updating modules'.format(self.name)) self.endDialog(sessionId=sessionId, text=self.randomTalk('confirmAssistantUpdate')) self.ModuleManager.checkForModuleUpdates() if update in {1, 2}: # All or Alice self._logger.info('[{}] Updating Alice'.format(self.name)) if update == 2: self.endDialog( sessionId=sessionId, text=self.randomTalk('confirmAssistantUpdate')) if update in {1, 3}: # All or Assistant self._logger.info('[{}] Updating assistant'.format(self.name)) if not self.LanguageManager.activeSnipsProjectId: self.endDialog(sessionId=sessionId, text=self.randomTalk('noProjectIdSet')) elif not self.SnipsConsoleManager.loginCredentialsAreConfigured( ): self.endDialog( sessionId=sessionId, text=self.randomTalk('bundleUpdateNoCredentials')) else: if update == 3: self.endDialog( sessionId=sessionId, text=self.randomTalk('confirmAssistantUpdate')) self.ThreadManager.doLater(interval=2, func=self.SamkillaManager.sync) elif intent == self._INTENT_REBOOT: self.continueDialog(sessionId=sessionId, text=self.randomTalk('confirmReboot'), intentFilter=[self._INTENT_ANSWER_YES_OR_NO], previousIntent=self._INTENT_REBOOT, customData={ 'module': self.name, 'step': 1 }) elif intent == self._INTENT_STOP_LISTEN: if 'Duration' in slots: duration = commons.getDuration(session) if duration > 0: self.ThreadManager.doLater(interval=duration, func=self.unmuteSite, args=[siteId]) aliceModule = self.ModuleManager.getModuleInstance( 'AliceSatellite') if aliceModule: aliceModule.notifyDevice('projectalice/devices/stopListen', siteId=siteId) self.endDialog(sessionId=sessionId) elif session.previousIntent == self._INTENT_DUMMY_ADD_USER and intent in { self._INTENT_ANSWER_NAME, self._INTENT_SPELL_WORD }: if not self.UserManager.users: if intent == self._INTENT_ANSWER_NAME: name: str = str(slots['Name']).lower() if commons.isSpelledWord(name): name = name.replace(' ', '') else: name = ''.join( [slot.value['value'] for slot in slotsObj['Letters']]) if name in self.UserManager.getAllUserNames(skipGuests=False): self.continueDialog( sessionId=sessionId, text=self.randomTalk(text='userAlreadyExist', replace=[name]), intentFilter=[ self._INTENT_ANSWER_NAME, self._INTENT_SPELL_WORD ], previousIntent=self._INTENT_DUMMY_ADD_USER) else: self.continueDialog( sessionId=sessionId, text=self.randomTalk(text='confirmUsername', replace=[name]), intentFilter=[self._INTENT_ANSWER_YES_OR_NO], previousIntent=self._INTENT_DUMMY_ADD_USER, customData={'name': name}) else: self.endDialog(sessionId) elif intent in { self._INTENT_ADD_USER, self._INTENT_ANSWER_ACCESSLEVEL } or session.previousIntent == self._INTENT_ADD_USER and intent != self._INTENT_SPELL_WORD: if 'Name' not in slots: self.continueDialog( sessionId=sessionId, text=self.randomTalk('addUserWhatsTheName'), intentFilter=[self._INTENT_ANSWER_NAME], previousIntent=self._INTENT_ADD_USER, slot='Name') return True if session.slotRawValue('Name') == constants.UNKNOWN_WORD: self.continueDialog( sessionId=sessionId, text=self.TalkManager.randomTalk('notUnderstood', module='system'), intentFilter=[ self._INTENT_ANSWER_NAME, self._INTENT_SPELL_WORD ], previousIntent=self._INTENT_ADD_USER) return True if slots['Name'] in self.UserManager.getAllUserNames( skipGuests=False): self.continueDialog( sessionId=sessionId, text=self.randomTalk(text='userAlreadyExist', replace=[slots['Name']]), intentFilter=[ self._INTENT_ANSWER_NAME, self._INTENT_SPELL_WORD ], previousIntent=self._INTENT_ADD_USER) return True if 'UserAccessLevel' not in slots: self.continueDialog( sessionId=sessionId, text=self.randomTalk('addUserWhatAccessLevel'), intentFilter=[self._INTENT_ANSWER_ACCESSLEVEL], previousIntent=self._INTENT_ADD_USER, slot='UserAccessLevel') return True self.continueDialog(sessionId=sessionId, text=self.randomTalk( text='addUserConfirmUsername', replace=[slots['Name']]), intentFilter=[self._INTENT_ANSWER_YES_OR_NO], previousIntent=self._INTENT_ADD_USER, customData={'username': slots['Name']}) return True elif intent == self._INTENT_SPELL_WORD and session.previousIntent == self._INTENT_ADD_USER: name = ''.join( [slot.value['value'] for slot in slotsObj['Letters']]) session.slots['Name']['value'] = name if name in self.UserManager.getAllUserNames(skipGuests=False): self.continueDialog( sessionId=sessionId, text=self.randomTalk(text='userAlreadyExist', replace=[name]), intentFilter=[ self._INTENT_ANSWER_NAME, self._INTENT_SPELL_WORD ], previousIntent=self._INTENT_ADD_USER) else: self.continueDialog( sessionId=sessionId, text=self.randomTalk(text='addUserConfirmUsername', replace=[name]), intentFilter=[self._INTENT_ANSWER_YES_OR_NO], previousIntent=self._INTENT_ADD_USER, customData={'username': name}) return True