From b276bfacaff3ff5e32b210272cd79c1b0f1f4032 Mon Sep 17 00:00:00 2001 From: Badiboy Date: Wed, 13 Jul 2022 12:30:13 +0300 Subject: [PATCH 1/2] Fix exception with typed_middleware_handlers + some additional checks --- telebot/__init__.py | 29 +++++++++++++++++------------ 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/telebot/__init__.py b/telebot/__init__.py index c0df4f4..b1260c2 100644 --- a/telebot/__init__.py +++ b/telebot/__init__.py @@ -161,6 +161,7 @@ class TeleBot: } self.default_middleware_handlers = [] if apihelper.ENABLE_MIDDLEWARE and use_class_middlewares: + self.typed_middleware_handlers = {} logger.warning( 'You are using class based middlewares, but you have ' 'ENABLE_MIDDLEWARE set to True. This is not recommended.' @@ -595,16 +596,17 @@ class TeleBot: self._notify_command_handlers(self.chat_join_request_handlers, chat_join_request, 'chat_join_request') def process_middlewares(self, update): - for update_type, middlewares in self.typed_middleware_handlers.items(): - if getattr(update, update_type) is not None: - for typed_middleware_handler in middlewares: - try: - typed_middleware_handler(self, getattr(update, update_type)) - except Exception as e: - e.args = e.args + (f'Typed middleware handler "{typed_middleware_handler.__qualname__}"',) - raise + if self.typed_middleware_handlers: + for update_type, middlewares in self.typed_middleware_handlers.items(): + if getattr(update, update_type) is not None: + for typed_middleware_handler in middlewares: + try: + typed_middleware_handler(self, getattr(update, update_type)) + except Exception as e: + e.args = e.args + (f'Typed middleware handler "{typed_middleware_handler.__qualname__}"',) + raise - if len(self.default_middleware_handlers) > 0: + if self.default_middleware_handlers: for default_middleware_handler in self.default_middleware_handlers: try: default_middleware_handler(self, update) @@ -3170,10 +3172,13 @@ class TeleBot: if not apihelper.ENABLE_MIDDLEWARE: raise RuntimeError("Middleware is not enabled. Use apihelper.ENABLE_MIDDLEWARE before initialising TeleBot.") - if update_types: + added = False + if update_types and self.typed_middleware_handlers: for update_type in update_types: - self.typed_middleware_handlers[update_type].append(handler) - else: + if update_type in self.typed_middleware_handlers: + added = True + self.typed_middleware_handlers[update_type].append(handler) + if not added: self.default_middleware_handlers.append(handler) # function register_middleware_handler From a1bcd3c42ed7fa5a87b8fbce9c486508814aa781 Mon Sep 17 00:00:00 2001 From: Badiboy Date: Wed, 13 Jul 2022 13:10:16 +0300 Subject: [PATCH 2/2] use_class_middlewares checks added --- telebot/__init__.py | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/telebot/__init__.py b/telebot/__init__.py index b1260c2..b086cc9 100644 --- a/telebot/__init__.py +++ b/telebot/__init__.py @@ -161,10 +161,9 @@ class TeleBot: } self.default_middleware_handlers = [] if apihelper.ENABLE_MIDDLEWARE and use_class_middlewares: - self.typed_middleware_handlers = {} - logger.warning( - 'You are using class based middlewares, but you have ' - 'ENABLE_MIDDLEWARE set to True. This is not recommended.' + self.typed_middleware_handlers = None + logger.error( + 'You are using class based middlewares while having ENABLE_MIDDLEWARE set to True. This is not recommended.' ) self.middlewares = [] if use_class_middlewares else None self.threaded = threaded @@ -465,7 +464,7 @@ class TeleBot: new_chat_join_request = None for update in updates: - if apihelper.ENABLE_MIDDLEWARE: + if apihelper.ENABLE_MIDDLEWARE and not self.use_class_middlewares: try: self.process_middlewares(update) except Exception as e: @@ -3172,6 +3171,10 @@ class TeleBot: if not apihelper.ENABLE_MIDDLEWARE: raise RuntimeError("Middleware is not enabled. Use apihelper.ENABLE_MIDDLEWARE before initialising TeleBot.") + if self.use_class_middlewares: + logger.error("middleware_handler/register_middleware_handler/add_middleware_handler cannot be used with use_class_middlewares=True. Skipped.") + return + added = False if update_types and self.typed_middleware_handlers: for update_type in update_types: