Ejemplo n.º 1
0
def fake_message(ctx_api: API, **data: typing.Any) -> Message:
    return message_min(
        {
            "object": {
                "message": data,
                "client_info": data.get(
                    "client_info", EXAMPLE_EVENT["updates"][1]["object"]["client_info"]
                ),
            }
        },
        ctx_api,
    )
Ejemplo n.º 2
0
    async def handle_event(self, event: dict, ctx_api: "ABCAPI",
                           state_dispenser: "ABCStateDispenser") -> Any:

        logger.debug("Handling event ({}) with message view".format(
            event.get("event_id")))
        context_variables = {}
        message = message_min(event, ctx_api)
        message.state_peer = await state_dispenser.cast(
            self.get_state_key(event))

        for text_ax in self.default_text_approximators:
            message.text = text_ax(message)

        for middleware in self.middlewares:
            response = await middleware.pre(message)
            if response == MiddlewareResponse(False):
                return
            elif isinstance(response, dict):
                context_variables.update(response)

        handle_responses = []
        handlers = []

        for handler in self.handlers:
            result = await handler.filter(message)
            logger.debug("Handler {} returned {}".format(handler, result))

            if result is False:
                continue

            elif isinstance(result, dict):
                context_variables.update(result)

            handler_response = await handler.handle(message,
                                                    **context_variables)
            handle_responses.append(handler_response)
            handlers.append(handler)

            return_handler = self.handler_return_manager.get_handler(
                handler_response)
            if return_handler is not None:
                await return_handler(self.handler_return_manager,
                                     handler_response, message,
                                     context_variables)

            if handler.blocking:
                break

        for middleware in self.middlewares:
            await middleware.post(message, self, handle_responses, handlers)