Пример #1
0
    def in_ready(self, message: Any, sender: RhasspyActor) -> None:
        """Handle messages in ready state."""
        if isinstance(message, HandleIntent):
            self.receiver = message.receiver or sender
            intent = message.intent
            try:
                self._logger.debug(self.command)

                # JSON -> STDIN -> STDOUT -> JSON
                json_input = json.dumps(intent).encode()
                output = subprocess.run(
                    self.command,
                    check=True,
                    input=json_input,
                    stdout=subprocess.PIPE).stdout.decode()

                intent = json.loads(output)
                self._logger.debug(intent)

                # Check for speech
                speech = intent.get("speech", {})
                speech_text = speech.get("text", "")
                if speech_text and self.speech_actor:
                    self.send(self.speech_actor, SpeakSentence(speech_text))
            except Exception as e:
                self._logger.exception("in_started")
                intent["error"] = str(e)

            if self.forward_to_hass and self.hass_handler:
                self.transition("forwarding")
                self.send(self.hass_handler, ForwardIntent(intent))
            else:
                # No forwarding
                self.send(self.receiver, IntentHandled(intent))
Пример #2
0
    def in_ready(self, message: Any, sender: RhasspyActor) -> None:
        """Handle messages in ready state."""
        if isinstance(message, HandleIntent):
            self.receiver = message.receiver or sender
            intent = message.intent
            try:
                # JSON -> Remote -> JSON
                response = requests.post(self.remote_url, json=message.intent)
                response.raise_for_status()

                intent = response.json()
                self._logger.debug(intent)

                # Check for speech
                speech = intent.get("speech", {})
                speech_text = speech.get("text", "")
                if speech_text and self.speech_actor:
                    self.send(self.speech_actor, SpeakSentence(speech_text))
            except Exception as e:
                self._logger.exception("in_started")
                intent["error"] = str(e)

            if self.forward_to_hass and self.hass_handler:
                self.transition("forwarding")
                self.send(self.hass_handler, ForwardIntent(intent))
            else:
                # No forwarding
                self.send(self.receiver, IntentHandled(intent))