Exemplo n.º 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:
                # 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))
Exemplo n.º 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:
                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))
Exemplo n.º 3
0
    def in_started(self, message: Any, sender: RhasspyActor) -> None:
        """Handle messages in started state."""
        if isinstance(message, HandleIntent):
            intent = message.intent
            try:
                intent = self.handle_intent(intent)
            except Exception as e:
                self._logger.exception("handle_intent")
                intent["error"] = str(e)

            self.send(message.receiver or sender, IntentHandled(intent))
        elif isinstance(message, ForwardIntent):
            intent = message.intent
            try:
                intent_name = pydash.get(intent, "intent.name", "")
                event_type: str = ""
                event_data: Dict[str, Any] = {}

                if "hass_event" not in intent:
                    event_type, event_data = self.make_hass_event(intent)
                    intent["hass_event"] = {
                        "event_type": event_type,
                        "event_data": event_data,
                    }
                else:
                    event_type = intent["hass_event"]["event_type"]
                    event_data = intent["hass_event"]["event_data"]

                self.forward_intent(intent_name, event_type, event_data)
            except Exception as e:
                self._logger.exception("forward_intent")
                intent["error"] = str(e)

            self.send(message.receiver or sender, IntentForwarded(intent))
Exemplo n.º 4
0
 def in_started(self, message: Any, sender: RhasspyActor) -> None:
     """Handle messages in started state."""
     if isinstance(message, HandleIntent):
         self.send(message.receiver or sender,
                   IntentHandled(message.intent))
     elif isinstance(message, ForwardIntent):
         self.send(message.receiver or sender,
                   IntentForwarded(message.intent))
Exemplo n.º 5
0
 def in_forwarding(self, message: Any, sender: RhasspyActor) -> None:
     """Handle messages in forwarding state."""
     if isinstance(message, IntentForwarded):
         # Return back to sender
         self.transition("ready")
         self.send(self.receiver, IntentHandled(message.intent))