def onSnipsIntentNotRecognized(self, client, data, msg: mqtt.MQTTMessage): sessionId = commons.parseSessionId(msg) session = self.DialogSessionManager.getSession(sessionId) if not session: self.ask(text=self.TalkManager.randomTalk('notUnderstood', module='system'), client=session.siteId) else: if msg.topic == Intent('UserRandomAnswer'): return if session.customData and 'module' in session.customData and 'RandomWord' in session.slots: module = self.ModuleManager.getModuleInstance( session.customData['module']) if module: module.onMessage(Intent('UserRandomAnswer'), session) return if session.notUnderstood < 3: session.notUnderstood = session.notUnderstood + 1 self.reviveSession( session, self.TalkManager.randomTalk('notUnderstood', module='system')) else: del session.notUnderstood self.endDialog(sessionId=sessionId, text=self.TalkManager.randomTalk( 'notUnderstoodEnd', module='system')) SuperManager.getInstance().broadcast(method='onIntentNotRecognized', exceptions=[self.name], propagateToModules=True, session=session)
def processMessage(self, message: MQTTMessage) -> bool: sessionId = commons.parseSessionId(message) session = self.DialogSessionManager.getSession(sessionId) if not session or self.isProcessing(sessionId): return False payload = session.payload if 'input' in payload: separators = self.LanguageManager.getStrings('intentSeparator') GLUE_SPLITTER = '__multi_intent__' userInput = payload['input'] for separator in separators: userInput.replace(separator, GLUE_SPLITTER) if GLUE_SPLITTER in userInput: multiIntent = MultiIntent(session, userInput) for string in userInput.split(GLUE_SPLITTER): multiIntent.addIntent(string) self._multiIntents[session.sessionId] = multiIntent return self.processNextIntent(session.sessionId) else: return False else: return False
def onSnipsCaptured(self, client, data, msg: mqtt.MQTTMessage): sessionId = commons.parseSessionId(msg) session = self.DialogSessionManager.getSession(sessionId=sessionId) if session: SuperManager.getInstance().broadcast(method='onCaptured', exceptions=[self.name], propagateToModules=True, session=session)
def onSnipsSessionQueued(self, client, data, msg: mqtt.MQTTMessage): sessionId = commons.parseSessionId(msg) session = self.DialogSessionManager.addSession(sessionId=sessionId, message=msg) if session: SuperManager.getInstance().broadcast(method='onSessionQueued', exceptions=[self.name], args=[session], propagateToModules=True)
def onSnipsSayFinished(self, client, data, msg: mqtt.MQTTMessage): sessionId = commons.parseSessionId(msg) payload = commons.payload(msg) session = self.DialogSessionManager.getSession(sessionId) if session: session.payload = payload SuperManager.getInstance().broadcast(method='onSayFinished', exceptions=[self.name], args=[session], propagateToModules=True)
def onSnipsIntentParsed(self, client, data, msg: mqtt.MQTTMessage): sessionId = commons.parseSessionId(msg) session = self.DialogSessionManager.getSession(sessionId=sessionId) if session: session.update(msg) SuperManager.getInstance().broadcast(method='onIntentParsed', exceptions=[self.name], propagateToModules=True, session=session) if self.ConfigManager.getAliceConfigByName( 'asr').lower() != 'snips': intent = Intent( session.payload['intent']['intentName'].split(':')[1]) message = mqtt.MQTTMessage(topic=str.encode(str(intent))) message.payload = json.dumps(session.payload) self.onMessage(client=client, userdata=data, message=message)
def onSnipsSay(self, client, data, msg: mqtt.MQTTMessage): sessionId = commons.parseSessionId(msg) payload = commons.payload(msg) session = self.DialogSessionManager.getSession(sessionId) if session: session.payload = payload siteId = session.siteId else: siteId = commons.parseSiteId(msg) if 'text' in payload: module = self.ModuleManager.getModuleInstance('ContextSensitive') if module: module.addChat(text=payload['text'], siteId=siteId) SuperManager.getInstance().broadcast(method='onSay', exceptions=[self.name], propagateToModules=True, session=session)
def onSnipsSessionEnded(self, client, data, msg: mqtt.MQTTMessage): sessionId = commons.parseSessionId(msg) session = self.DialogSessionManager.getSession(sessionId) if session: session.update(msg) else: self.ModuleManager.broadcast('onSessionEnded') return reason = session.payload['termination']['reason'] if reason: if reason == 'abortedByUser': SuperManager.getInstance().broadcast(method='onUserCancel', exceptions=[self.name], propagateToModules=True, session=session) elif reason == 'timeout': SuperManager.getInstance().broadcast(method='onSessionTimeout', exceptions=[self.name], propagateToModules=True, session=session) elif reason == 'intentNotRecognized': # This should never trigger, as "sendIntentNotRecognized" is always set to True, but we never know self.onSnipsIntentNotRecognized(None, data, msg) elif reason == 'error': SuperManager.getInstance().broadcast(method='onSessionError', exceptions=[self.name], propagateToModules=True, session=session) else: SuperManager.getInstance().broadcast(method='onSessionEnded', exceptions=[self.name], propagateToModules=True, session=session) SuperManager.getInstance().broadcast(method='onSessionEnded', exceptions=[self.name], propagateToModules=True, session=session) self.DialogSessionManager.removeSession(sessionId=sessionId)
def onMessage(self, client, userdata, message: mqtt.MQTTMessage): try: if message.topic == constants.TOPIC_AUDIO_FRAME: SuperManager.getInstance().broadcast(method='onAudioFrame', exceptions=[self.name], propagateToModules=True, message=message) return if message.topic == constants.TOPIC_INTENT_PARSED: return siteId = commons.parseSiteId(message) payload = commons.payload(message) sessionId = commons.parseSessionId(message) session = self.DialogSessionManager.getSession(sessionId) if session: session.update(message) if self.MultiIntentManager.processMessage(message): return if message.topic == constants.TOPIC_TEXT_CAPTURED and session: return elif message.topic == constants.TOPIC_ASR_START_LISTENING: self.ModuleManager.broadcast('onListening', siteId=siteId) return elif message.topic == constants.TOPIC_HOTWORD_TOGGLE_ON: self.ModuleManager.broadcast('onHotwordToggleOn', siteId=siteId) return session = self.DialogSessionManager.getSession(sessionId) if not session: session = self.DeviceManager.onMessage(message) if not session: self._logger.warning( f'[{self.name}] Got a message on ({message.topic}) but nobody knows what to do with it' ) self.endDialog(sessionId) return redQueen = self.ModuleManager.getModuleInstance('RedQueen') if redQueen and not redQueen.inTheMood(session): return customData = session.customData if 'intent' in payload and payload['intent'][ 'confidenceScore'] < self.ConfigManager.getAliceConfigByName( 'probabilityTreshold'): if session.notUnderstood < 3: session.notUnderstood = session.notUnderstood + 1 self.continueDialog(sessionId=sessionId, text=self.TalkManager.randomTalk( 'notUnderstood', module='system')) else: del session.notUnderstood self.endDialog(sessionId=sessionId, text=self.TalkManager.randomTalk( 'notUnderstoodEnd', module='system')) return del session.notUnderstood module = self.ModuleManager.getModuleInstance('ContextSensitive') if module: module.addToMessageHistory(session) modules = self.ModuleManager.getModules() for modul in modules.values(): module = modul['instance'] try: consumed = module.filterIntent( message.topic, session) and module.onMessage( message.topic, session) except AccessLevelTooLow: # The command was recognized but required higher access level return # Authentication might end the session directly from a module if not self.DialogSessionManager.getSession(sessionId): return if self.MultiIntentManager.isProcessing(sessionId): self.MultiIntentManager.processNextIntent(sessionId) return elif consumed: return self._logger.warning( f"[{self.name}] Intent \"{message.topic}\" wasn't consumed by any module" ) self.endDialog(sessionId) except Exception as e: try: self._logger.info(traceback.print_exc()) except: pass self._logger.error( f'[{self.name}] Uncaught error in onMessage: {e}')