Exemplo n.º 1
0
 async def handle_intent(self, intent: Dict[str, Any]) -> IntentHandled:
     """Handle an intent."""
     assert self.actor_system is not None
     with self.actor_system.private() as sys:
         result = await sys.async_ask(self.dialogue_manager,
                                      HandleIntent(intent))
         assert isinstance(result, IntentHandled), result
         return result
Exemplo n.º 2
0
    def in_recognizing(self, message: Any, sender: RhasspyActor) -> None:
        """Handle messages in recognizing state."""
        if isinstance(message, IntentRecognized):

            if not pydash.get(message.intent, "intent.name", ""):
                if self.profile.get("intent.error_sound", True):
                    # Play error sound when not recognized
                    wav_path = os.path.expandvars(
                        self.profile.get("sounds.error", None)
                    )
                    if wav_path is not None:
                        self.send(self.player, PlayWavFile(wav_path))

            if self.recorder_class == HTTPAudioRecorder:
                # Forward to audio recorder
                self.send(self.recorder, message)

            message.intent["wakeId"] = self.wake_detected_name or ""
            message.intent["siteId"] = self.site_id

            # Augment with extra entities
            entities = message.intent.get("entities", [])
            entities.extend(self.listen_entities)
            message.intent["entities"] = entities

            slots = message.intent.get("slots", {})
            for entity_dict in self.listen_entities:
                slots[entity_dict["entity"]] = entity_dict["value"]

            # Handle intent
            self._logger.debug(message.intent)
            if message.handle:
                # Forward to Home Assistant
                self.send(self.handler, HandleIntent(message.intent))

                # Forward to MQTT (hermes)
                if self.mqtt is not None:
                    self.send(self.mqtt, message)

                # Forward to observer
                if self.observer:
                    self.send(self.observer, message)

                self.transition("handling")
            else:
                self._logger.debug("Not actually handling intent")
                if self.intent_receiver is not None:
                    self.send(self.intent_receiver, message.intent)
                self.transition("ready")
        else:
            self.handle_any(message, sender)
Exemplo n.º 3
0
    def handle_any(self, message: Any, sender: RhasspyActor) -> None:
        """Handle messages in any state."""
        if isinstance(message, ListenForCommand):
            # Force voice command
            self.handle = message.handle
            self.intent_receiver = message.receiver or sender
            self.listen_timeout_sec = message.timeout
            self.listen_entities = message.entities
            self.transition("awake")
        elif isinstance(message, GetVoiceCommand):
            # Record voice command, but don't do anything with it
            self.send(
                self.command,
                ListenForCommand(message.receiver or sender, timeout=message.timeout),
            )
        elif isinstance(message, TranscribeWav):
            # speech -> text
            self.send(
                self.decoder,
                TranscribeWav(message.wav_data, sender, handle=message.handle),
            )
        elif isinstance(message, RecognizeIntent):
            # text -> intent
            self.send(
                self.recognizer,
                RecognizeIntent(
                    message.text,
                    confidence=message.confidence,
                    receiver=sender,
                    handle=message.handle,
                ),
            )
        elif isinstance(message, HandleIntent):
            # intent -> action
            self.send(self.handler, HandleIntent(message.intent, sender))

            # Forward to MQTT (hermes)
            if self.mqtt is not None:
                self.send(self.mqtt, IntentRecognized(message.intent))
        elif isinstance(message, GetWordPhonemes):
            # eSpeak -> CMU
            self.send(
                self.word_pronouncer, GetWordPhonemes(message.word, receiver=sender)
            )
        elif isinstance(message, SpeakWord):
            # eSpeak -> WAV
            self.send(self.word_pronouncer, SpeakWord(message.word, receiver=sender))
        elif isinstance(message, GetWordPronunciations):
            # word -> [CMU]
            self.send(
                self.word_pronouncer,
                GetWordPronunciations(message.words, n=message.n, receiver=sender),
            )
        elif isinstance(message, SpeakSentence):
            # text -> speech
            self.send(
                self.speech,
                SpeakSentence(
                    message.sentence,
                    receiver=sender,
                    play=message.play,
                    voice=message.voice,
                    language=message.language,
                ),
            )
        elif isinstance(message, TrainProfile):
            # Training
            self.reload_actors_after_training = message.reload_actors
            self.send(self.wake, StopListeningForWakeWord())
            self.training_receiver = message.receiver or sender
            self.transition("training_sentences")
            # self.send(self.sentence_generator, GenerateSentences())
        elif isinstance(message, StartRecordingToBuffer):
            # Record WAV
            self.send(self.recorder, message)
        elif isinstance(message, StopRecordingToBuffer):
            # Stop recording WAV
            self.send(
                self.recorder,
                StopRecordingToBuffer(message.buffer_name, message.receiver or sender),
            )
        elif isinstance(message, StateTransition):
            # Track state of every actor
            self.handle_transition(message, sender)
        elif isinstance(message, GetActorStates):
            self.send(sender, self.actor_states)
        elif isinstance(message, WakeupMessage):
            pass
        elif isinstance(message, WavPlayed):
            pass
        elif isinstance(message, GetProblems):
            # Report problems from child actors
            self.send(sender, Problems(self.problems))
        else:
            self.handle_forward(message, sender)