Ejemplo n.º 1
0
def given_bot_response() -> BotResponse:
    return BotResponse(
        messages=[given_sentence(),
                  given_card(),
                  given_carousel()],
        story_id="story_id",
        entities=[],
        context=ResponseContext(request_id="request_id",
                                date=datetime(2020, 1, 1, 0, 0, 0)),
        step="step")
Ejemplo n.º 2
0
    def __bot_handler(self, tock_message: TockMessage) -> TockMessage:
        self.__logger.debug(f"receive tock_message {tock_message}")
        messages: List[BotMessage] = []
        request: BotRequest = tock_message.bot_request
        current_user_id: UserId = request.context.user_id

        session = self.__bot_storage.get_session(current_user_id)
        session.entities = request.entities

        story_type: Type[Story] = self.__story_definitions.find_story(
            request.story_id)
        session.previous_intent = Intent(request.intent)

        bus = self.__bus(session=session,
                         send=lambda bot_message: messages.append(bot_message),
                         request=request)

        if story_type is not None:
            story_name: str = story_type.configuration().name
            self.__logger.info("story found %s for intent %s", story_name,
                               request.intent)
            session.current_story = story_name
        else:
            self.__logger.info("No story for intent %s", request.intent)
            story_type = unknown()

        story_instance: Story = self.__create(story_type, bus)

        try:
            story_instance.answer(bus)
        except:
            self.__logger.exception("Unexpected error")

        response = TockMessage(
            bot_response=BotResponse(
                messages=messages,
                story_id=story_instance.configuration().name,
                step=None,
                context=ResponseContext(request_id=tock_message.request_id,
                                        date=datetime.now()),
                entities=request.entities),
            request_id=tock_message.request_id,
        )
        self.__logger.debug(f"send tock_message {response}")
        self.__bot_storage.save(session)
        return response
Ejemplo n.º 3
0
    def __bot_handler(self, tock_message: TockMessage) -> str:
        messages: List[BotMessage] = []
        request: BotRequest = tock_message.bot_request

        story_class: Type[Story] = self.__stories.find_story(
            Intent(request.intent), self.__context.current_story)

        self.__context.entities = self.__context.entities + request.entities
        self.__context.current_story = story_class
        bus = self.__bus(context=self.__context,
                         send=lambda bot_message: messages.append(bot_message),
                         request=request)

        if story_class is not None:
            self.__logger.info("story found %s for intent %s",
                               story_class.__name__, request.intent)
            story = self.__create(story_class, bus)
        else:
            self.__logger.info("No story for intent %s", request.intent)
            story = ErrorStory(request=request, answer=self.__error_handler)

        try:
            story.answer(bus)
        except:
            self.__logger.exception("Unexpected error")

        response = TockMessage(
            bot_response=BotResponse(messages=messages,
                                     story_id="story_id",
                                     step=None,
                                     context=ResponseContext(
                                         request_id=tock_message.request_id,
                                         date=datetime.now()),
                                     entities=[]),
            request_id=tock_message.request_id,
        )
        tock_response: str = TockMessageSchema().dumps(response)
        return tock_response
Ejemplo n.º 4
0
def given_response_context() -> ResponseContext:
    return ResponseContext(request_id="request_id",
                           date=datetime(2020, 1, 1, 0, 0, 0))
Ejemplo n.º 5
0
 def make_response_context(self, data, **kwargs):
     return ResponseContext(**data)