mirror of
https://github.com/eternnoir/pyTelegramBotAPI.git
synced 2023-08-10 21:12:57 +03:00
Code simplify and sync/async unificatiion
This commit is contained in:
parent
3d97b08289
commit
b1a4136603
@ -5678,7 +5678,7 @@ class TeleBot:
|
|||||||
"""
|
"""
|
||||||
self.my_chat_member_handlers.append(handler_dict)
|
self.my_chat_member_handlers.append(handler_dict)
|
||||||
|
|
||||||
def register_my_chat_member_handler(self, callback: Callable, func: Optional[Callable]=None, pass_bot: Optional[Callable]=False, **kwargs):
|
def register_my_chat_member_handler(self, callback: Callable, func: Optional[Callable]=None, pass_bot: Optional[bool]=False, **kwargs):
|
||||||
"""
|
"""
|
||||||
Registers my chat member handler.
|
Registers my chat member handler.
|
||||||
|
|
||||||
@ -5898,7 +5898,8 @@ class TeleBot:
|
|||||||
data = {}
|
data = {}
|
||||||
params =[]
|
params =[]
|
||||||
handler_error = None
|
handler_error = None
|
||||||
skip_handler = False
|
skip_handlers = False
|
||||||
|
|
||||||
if middlewares:
|
if middlewares:
|
||||||
for middleware in middlewares:
|
for middleware in middlewares:
|
||||||
if middleware.update_sensitive:
|
if middleware.update_sensitive:
|
||||||
@ -5913,60 +5914,54 @@ class TeleBot:
|
|||||||
# Also, we will not run other middlewares
|
# Also, we will not run other middlewares
|
||||||
if isinstance(result, CancelUpdate):
|
if isinstance(result, CancelUpdate):
|
||||||
return
|
return
|
||||||
elif isinstance(result, SkipHandler) and skip_handler is False:
|
elif isinstance(result, SkipHandler):
|
||||||
skip_handler = True
|
skip_handlers = True
|
||||||
|
|
||||||
try:
|
if handlers and not(skip_handlers):
|
||||||
if handlers and not skip_handler:
|
try:
|
||||||
for handler in handlers:
|
for handler in handlers:
|
||||||
process_handler = self._test_message_handler(handler, message)
|
process_handler = self._test_message_handler(handler, message)
|
||||||
if not process_handler: continue
|
if not process_handler: continue
|
||||||
else:
|
for i in inspect.signature(handler['function']).parameters:
|
||||||
for i in inspect.signature(handler['function']).parameters:
|
params.append(i)
|
||||||
params.append(i)
|
if len(params) == 1:
|
||||||
if len(params) == 1:
|
handler['function'](message)
|
||||||
handler['function'](message)
|
elif "data" in params:
|
||||||
|
if len(params) == 2:
|
||||||
|
handler['function'](message, data)
|
||||||
|
elif len(params) == 3:
|
||||||
|
handler['function'](message, data=data, bot=self)
|
||||||
else:
|
else:
|
||||||
if "data" in params:
|
logger.error("It is not allowed to pass data and values inside data to the handler. Check your handler: {}".format(handler['function']))
|
||||||
if len(params) == 2:
|
return
|
||||||
handler['function'](message, data)
|
else:
|
||||||
elif len(params) == 3:
|
data_copy = data.copy()
|
||||||
handler['function'](message, data=data, bot=self)
|
for key in list(data_copy):
|
||||||
else:
|
# remove data from data_copy if handler does not accept it
|
||||||
logger.error("It is not allowed to pass data and values inside data to the handler. Check your handler: {}".format(handler['function']))
|
if key not in params:
|
||||||
return
|
del data_copy[key]
|
||||||
|
if handler.get('pass_bot'):
|
||||||
else:
|
data_copy["bot"] = self
|
||||||
|
if len(data_copy) > len(params) - 1: # remove the message parameter
|
||||||
data_copy = data.copy()
|
logger.error("You are passing more parameters than the handler needs. Check your handler: {}".format(handler['function']))
|
||||||
|
return
|
||||||
for key in list(data_copy):
|
handler["function"](message, **data_copy)
|
||||||
# remove data from data_copy if handler does not accept it
|
break
|
||||||
if key not in params:
|
except Exception as e:
|
||||||
del data_copy[key]
|
handler_error = e
|
||||||
if handler.get('pass_bot'): data_copy["bot"] = self
|
if self.exception_handler:
|
||||||
if len(data_copy) > len(params) - 1: # remove the message parameter
|
self.exception_handler.handle(e)
|
||||||
logger.error("You are passing more data than the handler needs. Check your handler: {}".format(handler['function']))
|
else: logging.error(str(e))
|
||||||
return
|
|
||||||
|
|
||||||
handler["function"](message, **data_copy)
|
|
||||||
break
|
|
||||||
except Exception as e:
|
|
||||||
handler_error = e
|
|
||||||
if self.exception_handler:
|
|
||||||
self.exception_handler.handle(e)
|
|
||||||
else: logging.error(str(e))
|
|
||||||
|
|
||||||
|
|
||||||
if middlewares:
|
if middlewares:
|
||||||
for middleware in middlewares:
|
for middleware in middlewares:
|
||||||
if middleware.update_sensitive:
|
if middleware.update_sensitive:
|
||||||
if hasattr(middleware, f'post_process_{update_type}'):
|
if hasattr(middleware, f'post_process_{update_type}'):
|
||||||
result = getattr(middleware, f'post_process_{update_type}')(message, data, handler_error)
|
getattr(middleware, f'post_process_{update_type}')(message, data, handler_error)
|
||||||
else:
|
else:
|
||||||
logger.error("Middleware: {} does not have post_process_{} method. Post process function was not executed.".format(middleware.__class__.__name__, update_type))
|
logger.error("Middleware: {} does not have post_process_{} method. Post process function was not executed.".format(middleware.__class__.__name__, update_type))
|
||||||
else:
|
else:
|
||||||
result = middleware.post_process(message, data, handler_error)
|
middleware.post_process(message, data, handler_error)
|
||||||
|
|
||||||
def _notify_command_handlers(self, handlers, new_messages, update_type):
|
def _notify_command_handlers(self, handlers, new_messages, update_type):
|
||||||
"""
|
"""
|
||||||
|
@ -359,8 +359,9 @@ class AsyncTeleBot:
|
|||||||
async def _run_middlewares_and_handlers(self, handlers, message, middlewares, update_type):
|
async def _run_middlewares_and_handlers(self, handlers, message, middlewares, update_type):
|
||||||
handler_error = None
|
handler_error = None
|
||||||
data = {}
|
data = {}
|
||||||
process_handler = True
|
skip_handlers = False
|
||||||
params = []
|
params = []
|
||||||
|
|
||||||
if middlewares:
|
if middlewares:
|
||||||
for middleware in middlewares:
|
for middleware in middlewares:
|
||||||
if middleware.update_sensitive:
|
if middleware.update_sensitive:
|
||||||
@ -371,60 +372,50 @@ class AsyncTeleBot:
|
|||||||
middleware_result = None
|
middleware_result = None
|
||||||
else:
|
else:
|
||||||
middleware_result = await middleware.pre_process(message, data)
|
middleware_result = await middleware.pre_process(message, data)
|
||||||
if isinstance(middleware_result, SkipHandler):
|
|
||||||
await middleware.post_process(message, data, handler_error)
|
|
||||||
process_handler = False
|
|
||||||
if isinstance(middleware_result, CancelUpdate):
|
if isinstance(middleware_result, CancelUpdate):
|
||||||
return
|
return
|
||||||
for handler in handlers:
|
elif isinstance(middleware_result, SkipHandler):
|
||||||
if not process_handler:
|
await middleware.post_process(message, data, handler_error)
|
||||||
break
|
skip_handlers = True
|
||||||
|
|
||||||
process_update = await self._test_message_handler(handler, message)
|
if handlers and not(skip_handlers):
|
||||||
if not process_update:
|
try:
|
||||||
continue
|
for handler in handlers:
|
||||||
elif process_update:
|
process_update = await self._test_message_handler(handler, message)
|
||||||
try:
|
if not process_update: continue
|
||||||
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
|
break
|
||||||
else:
|
elif "data" in params:
|
||||||
if "data" in params:
|
if len(params) == 2:
|
||||||
if len(params) == 2:
|
await handler['function'](message, data)
|
||||||
await handler['function'](message, data)
|
|
||||||
break
|
|
||||||
elif len(params) == 3:
|
|
||||||
await handler['function'](message, data=data, bot=self)
|
|
||||||
break
|
|
||||||
else:
|
|
||||||
logger.error("It is not allowed to pass data and values inside data to the handler. Check your handler: {}".format(handler['function']))
|
|
||||||
return
|
|
||||||
|
|
||||||
else:
|
|
||||||
|
|
||||||
data_copy = data.copy()
|
|
||||||
|
|
||||||
for key in list(data_copy):
|
|
||||||
# remove data from data_copy if handler does not accept it
|
|
||||||
if key not in params:
|
|
||||||
del data_copy[key]
|
|
||||||
if handler.get('pass_bot'): data_copy["bot"] = self
|
|
||||||
if len(data_copy) > 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
|
|
||||||
|
|
||||||
await handler["function"](message, **data_copy)
|
|
||||||
break
|
break
|
||||||
except Exception as e:
|
elif len(params) == 3:
|
||||||
handler_error = e
|
await handler['function'](message, data=data, bot=self)
|
||||||
|
break
|
||||||
if self.exception_handler:
|
else:
|
||||||
self.exception_handler.handle(e)
|
logger.error("It is not allowed to pass data and values inside data to the handler. Check your handler: {}".format(handler['function']))
|
||||||
else: logger.error(str(e))
|
return
|
||||||
break
|
else:
|
||||||
|
data_copy = data.copy()
|
||||||
|
for key in list(data_copy):
|
||||||
|
# remove data from data_copy if handler does not accept it
|
||||||
|
if key not in params:
|
||||||
|
del data_copy[key]
|
||||||
|
if handler.get('pass_bot'):
|
||||||
|
data_copy["bot"] = self
|
||||||
|
if len(data_copy) > 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
|
||||||
|
await handler["function"](message, **data_copy)
|
||||||
|
break
|
||||||
|
except Exception as e:
|
||||||
|
if self.exception_handler:
|
||||||
|
self.exception_handler.handle(e)
|
||||||
|
else:
|
||||||
|
logger.error(str(e))
|
||||||
|
|
||||||
if middlewares:
|
if middlewares:
|
||||||
for middleware in middlewares:
|
for middleware in middlewares:
|
||||||
@ -1619,7 +1610,7 @@ class AsyncTeleBot:
|
|||||||
"""
|
"""
|
||||||
self.my_chat_member_handlers.append(handler_dict)
|
self.my_chat_member_handlers.append(handler_dict)
|
||||||
|
|
||||||
def register_my_chat_member_handler(self, callback: Awaitable, func: Optional[Callable]=None, pass_bot: Optional[Callable]=False, **kwargs):
|
def register_my_chat_member_handler(self, callback: Awaitable, func: Optional[Callable]=None, pass_bot: Optional[bool]=False, **kwargs):
|
||||||
"""
|
"""
|
||||||
Registers my chat member handler.
|
Registers my chat member handler.
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user