Exemple #1
0
async def test_http_parsing():
    message = UserMessage("lunch?")

    endpoint = EndpointConfig("https://interpreter.com")
    with aioresponses() as mocked:
        mocked.post("https://interpreter.com/model/parse",
                    repeat=True,
                    status=200)

        inter = RasaNLUHttpInterpreter(endpoint=endpoint)
        try:
            await MessageProcessor(inter, None, None, None,
                                   None)._parse_message(message)
        except KeyError:
            pass  # logger looks for intent and entities, so we except

        r = latest_request(mocked, "POST",
                           "https://interpreter.com/model/parse")

        assert r
Exemple #2
0
        async def handle_message(sid, data):
            output_channel = SocketIOOutput(sio, sid, self.bot_message_evt)

            if self.session_persistence:
                if not data.get("session_id"):
                    logger.warning("A message without a valid sender_id "
                                   "was received. This message will be "
                                   "ignored. Make sure to set a proper "
                                   "session id using the "
                                   "`session_request` socketIO event.")
                    return
                sender_id = data["session_id"]
            else:
                sender_id = sid

            message = UserMessage(data["message"],
                                  output_channel,
                                  sender_id,
                                  input_channel=self.name())
            await on_new_message(message)
    async def _handle_user_message(
            self, text: Text, sender_id: Text,
            metadata: Optional[Dict[Text, Any]]) -> None:
        """Pass on the text to the dialogue engine for processing."""

        out_channel = MessengerBot(self.client)
        user_msg = UserMessage(text,
                               out_channel,
                               sender_id,
                               input_channel=self.name(),
                               metadata=metadata)

        # noinspection PyBroadException
        try:
            await self.on_new_message(user_msg)
        except Exception:
            logger.exception(
                "Exception when trying to handle webhook for facebook message."
            )
            pass
Exemple #4
0
    async def add_message(request: Request, conversation_id: Text):
        validate_request_body(
            request,
            "No message defined in request body. Add a message to the request body in "
            "order to add it to the tracker.",
        )

        request_params = request.json

        message = request_params.get("text")
        sender = request_params.get("sender")
        parse_data = request_params.get("parse_data")

        verbosity = event_verbosity_parameter(request,
                                              EventVerbosity.AFTER_RESTART)

        # TODO: implement for agent / bot
        if sender != "user":
            raise ErrorResponse(
                400,
                "BadRequest",
                "Currently, only user messages can be passed to this endpoint. "
                "Messages of sender '{}' cannot be handled.".format(sender),
                {
                    "parameter": "sender",
                    "in": "body"
                },
            )

        try:
            user_message = UserMessage(message, None, conversation_id,
                                       parse_data)
            tracker = await app.agent.log_message(user_message)
            return response.json(tracker.current_state(verbosity))
        except Exception as e:
            logger.debug(traceback.format_exc())
            raise ErrorResponse(
                500,
                "ConversationError",
                "An unexpected error occurred. Error: {}".format(e),
            )
Exemple #5
0
    async def handle_reminder(
        self,
        reminder_event: ReminderScheduled,
        sender_id: Text,
        output_channel: OutputChannel,
        nlg: NaturalLanguageGenerator,
    ) -> None:
        """Handle a reminder that is triggered asynchronously."""

        tracker = self._get_tracker(sender_id)

        if not tracker:
            logger.warning(
                f"Failed to retrieve or create tracker for sender '{sender_id}'."
            )
            return None

        if (
            reminder_event.kill_on_user_message
            and self._has_message_after_reminder(tracker, reminder_event)
            or not self._is_reminder_still_valid(tracker, reminder_event)
        ):
            logger.debug(
                "Canceled reminder because it is outdated. "
                "(event: {} id: {})".format(
                    reminder_event.action_name, reminder_event.name
                )
            )
        else:
            # necessary for proper featurization, otherwise the previous
            # unrelated message would influence featurization
            tracker.update(UserUttered.empty())
            action = self._get_action(reminder_event.action_name)
            should_continue = await self._run_action(
                action, tracker, output_channel, nlg
            )
            if should_continue:
                user_msg = UserMessage(None, output_channel, sender_id)
                await self._predict_and_execute_next_action(user_msg, tracker)
            # save tracker state to continue conversation from this state
            self._save_tracker(tracker)
Exemple #6
0
        async def handle_message(sid: Text, data: Dict) -> Any:
            output_channel = SocketIOOutput(sio, self.bot_message_evt)

            if self.session_persistence:
                if not data.get("session_id"):
                    rasa.shared.utils.io.raise_warning(
                        "A message without a valid session_id "
                        "was received. This message will be "
                        "ignored. Make sure to set a proper "
                        "session id using the "
                        "`session_request` socketIO event."
                    )
                    return
                sender_id = data["session_id"]
            else:
                sender_id = sid

            message = UserMessage(
                data["message"], output_channel, sender_id, input_channel=self.name()
            )
            if data["message"][0] != '/':

                logger.debug(f"from_user_id={data['customData']['from_user_id']}, "
                             f"session_id={data['session_id']}, "
                             f"content={data['message']}, "
                             f"ip_address={self.remote_addr}")

                query = """insert into message_received
                    (from_user_id, session_id, content, `when`, ip_address)
                    values (%s, %s, %s, %s, %s)"""
                try:
                    self.c.execute(query, (data["customData"]["from_user_id"],
                                           data["session_id"],
                                           data["message"],
                                           time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()),
                                           self.remote_addr))
                    self.c.connection.commit()
                    logger.debug("self.c.connection.commit()")
                except Exception as e:
                    logger.error(e)
            await on_new_message(message)
        async def handle_message(sid, data):

            output_channel = SocketIOOutput(sio, sid, self.bot_message_evt, data['message'])
            if data['message'] == "/get_started":
                message = data['message']
            else:
                ##receive audio
                received_file = 'output_'+sid+'.wav'

                urllib.request.urlretrieve(data['message'], received_file)
                path = os.path.dirname(__file__)

                fs, audio = wav.read("output_{0}.wav".format(sid))
                message = ds.stt(audio, fs)

                await sio.emit(self.user_message_evt, {"text":message}, room=sid)


            message_rasa = UserMessage(message, output_channel, sid,
                                  input_channel=self.name())
            await on_new_message(message_rasa)
Exemple #8
0
    async def _handle_user_message(
            self, text: Text, sender_id: Text, metadata: Optional[Dict[Text, Any]], bot: str
    ) -> None:
        """Pass on the text to the dialogue engine for processing."""

        out_channel = MessengerBot(self.client)
        await out_channel.send_action(sender_id, sender_action="mark_seen")

        user_msg = UserMessage(
            text, out_channel, sender_id, input_channel=self.name(), metadata=metadata
        )
        await out_channel.send_action(sender_id, sender_action="typing_on")
        # noinspection PyBroadException
        try:
            await self.process_message(bot, user_msg)
        except Exception:
            logger.exception(
                "Exception when trying to handle webhook for facebook message."
            )
        finally:
            await out_channel.send_action(sender_id, sender_action="typing_off")
Exemple #9
0
        async def handle_message(sid, data):

            output_channel = SocketIOOutput(sio, sid, self.bot_message_evt,
                                            data['message'])
            if data['message'] == "/get_started":
                message = data['message']
            else:
                ##receive audio
                received_file = 'output_' + sid + '.wav'

                urllib.request.urlretrieve(data['message'], received_file)

                message = speech_to_text(received_file)
                await sio.emit(self.user_message_evt, {"text": message},
                               room=sid)

            message_rasa = UserMessage(message,
                                       output_channel,
                                       sid,
                                       input_channel=self.name())
            await on_new_message(message_rasa)
Exemple #10
0
    async def process_message(
        self,
        on_new_message: Callable[[UserMessage], Awaitable[Any]],
        text: Optional[Text],
        sender_id: Optional[Text],
        metadata: Optional[Dict],
    ) -> Any:

        try:
            out_channel = self.get_output_channel()
            user_msg = UserMessage(
                text,
                out_channel,
                sender_id,
                input_channel=self.name(),
                metadata=metadata,
            )
            await on_new_message(user_msg)
        except Exception as e:
            logger.error(f"Exception when trying to handle message.{e}")
            logger.error(str(e), exc_info=True)
Exemple #11
0
        async def receive(request: Request) -> HTTPResponse:
            sender_id = await self._extract_sender(request)
            text = self._extract_message(request)
            message_type = self._extract_type(request)
            should_use_stream = rasa.utils.endpoints.bool_arg(request,
                                                              'stream',
                                                              default=False)
            input_channel = self._extract_input_channel(request)
            metadata = self.get_metadata(request)

            if should_use_stream:
                return response.stream(
                    self.stream_response(on_new_message, text, sender_id,
                                         input_channel, metadata),
                    content_type='text/event-stream',
                )
            else:
                collector = CollectingOutputChannel()
                # noinspection PyBroadException
                try:
                    await on_new_message(
                        UserMessage(
                            text,
                            collector,
                            sender_id,
                            input_channel=input_channel,
                            metadata=metadata,
                        ))
                except CancelledError:
                    logger.error(f'Message handling timed out for '
                                 f"user message '{text}'.")
                except Exception:
                    logger.exception(f'An exception occurred while handling '
                                     f"user message '{text}'.")
                messages = collector.messages
                for message_rasa in messages:
                    if message_type == 'audio':
                        audio = synthesize_text(message_rasa['text'])
                        message_rasa['audio'] = audio
                return response.json(messages)
        async def handle_message(sid, data):
            output_channel = SocketIOOutput(sio, sid, self.bot_message_evt)
            print(data)
            #await output_channel._send_message(sid,{"text": "__ /!\ Enter the passphrase to use the chatbot :__"})
            #await message_handler.handle_message(data, db_users, db_users.search(User.userName == data['customData']['userName']), output_channel, sid)
            ''' if( data["message"] == "%/get_started"):      
                message = {"text": "__Welcome to the chatbot testing page, please enter provided passphrase to access functionalities__", "quick_replies": []}
                message["quick_replies"].append({
                    "content_type": "text",
                    "title": "English",
                    "payload": "/English",
                })
                message["quick_replies"].append({
                    "content_type": "text",
                    "title": "French",
                    "payload": "/French",
                })
                # await output_channel._send_message(sid,message)
            else:
                await output_channel._send_message(sid,{"text": "__ /!\ Enter the passphrase to use the chatbot :__"})'''

            if self.session_persistence:
                if not data.get("session_id"):
                    logger.warning("A message without a valid sender_id "
                                   "was received. This message will be "
                                   "ignored. Make sure to set a proper "
                                   "session id using the "
                                   "`session_request` socketIO event.")
                    return
                sender_id = data["session_id"]
            else:
                sender_id = sid
                logger.info("message reçu")

            message = UserMessage(data["message"],
                                  output_channel,
                                  sender_id,
                                  input_channel=self.name())
            await on_new_message(message)
            print(message)
Exemple #13
0
        async def receive(request: Request) -> HTTPResponse:
            sender_id = await self._extract_sender(request)
            text = self._extract_message(request)
            should_use_stream = rasa.utils.endpoints.bool_arg(
                request, "stream", default=False
            )
            input_channel = self._extract_input_channel(request)
            metadata = self.get_metadata(request)

            if should_use_stream:
                return response.stream(
                    self.stream_response(
                        on_new_message, text, sender_id, input_channel, metadata
                    ),
                    content_type="text/event-stream",
                )
            else:
                collector = CollectingOutputChannel()
                # noinspection PyBroadException
                try:
                    await on_new_message(
                        UserMessage(
                            text,
                            collector,
                            sender_id,
                            input_channel=input_channel,
                            metadata=metadata,
                        )
                    )
                except CancelledError:
                    logger.error(
                        "Message handling timed out for "
                        "user message '{}'.".format(text)
                    )
                except Exception:
                    logger.exception(
                        "An exception occured while handling "
                        "user message '{}'.".format(text)
                    )
                return response.json(collector.messages)
Exemple #14
0
        async def receive(request: Request):
            """
            Webhook endpoint that is called by Google API
            """
            payload = request.json
            user_id = payload['user']['userId']
            intent = payload['inputs'][0]['intent']
            query = payload['inputs'][0]['rawInputs'][0]['query']

            if intent == 'actions.intent.MAIN':
                message = '<speak>Hello! <break time="0.3"/>I am an example Bot.</speak>'
            else:
                output_channel = GoogleAssistantOutput()
                await on_new_message(
                    UserMessage(query, output_channel, user_id, input_channel=GoogleAssistantInput.name()))
                message = [message['text'] for message in output_channel.messages][0]
                logger.debug(output_channel.messages)

            response_json = {
                "conversationToken": "{\"state\":null,\"data\":{}}",
                "expectUserResponse": 'true',
                "expectedInputs": [
                    {
                        "inputPrompt": {
                            "initialPrompts": [
                                {
                                    # This is a very basic MVP that can only return text answers from the bot!
                                    "ssml": f'<speak>{message}</speak>'
                                }
                            ]
                        },
                        "possibleIntents": [
                            {
                                "intent": "actions.intent.TEXT"
                            }
                        ]
                    }
                ]
            }
            return response.json(response_json)
Exemple #15
0
        async def webhook(request: Request):
            payload = request.json
            # print(payload)
            # sender_id = payload['user']['userId']
            sender_id = "default"
            intent = payload['inputs'][0]['intent']
            text = payload['inputs'][0]['rawInputs'][0]['query']
            # print(intent)
            if intent == 'actions.intent.MAIN':
                message = "<speak>Hello! <break time=\"1\"/> Welcome to the Rasa-powered Google Assistant skill. You can start by saying hi.</speak>"
            else:
                out = CollectingOutputChannel(
                )  # collect the bot responses Core creates while the bot is processing your messages
                on_new_message(
                    UserMessage(text,
                                out,
                                sender_id,
                                input_channel=self.name()))
                responses = [m["text"] for m in out.messages]
                message = responses[0]

            r = json.dumps({
                "conversationToken":
                "{\"state\":null,\"data\":{}}",
                "expectUserResponse":
                'true',
                "expectedInputs": [{
                    "inputPrompt": {
                        "initialPrompts": [{
                            "ssml": message
                        }]
                    },
                    "possibleIntents": [{
                        "intent": "actions.intent.TEXT"
                    }]
                }]
            })
            print(message)
            print(r)
            return r
Exemple #16
0
async def test_processor_logs_text_tokens_in_tracker(mood_agent: Agent):
    text = "Hello there"
    tokenizer = WhitespaceTokenizer()
    tokens = tokenizer.tokenize(Message(data={"text": text}), "text")
    indices = [(t.start, t.end) for t in tokens]

    message = UserMessage(text)
    tracker_store = InMemoryTrackerStore(mood_agent.domain)
    lock_store = InMemoryLockStore()
    processor = MessageProcessor(
        mood_agent.interpreter,
        mood_agent.policy_ensemble,
        mood_agent.domain,
        tracker_store,
        lock_store,
        TemplatedNaturalLanguageGenerator(mood_agent.domain.responses),
    )
    tracker = await processor.log_message(message)
    event = tracker.get_last_event_for(event_type=UserUttered)
    event_tokens = event.as_dict().get("parse_data").get("text_tokens")

    assert event_tokens == indices
Exemple #17
0
 async def _handle_message(
     self,
     message: Text,
     sender_id: Text,
     bot_channel: Text,
     metadata: Optional[Dict],
     on_new_message: Callable[[UserMessage], Awaitable[None]],
 ):
     try:
         out_channel = MattermostBot(self.url, self.token, bot_channel,
                                     self.webhook_url)
         user_msg = UserMessage(
             message,
             out_channel,
             sender_id,
             input_channel=self.name(),
             metadata=metadata,
         )
         await on_new_message(user_msg)
     except Exception as e:
         logger.error(f"Exception when trying to handle message.{e}")
         logger.debug(e, exc_info=True)
Exemple #18
0
        async def receive(request: Request) -> HTTPResponse:
            sender_id = await self._extract_sender(request)
            user_message = self._extract_message(request)
            sender_name = self._extract_sender_name(request)
            channel_id = self._extract_channel_id(request)
            metadata = self.get_metadata(request)
            role = self._extract_role(request)

            if not request.json.get("type", "") == "message.new":
                return response.text("")

            print("*" * 25,
                  request.json,
                  "*" * 25,
                  sender_id,
                  user_message,
                  sender_name,
                  channel_id,
                  sep='\n')

            if role != "admin":
                collector = await self.get_output_channel(channel_id)
                try:
                    message = await on_new_message(
                        UserMessage(
                            text=user_message,
                            output_channel=collector,
                            sender_id=sender_id,
                            input_channel=self.name(),
                            metadata=metadata,
                        ))
                except CancelledError:
                    logger.error("Message handling timed out for "
                                 "user message '{}'.".format(user_message))
                except Exception:
                    logger.exception("An exception occured while handling "
                                     "user message '{}'.".format(user_message))

            return response.text("")
    async def _handle_user_message(
            self,
            user_text: Text,
            sender_id: Text,
            reply_token: Text,
    ) -> None:
        output_channel = LineBot(self.client, reply_token)
        # print(f"OUTPUT: {output_channel}")
        user_msg = UserMessage(
            text=user_text,
            output_channel=output_channel,
            sender_id=sender_id,
            input_channel=self.name())

        # noinspection PyBroadException
        try:
            await self.on_new_message(user_msg)
        except Exception:
            logger.exception(
                "Exception when trying to handle webhook for Line"
            )
            pass
Exemple #20
0
        async def receive(request: Request):
            print(f"dumping request: {request}")
            print(request.json)
            sender_id = await self._extract_sender(request)
            # sender_id = request.json.get("session")["user"]["userId"]
            print("sender_id: "+sender_id)
            req = request.json.get("request")
            should_use_stream = rasa.utils.endpoints.bool_arg(
                request, "stream", default=False
            )

            if should_use_stream:
                return response.stream(
                    self.stream_response(on_new_message, req, sender_id),
                    content_type="text/event-stream",
                )
            else:
                collector = CollectingOutputChannel()
                # noinspection PyBroadException
                try:
                    await on_new_message(
                        UserMessage(
                            json.dumps(req), collector, sender_id,
                            input_channel=self.name()
                        )
                    )
                except CancelledError:
                    logger.error(
                        "Message handling timed out for "
                        "user message '{}'.".format(req)
                    )
                except Exception:
                    logger.exception(
                        "An exception occured while handling "
                        "user message '{}'.".format(req)
                    )
                # return response.json(collector.messages)
                return response.json(mapp2Echo(collector.messages))
Exemple #21
0
    async def process_message(
        self,
        request: Request,
        on_new_message: Callable[[UserMessage], Awaitable[Any]],
        text,
        sender_id: Optional[Text],
        metadata: Optional[Dict],
    ) -> Any:
        """Slack retries to post messages up to 3 times based on
        failure conditions defined here:
        https://api.slack.com/events-api#failure_conditions
        """
        retry_reason = request.headers.get(self.retry_reason_header)
        retry_count = request.headers.get(self.retry_num_header)
        if retry_count and retry_reason in self.errors_ignore_retry:
            logger.warning("Received retry #{} request from slack"
                           " due to {}".format(retry_count, retry_reason))

            return response.text(None,
                                 status=201,
                                 headers={"X-Slack-No-Retry": 1})

        try:
            out_channel = self.get_output_channel()
            user_msg = UserMessage(
                text,
                out_channel,
                sender_id,
                input_channel=self.name(),
                metadata=metadata,
            )

            await on_new_message(user_msg)
        except Exception as e:
            logger.error(f"Exception when trying to handle message.{e}")
            logger.error(str(e), exc_info=True)

        return response.text("")
Exemple #22
0
        async def handle_message(sid, data):

            output_channel = SocketIOOutput(sio, sid, self.bot_message_evt,
                                            data['message'])
            if data['message'] == "/get_started":
                message = data['message']
            else:
                ##receive audio
                received_file = 'output_' + sid + '.wav'

                urllib.request.urlretrieve(data['message'], received_file)
                path = os.path.dirname(__file__)
                with wave.open("output_{0}.wav".format(sid)) as fd:
                    frames = fd.readframes(1000000)

                fs, audio = wav.read("output_{0}.wav".format(sid))
                print(type(audio))
                audio = speech.RecognitionAudio(content=frames)
                config = speech.RecognitionConfig(
                    encoding=speech.RecognitionConfig.AudioEncoding.LINEAR16,
                    sample_rate_hertz=16000,
                    language_code="fr-FR",
                )
                audio_response = tts_client.recognize(config=config,
                                                      audio=audio)
                message = ""
                for result in audio_response.results:
                    message += u"Transcript: {}".format(
                        result.alternatives[0].transcript)

                await sio.emit(self.user_message_evt, {"text": message},
                               room=sid)
            print(message)
            message_rasa = UserMessage(message,
                                       output_channel,
                                       sid,
                                       input_channel=self.name())
            await on_new_message(message_rasa)
        async def receive(request):
            payload = request.json
            intent = payload['inputs'][0]['intent']
            text = payload['inputs'][0]['rawInputs'][0]['query']

            if intent == 'actions.intent.MAIN':
                message = "Hello! Welcome to the Rasa-powered Google Assistant skill. You can start by saying hi."
            else:
                out = CollectingOutputChannel()
                await on_new_message(UserMessage(text, out))
                responses = [m["text"] for m in out.messages]
                message = responses[0]
            r = {
                "expectUserResponse": 'true',
                "expectedInputs": [
                    {
                        "possibleIntents": [
                            {
                                "intent": "actions.intent.TEXT"
                            }
                        ],
                        "inputPrompt": {
                            "richInitialPrompt": {
                                "items": [
                                    {
                                        "simpleResponse": {
                                            "textToSpeech": message,
                                            "displayText": message
                                        }
                                    }
                                ]
                            }
                        }
                    }
                ]
            }

            return response.json(r)
Exemple #24
0
    async def post(self, bot: str, token: str):
        super().authenticate_channel(token, bot, self.request)
        hangout = ChatDataProcessor.get_channel_config("hangouts", bot=bot, mask_characters=False)
        project_id = hangout['config']['project_id']
        request_data = json_decode(self.request.body)
        if project_id:
            token = self.request.headers.get("Authorization").replace("Bearer ", "")
            self._check_token(token, project_id)

        sender_id = self._extract_sender(request_data)
        room_name = self._extract_room(request_data)
        text = self._extract_message(request_data)
        if text is None:
            return response.text("OK")
        input_channel = self._extract_input_channel()

        collector = HangoutsOutput()

        try:
            await AgentProcessor.get_agent(bot).handle_message(UserMessage(
                    text,
                    collector,
                    sender_id,
                    input_channel=input_channel,
                    metadata={"room": room_name, "out_channel": collector.name()},
                ))
        except CancelledError:
            logger.error(
                "Message handling timed out for " "user message '{}'.".format(text)
            )
        except Exception as e:
            logger.exception(
                f"An exception occurred while handling user message: {e}, "
                f"text: {text}"
            )

        self.write(json_encode(collector.messages))
        return
Exemple #25
0
async def test_action_utter_retrieved_empty_response(default_channel,
                                                     default_nlg,
                                                     default_tracker,
                                                     default_domain):
    from rasa.core.channels.channel import UserMessage

    action_name = "respond_chitchat"
    default_tracker.latest_message = UserMessage(
        "Who are you?",
        parse_data={
            "response_selector": {
                "dummy": {
                    "response": {
                        "name": "I am a bot."
                    }
                }
            }
        },
    )
    events = await ActionRetrieveResponse(action_name).run(
        default_channel, default_nlg, default_tracker, default_domain)

    assert events == []
Exemple #26
0
async def test_action_utter_retrieved_response(default_channel, default_nlg,
                                               default_tracker,
                                               default_domain):
    from rasa.core.channels.channel import UserMessage

    action_name = "respond_chitchat"
    default_tracker.latest_message = UserMessage(
        "Who are you?",
        parse_data={
            "response_selector": {
                "chitchat": {
                    "response": {
                        "name": "I am a bot."
                    }
                }
            }
        },
    )
    events = await ActionRetrieveResponse(action_name).run(
        default_channel, default_nlg, default_tracker, default_domain)

    assert events[0].as_dict().get("text") == BotUttered(
        "I am a bot.").as_dict().get("text")
Exemple #27
0
 async def webhook(request):
     output = request.json
     if output:
         # splitting to get rid of the @botmention
         # trigger we are using for this
         text = output['text'].split(" ", 1)
         text = text[1]
         sender_id = output['user_id']
         self.bot_channel = output['channel_id']
         try:
             out_channel = MattermostBot(self.url, self.team, self.user,
                                         self.pw, self.bot_channel)
             user_msg = UserMessage(text,
                                    out_channel,
                                    sender_id,
                                    input_channel=self.name())
             await on_new_message(user_msg)
         except Exception as e:
             logger.error("Exception when trying to handle "
                          "message.{0}".format(e))
             logger.debug(e, exc_info=True)
             pass
     return response.text("")
Exemple #28
0
async def test_action_utter_default_retrieved_response(default_channel,
                                                       default_nlg,
                                                       default_tracker,
                                                       default_domain):
    from rasa.core.channels.channel import UserMessage

    action_name = "utter_chitchat"
    default_tracker.latest_message = UserMessage(
        "Who are you?",
        parse_data={
            "response_selector": {
                "default": {
                    "response": {
                        "intent_response_key": "chitchat/ask_name",
                        "response_templates": [{
                            "text": "I am a bot."
                        }],
                        "template_name": "utter_chitchat/ask_name",
                    }
                }
            }
        },
    )

    default_domain.templates.update(
        {"utter_chitchat/ask_name": [{
            "text": "I am a bot."
        }]})

    events = await ActionRetrieveResponse(action_name).run(
        default_channel, default_nlg, default_tracker, default_domain)

    assert events[0].as_dict().get("text") == BotUttered(
        "I am a bot.").as_dict().get("text")

    assert (events[0].as_dict().get("metadata").get("template_name") ==
            "utter_chitchat/ask_name")
Exemple #29
0
    async def handle_text(
        self,
        text_message: Union[Text, Dict[Text, Any]],
        message_preprocessor: Optional[Callable[[Text], Text]] = None,
        output_channel: Optional[OutputChannel] = None,
        sender_id: Optional[Text] = UserMessage.DEFAULT_SENDER_ID,
        request_id: Optional[Text] = UserMessage.DEFAULT_REQUEST_ID,
        user_id: Optional[Text] = UserMessage.DEFAULT_USER_ID,
    ) -> Optional[List[Dict[Text, Any]]]:
        """Handle a single message.

        If a message preprocessor is passed, the message will be passed to that
        function first and the return value is then used as the
        input for the dialogue engine.

        The return value of this function depends on the ``output_channel``. If
        the output channel is not set, set to ``None``, or set
        to ``CollectingOutputChannel`` this function will return the messages
        the bot wants to respond.

        :Example:

            >>> from rasa.core.agent import Agent
            >>> from rasa.core.interpreter import RasaNLUInterpreter
            >>> agent = Agent.load("examples/restaurantbot/models/current")
            >>> await agent.handle_text("hello")
            [u'how can I help you?']

        """

        if isinstance(text_message, str):
            text_message = {"text": text_message}

        parse_data = await self.interpreter.parse(text_message.get("text"))
        msg = UserMessage(text_message.get("text"), output_channel, sender_id, request_id, user_id, parse_data)

        return await self.handle_message(msg, message_preprocessor)
Exemple #30
0
        async def webhook(request: Request):
            postdata = request.json

            try:
                if postdata["type"] == "message":
                    out_channel = BotFramework(self.app_id, self.app_password,
                                               postdata["conversation"],
                                               postdata["recipient"],
                                               postdata["serviceUrl"])

                    user_msg = UserMessage(postdata["text"],
                                           out_channel,
                                           postdata["from"]["id"],
                                           input_channel=self.name())
                    await on_new_message(user_msg)
                else:
                    logger.info("Not received message type")
            except Exception as e:
                logger.error("Exception when trying to handle "
                             "message.{0}".format(e))
                logger.debug(e, exc_info=True)
                pass

            return response.text("success")