mirror of
https://github.com/eternnoir/pyTelegramBotAPI.git
synced 2023-08-10 21:12:57 +03:00
Fix exception with typed_middleware_handlers
+ some additional checks
This commit is contained in:
parent
726b203724
commit
b276bfacaf
@ -161,6 +161,7 @@ class TeleBot:
|
|||||||
}
|
}
|
||||||
self.default_middleware_handlers = []
|
self.default_middleware_handlers = []
|
||||||
if apihelper.ENABLE_MIDDLEWARE and use_class_middlewares:
|
if apihelper.ENABLE_MIDDLEWARE and use_class_middlewares:
|
||||||
|
self.typed_middleware_handlers = {}
|
||||||
logger.warning(
|
logger.warning(
|
||||||
'You are using class based middlewares, but you have '
|
'You are using class based middlewares, but you have '
|
||||||
'ENABLE_MIDDLEWARE set to True. This is not recommended.'
|
'ENABLE_MIDDLEWARE set to True. This is not recommended.'
|
||||||
@ -595,6 +596,7 @@ class TeleBot:
|
|||||||
self._notify_command_handlers(self.chat_join_request_handlers, chat_join_request, 'chat_join_request')
|
self._notify_command_handlers(self.chat_join_request_handlers, chat_join_request, 'chat_join_request')
|
||||||
|
|
||||||
def process_middlewares(self, update):
|
def process_middlewares(self, update):
|
||||||
|
if self.typed_middleware_handlers:
|
||||||
for update_type, middlewares in self.typed_middleware_handlers.items():
|
for update_type, middlewares in self.typed_middleware_handlers.items():
|
||||||
if getattr(update, update_type) is not None:
|
if getattr(update, update_type) is not None:
|
||||||
for typed_middleware_handler in middlewares:
|
for typed_middleware_handler in middlewares:
|
||||||
@ -604,7 +606,7 @@ class TeleBot:
|
|||||||
e.args = e.args + (f'Typed middleware handler "{typed_middleware_handler.__qualname__}"',)
|
e.args = e.args + (f'Typed middleware handler "{typed_middleware_handler.__qualname__}"',)
|
||||||
raise
|
raise
|
||||||
|
|
||||||
if len(self.default_middleware_handlers) > 0:
|
if self.default_middleware_handlers:
|
||||||
for default_middleware_handler in self.default_middleware_handlers:
|
for default_middleware_handler in self.default_middleware_handlers:
|
||||||
try:
|
try:
|
||||||
default_middleware_handler(self, update)
|
default_middleware_handler(self, update)
|
||||||
@ -3170,10 +3172,13 @@ class TeleBot:
|
|||||||
if not apihelper.ENABLE_MIDDLEWARE:
|
if not apihelper.ENABLE_MIDDLEWARE:
|
||||||
raise RuntimeError("Middleware is not enabled. Use apihelper.ENABLE_MIDDLEWARE before initialising TeleBot.")
|
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:
|
for update_type in update_types:
|
||||||
|
if update_type in self.typed_middleware_handlers:
|
||||||
|
added = True
|
||||||
self.typed_middleware_handlers[update_type].append(handler)
|
self.typed_middleware_handlers[update_type].append(handler)
|
||||||
else:
|
if not added:
|
||||||
self.default_middleware_handlers.append(handler)
|
self.default_middleware_handlers.append(handler)
|
||||||
|
|
||||||
# function register_middleware_handler
|
# function register_middleware_handler
|
||||||
|
Loading…
Reference in New Issue
Block a user