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

Async changes and sync improvements

This commit is contained in:
_run 2022-06-25 22:15:53 +05:00
parent 1686ce4f44
commit a2893945b2
2 changed files with 28 additions and 20 deletions

View File

@ -9,6 +9,7 @@ import time
import traceback
from typing import Any, Callable, List, Optional, Union
# these imports are used to avoid circular import error
import telebot.util
import telebot.types
@ -3977,7 +3978,7 @@ class TeleBot:
middlewares = [i for i in self.middlewares if update_type in i.update_types]
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.
@ -4021,10 +4022,11 @@ class TeleBot:
return
else:
if len(data) > len(params) -2:
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
if handler.get('pass_bot'): data["bot"] = self
handler["function"](message, **data)
except Exception as e:
@ -4035,6 +4037,9 @@ class TeleBot:
return self.exception_handler.handle(e)
logging.error(str(e))
return
# remove the bot from data
if "bot" in data:
del data["bot"]
if middlewares:
for middleware in middlewares:
middleware.post_process(message, data, handler_error)

View File

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