def __init__(self): super().__init__() self.state_source_key = DEFAULT_STATE_KEY self.handlers: List["ABCHandler"] = [] self.middlewares: Set[Type["BaseMiddleware"]] = [] self.default_text_approximators: List[Callable[[MessageMin], str]] = [] self.handler_return_manager = BotMessageReturnHandler() self.middleware_instances = []
class MessageView(ABCView): def __init__(self): self.handlers: List["ABCHandler"] = [] self.middlewares: List["BaseMiddleware"] = [] self.default_text_approximators: List[Callable[[MessageMin], str]] = [] self.handler_return_manager = BotMessageReturnHandler() async def process_event(self, event: dict) -> bool: if GroupEventType(event["type"]) == GroupEventType.MESSAGE_NEW: return True 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(message.peer_id) 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)
class ABCMessageView(ABCDispenseView, ABC): def __init__(self): super().__init__() self.state_source_key = DEFAULT_STATE_KEY self.handlers: List["ABCHandler"] = [] self.middlewares: Set[Type["BaseMiddleware"]] = [] self.default_text_approximators: List[Callable[[MessageMin], str]] = [] self.handler_return_manager = BotMessageReturnHandler() self.middleware_instances = [] async def process_event(self, event: dict) -> bool: return GroupEventType(event["type"]) == GroupEventType.MESSAGE_NEW 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: dict = {} 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) if await self.pre_middleware(message, context_variables) is False: return logger.info( "Handling stopped, pre_middleware returned error") 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 await self.post_middleware(self, handle_responses, handlers)
class RawEventView(ABCView): def __init__(self): self.handlers: Dict[GroupEventType, HandlerBasement] = {} self.middlewares: List["BaseMiddleware"] = [] self.handler_return_manager = BotMessageReturnHandler() async def process_event(self, event: dict) -> bool: if GroupEventType(event["type"]) in self.handlers: return True 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])
def __init__(self): self.handlers: Dict[GroupEventType, HandlerBasement] = {} self.middlewares: List["BaseMiddleware"] = [] self.handler_return_manager = BotMessageReturnHandler()
def __init__(self): super().__init__() self.handler_return_manager = BotMessageReturnHandler()
def __init__(self): super().__init__() self.handlers: Dict[GroupEventType, HandlerBasement] = {} self.middlewares: Set[Type["BaseMiddleware"]] = [] self.handler_return_manager = BotMessageReturnHandler() self.middleware_instances = []
def __init__(self): self.handlers: List["ABCHandler"] = [] self.middlewares: List["BaseMiddleware"] = [] self.default_text_approximators: List[Callable[[MessageMin], str]] = [] self.handler_return_manager = BotMessageReturnHandler()