예제 #1
0
    async def handle_not_recognized(
        self, not_recognized: NluIntentNotRecognized
    ) -> typing.AsyncIterable[
            typing.Union[DialogueIntentNotRecognized, EndSessionType,
                         StartSessionType, SayType]]:
        """Failed to recognized intent."""
        try:
            if self.session is None or (self.session.session_id !=
                                        not_recognized.session_id):
                _LOGGER.warning("No session for %s", not_recognized.session_id)
                return

            _LOGGER.warning("No intent recognized (session_id=%s)",
                            not_recognized.session_id)

            if self.session.send_intent_not_recognized:
                # Client will handle
                yield DialogueIntentNotRecognized(
                    session_id=self.session.session_id,
                    custom_data=self.session.custom_data,
                    site_id=not_recognized.site_id,
                    input=not_recognized.input,
                )
            else:
                # End session automatically
                async for end_result in self.end_session(
                        DialogueSessionTerminationReason.INTENT_NOT_RECOGNIZED,
                        site_id=not_recognized.site_id,
                ):
                    yield end_result
        except Exception:
            _LOGGER.exception("handle_not_recognized")
예제 #2
0
    async def handle_not_recognized(
        self, not_recognized: NluIntentNotRecognized
    ) -> typing.AsyncIterable[
        typing.Union[
            DialogueIntentNotRecognized, EndSessionType, StartSessionType, SayType
        ]
    ]:
        """Failed to recognized intent."""
        try:
            if not not_recognized.session_id:
                _LOGGER.debug("Missing session id on not recognized message")
                return

            site_session = self.all_sessions.get(not_recognized.session_id)

            if site_session is None:
                _LOGGER.warning(
                    "No session for id %s. Dropping not recognized.",
                    not_recognized.session_id,
                )
                return

            if not_recognized.custom_data is not None:
                # Overwrite custom data
                site_session.custom_data = not_recognized.custom_data

            _LOGGER.warning(
                "No intent recognized (site_id=%s, session_id=%s)",
                not_recognized.site_id,
                not_recognized.session_id,
            )

            if site_session.send_intent_not_recognized:
                # Client will handle
                yield DialogueIntentNotRecognized(
                    session_id=site_session.session_id,
                    custom_data=site_session.custom_data,
                    site_id=not_recognized.site_id,
                    input=not_recognized.input,
                )
            else:
                # End session automatically
                async for end_result in self.end_session(
                    DialogueSessionTerminationReason.INTENT_NOT_RECOGNIZED,
                    site_id=not_recognized.site_id,
                    session_id=not_recognized.session_id,
                    start_next_session=True,
                ):
                    yield end_result
        except Exception:
            _LOGGER.exception("handle_not_recognized")
예제 #3
0
    async def handle_not_recognized(self,
                                    not_recognized: NluIntentNotRecognized):
        """Failed to recognized intent."""
        try:
            assert self.session, "No session"

            _LOGGER.warning("No intent recognized")
            if self.session.sendIntentNotRecognized:
                # Client will handle
                return DialogueIntentNotRecognized(
                    sessionId=self.session.sessionId,
                    customData=self.session.customData,
                    siteId=self.siteId,
                    input=not_recognized.input,
                )
            else:
                # End session
                return self.end_session(
                    DialogueSessionTerminationReason.INTENT_NOT_RECOGNIZED)
        except Exception:
            _LOGGER.exception("handle_not_recognized")
    def _subscribe_callbacks(self) -> None:
        # Remove duplicate intent names
        intent_names: List[str] = list(set(self._callbacks_intent.keys()))
        topics: List[str] = [
            NluIntent.topic(intent_name=intent_name) for intent_name in intent_names
        ]

        if self._callbacks_hotword:
            topics.append(HotwordDetected.topic())

        if self._callbacks_intent_not_recognized:
            topics.append(NluIntentNotRecognized.topic())

        if self._callbacks_dialogue_intent_not_recognized:
            topics.append(DialogueIntentNotRecognized.topic())

        topic_names: List[str] = list(set(self._callbacks_topic.keys()))
        topics.extend(topic_names)
        topics.extend(self._additional_topic)

        self.subscribe_topics(*topics)
예제 #5
0
    async def on_raw_message(self, topic: str, payload: bytes):
        """This method handles messages from the MQTT broker.

        Arguments:
            topic: The topic of the received MQTT message.

            payload: The payload of the received MQTT message.

        .. warning:: Don't override this method in your app. This is where all the magic happens in Rhasspy Hermes App.
        """
        try:
            if HotwordDetected.is_topic(topic):
                # hermes/hotword/<wakeword_id>/detected
                try:
                    hotword_detected = HotwordDetected.from_json(payload)
                    for function_h in self._callbacks_hotword:
                        await function_h(hotword_detected)
                except KeyError as key:
                    _LOGGER.error("Missing key %s in JSON payload for %s: %s",
                                  key, topic, payload)
            elif NluIntent.is_topic(topic):
                # hermes/intent/<intent_name>
                try:
                    nlu_intent = NluIntent.from_json(payload)
                    intent_name = nlu_intent.intent.intent_name
                    if intent_name in self._callbacks_intent:
                        for function_i in self._callbacks_intent[intent_name]:
                            await function_i(nlu_intent)
                except KeyError as key:
                    _LOGGER.error("Missing key %s in JSON payload for %s: %s",
                                  key, topic, payload)
            elif NluIntentNotRecognized.is_topic(topic):
                # hermes/nlu/intentNotRecognized
                try:
                    nlu_intent_not_recognized = NluIntentNotRecognized.from_json(
                        payload)
                    for function_inr in self._callbacks_intent_not_recognized:
                        await function_inr(nlu_intent_not_recognized)
                except KeyError as key:
                    _LOGGER.error("Missing key %s in JSON payload for %s: %s",
                                  key, topic, payload)
            elif DialogueIntentNotRecognized.is_topic(topic):
                # hermes/dialogueManager/intentNotRecognized
                try:
                    dialogue_intent_not_recognized = DialogueIntentNotRecognized.from_json(
                        payload)
                    for function_dinr in self._callbacks_dialogue_intent_not_recognized:
                        await function_dinr(dialogue_intent_not_recognized)
                except KeyError as key:
                    _LOGGER.error("Missing key %s in JSON payload for %s: %s",
                                  key, topic, payload)
            else:
                unexpected_topic = True
                if topic in self._callbacks_topic:
                    for function_1 in self._callbacks_topic[topic]:
                        await function_1(TopicData(topic, {}), payload)
                        unexpected_topic = False
                else:
                    for function_2 in self._callbacks_topic_regex:
                        if hasattr(function_2, "topic_extras"):
                            topic_extras = getattr(function_2, "topic_extras")
                            for pattern, named_positions in topic_extras:
                                if re.match(pattern, topic) is not None:
                                    data = TopicData(topic, {})
                                    parts = topic.split(sep="/")
                                    if named_positions is not None:
                                        for name, position in named_positions.items(
                                        ):
                                            data.data[name] = parts[position]

                                    function_2(data, payload)
                                    unexpected_topic = False

                if unexpected_topic:
                    _LOGGER.warning("Unexpected topic: %s", topic)

        except Exception:
            _LOGGER.exception("on_raw_message")
예제 #6
0
def test_dialogue_intent_not_recognized():
    """Test DialogueIntentNotRecognized."""
    assert (DialogueIntentNotRecognized.topic() ==
            "hermes/dialogueManager/intentNotRecognized")