Esempio n. 1
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")))

        handler_basement = self.handlers[GroupEventType(event["type"])]
        context_variables = {}

        event_model = handler_basement.dataclass(**event)

        if isinstance(event_model, dict):
            event_model["ctx_api"] = ctx_api
        else:
            setattr(event_model, "unprepared_ctx_api", ctx_api)

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

        result = await handler_basement.handler.filter(event_model)
        logger.debug("Handler {} returned {}".format(handler_basement.handler,
                                                     result))

        if result is False:
            return

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

        handler_response = await handler_basement.handler.handle(
            event_model, **context_variables)

        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,
                event_model,
                context_variables,
            )

        for middleware in self.middlewares:
            await middleware.post(event_model, self, [handler_response],
                                  [handler_basement.handler])
Esempio n. 2
0
        def decorator(func):
            for e in event:

                if isinstance(e, str):
                    e = GroupEventType(e)

                self.raw_event_view.handlers[e] = HandlerBasement(
                    dataclass,
                    FromFuncHandler(
                        func,
                        *map(convert_shorten_filter, rules),
                        *self.auto_rules,
                        *self.get_custom_rules(custom_rules),
                    ),
                )
            return func
Esempio n. 3
0
 def decorator(func):
     for e in event:
         if isinstance(e, str):
             e = GroupEventType(e)
         handler_basement = BotHandlerBasement(
             dataclass,
             FromFuncHandler(
                 func,
                 *rules,
                 *self.auto_rules,
                 *self.get_custom_rules(custom_rules),
                 blocking=blocking,
             ),
         )
         event_handlers = self.raw_event_view.handlers.setdefault(e, [])
         event_handlers.append(handler_basement)
     return func
Esempio n. 4
0
 async def process_event(self, event: dict) -> bool:
     if GroupEventType(event["type"]) in self.handlers:
         return True
Esempio n. 5
0
 async def process_event(self, event: dict) -> bool:
     return GroupEventType(event["type"]) == GroupEventType.MESSAGE_NEW
Esempio n. 6
0
 async def process_event(self, event: dict) -> bool:
     return GroupEventType(
         self.get_event_type(event)) == GroupEventType.MESSAGE_NEW
Esempio n. 7
0
 async def process_event(self, event: dict) -> bool:
     return GroupEventType(self.get_event_type(event)) in self.handlers
Esempio n. 8
0
 def get_handler_basements(self, event: dict) -> List["BotHandlerBasement"]:
     return self.handlers[GroupEventType(self.get_event_type(event))]