Example #1
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
Example #2
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
Example #3
0
 async def __send_bot_configuration(self, client_configuration, ws):
     tock_message_schema = TockMessageSchema()
     tock_message = TockMessage(bot_configuration=client_configuration)
     bot_configuration: str = tock_message_schema.dumps(tock_message)
     self.__logger.debug(f"bot configuration sent {bot_configuration}")
     await ws.send_str(bot_configuration)
Example #4
0
def given_tock_message() -> TockMessage:
    return TockMessage(request_id="request_id",
                       bot_request=given_bot_request(),
                       bot_response=given_bot_response())
Example #5
0
 def make_tockmessage(self, data, **kwargs):
     return TockMessage(**data)
Example #6
0
 def __send_bot_configuration(self, client_configuration):
     tock_message_schema = TockMessageSchema()
     tock_message = TockMessage(bot_configuration=client_configuration)
     bot_configuration: str = tock_message_schema.dumps(tock_message)
     self.__logger.debug(f"bot configuration sent {bot_configuration}")
     return web.Response(text=bot_configuration)