1
0
mirror of https://github.com/eternnoir/pyTelegramBotAPI.git synced 2023-08-10 21:12:57 +03:00

Merge pull request #1587 from coder2020official/master

Middleware update: everything in data will be passed to handler if ne…
This commit is contained in:
Badiboy 2022-06-26 12:09:38 +03:00 committed by GitHub
commit 752f35614c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 42 additions and 34 deletions

View File

@ -9,6 +9,7 @@ import time
import traceback import traceback
from typing import Any, Callable, List, Optional, Union from typing import Any, Callable, List, Optional, Union
# these imports are used to avoid circular import error # these imports are used to avoid circular import error
import telebot.util import telebot.util
import telebot.types import telebot.types
@ -3977,7 +3978,7 @@ class TeleBot:
middlewares = [i for i in self.middlewares if update_type in i.update_types] middlewares = [i for i in self.middlewares if update_type in i.update_types]
return middlewares return middlewares
def _run_middlewares_and_handler(self, message, handlers, middlewares, *args, **kwargs): def _run_middlewares_and_handler(self, message, handlers, middlewares):
""" """
This class is made to run handler and middleware in queue. This class is made to run handler and middleware in queue.
@ -3999,6 +4000,7 @@ class TeleBot:
elif isinstance(result, SkipHandler) and skip_handler is False: elif isinstance(result, SkipHandler) and skip_handler is False:
skip_handler = True skip_handler = True
try: try:
if handlers and not skip_handler: if handlers and not skip_handler:
for handler in handlers: for handler in handlers:
@ -4009,23 +4011,23 @@ class TeleBot:
params.append(i) params.append(i)
if len(params) == 1: if len(params) == 1:
handler['function'](message) handler['function'](message)
else:
elif len(params) == 2: if "data" in params:
if handler.get('pass_bot') is True: if len(params) == 2:
handler['function'](message, self)
elif handler.get('pass_bot') is False:
handler['function'](message, data) handler['function'](message, data)
elif len(params) == 3: elif len(params) == 3:
if params[2] == 'bot' and handler.get('pass_bot') is True: handler['function'](message, data=data, bot=self)
handler['function'](message, data, self) else:
logger.error("It is not allowed to pass data and values inside data to the handler. Check your handler: {}".format(handler['function']))
elif not handler.get('pass_bot'): return
raise RuntimeError('Your handler accepts 3 parameters but pass_bot is False. Please re-check your handler.')
else: else:
handler['function'](message, self, data) if handler.get('pass_bot'): data["bot"] = self
if len(data) > len(params) - 1: # remove the message parameter
logger.error("You are passing more data than the handler needs. Check your handler: {}".format(handler['function']))
return
handler["function"](message, **data)
except Exception as e: except Exception as e:
handler_error = e handler_error = e
@ -4035,6 +4037,9 @@ class TeleBot:
return self.exception_handler.handle(e) return self.exception_handler.handle(e)
logging.error(str(e)) logging.error(str(e))
return return
# remove the bot from data
if "bot" in data:
del data["bot"]
if middlewares: if middlewares:
for middleware in middlewares: for middleware in middlewares:
middleware.post_process(message, data, handler_error) middleware.post_process(message, data, handler_error)

View File

@ -277,7 +277,7 @@ class AsyncTeleBot:
handler_error = None handler_error = None
data = {} data = {}
process_handler = True process_handler = True
params = []
if middlewares: if middlewares:
for middleware in middlewares: for middleware in middlewares:
middleware_result = await middleware.pre_process(message, data) middleware_result = await middleware.pre_process(message, data)
@ -295,27 +295,27 @@ class AsyncTeleBot:
continue continue
elif process_update: elif process_update:
try: try:
params = []
for i in signature(handler['function']).parameters: for i in signature(handler['function']).parameters:
params.append(i) params.append(i)
if len(params) == 1: if len(params) == 1:
await handler['function'](message) await handler['function'](message)
break
elif len(params) == 2:
if handler['pass_bot']:
await handler['function'](message, self)
break
else: else:
if "data" in params:
if len(params) == 2:
await handler['function'](message, data) await handler['function'](message, data)
break
elif len(params) == 3: elif len(params) == 3:
if handler['pass_bot'] and params[1] == 'bot': await handler['function'](message, data=data, bot=self)
await handler['function'](message, self, data)
break
else: else:
await handler['function'](message, data) logger.error("It is not allowed to pass data and values inside data to the handler. Check your handler: {}".format(handler['function']))
break return
else:
if handler.get('pass_bot'): data["bot"] = self
if len(data) > len(params) - 1:
logger.error("You are passing more data than the handler needs. Check your handler: {}".format(handler['function']))
return
await handler["function"](message, **data)
except Exception as e: except Exception as e:
handler_error = e handler_error = e
@ -324,6 +324,9 @@ class AsyncTeleBot:
return self.exception_handler.handle(e) return self.exception_handler.handle(e)
logging.error(str(e)) logging.error(str(e))
return return
# remove the bot from data
if "bot" in data:
del data["bot"]
if middlewares: if middlewares:
for middleware in middlewares: for middleware in middlewares: