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,21 +5914,19 @@ 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
|
||||||
|
|
||||||
|
if handlers and not(skip_handlers):
|
||||||
try:
|
try:
|
||||||
if handlers and not skip_handler:
|
|
||||||
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)
|
||||||
else:
|
elif "data" in params:
|
||||||
if "data" in params:
|
|
||||||
if len(params) == 2:
|
if len(params) == 2:
|
||||||
handler['function'](message, data)
|
handler['function'](message, data)
|
||||||
elif len(params) == 3:
|
elif len(params) == 3:
|
||||||
@ -5935,20 +5934,17 @@ class TeleBot:
|
|||||||
else:
|
else:
|
||||||
logger.error("It is not allowed to pass data and values inside data to the handler. Check your handler: {}".format(handler['function']))
|
logger.error("It is not allowed to pass data and values inside data to the handler. Check your handler: {}".format(handler['function']))
|
||||||
return
|
return
|
||||||
|
|
||||||
else:
|
else:
|
||||||
|
|
||||||
data_copy = data.copy()
|
data_copy = data.copy()
|
||||||
|
|
||||||
for key in list(data_copy):
|
for key in list(data_copy):
|
||||||
# remove data from data_copy if handler does not accept it
|
# remove data from data_copy if handler does not accept it
|
||||||
if key not in params:
|
if key not in params:
|
||||||
del data_copy[key]
|
del data_copy[key]
|
||||||
if handler.get('pass_bot'): data_copy["bot"] = self
|
if handler.get('pass_bot'):
|
||||||
|
data_copy["bot"] = self
|
||||||
if len(data_copy) > len(params) - 1: # remove the message parameter
|
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']))
|
logger.error("You are passing more parameters than the handler needs. Check your handler: {}".format(handler['function']))
|
||||||
return
|
return
|
||||||
|
|
||||||
handler["function"](message, **data_copy)
|
handler["function"](message, **data_copy)
|
||||||
break
|
break
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
@ -5957,16 +5953,15 @@ class TeleBot:
|
|||||||
self.exception_handler.handle(e)
|
self.exception_handler.handle(e)
|
||||||
else: logging.error(str(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,27 +372,23 @@ 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:
|
|
||||||
continue
|
|
||||||
elif process_update:
|
|
||||||
try:
|
try:
|
||||||
|
for handler in handlers:
|
||||||
|
process_update = await self._test_message_handler(handler, message)
|
||||||
|
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
|
break
|
||||||
@ -401,30 +398,24 @@ class AsyncTeleBot:
|
|||||||
else:
|
else:
|
||||||
logger.error("It is not allowed to pass data and values inside data to the handler. Check your handler: {}".format(handler['function']))
|
logger.error("It is not allowed to pass data and values inside data to the handler. Check your handler: {}".format(handler['function']))
|
||||||
return
|
return
|
||||||
|
|
||||||
else:
|
else:
|
||||||
|
|
||||||
data_copy = data.copy()
|
data_copy = data.copy()
|
||||||
|
|
||||||
for key in list(data_copy):
|
for key in list(data_copy):
|
||||||
# remove data from data_copy if handler does not accept it
|
# remove data from data_copy if handler does not accept it
|
||||||
if key not in params:
|
if key not in params:
|
||||||
del data_copy[key]
|
del data_copy[key]
|
||||||
if handler.get('pass_bot'): data_copy["bot"] = self
|
if handler.get('pass_bot'):
|
||||||
|
data_copy["bot"] = self
|
||||||
if len(data_copy) > len(params) - 1: # remove the message parameter
|
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']))
|
logger.error("You are passing more data than the handler needs. Check your handler: {}".format(handler['function']))
|
||||||
return
|
return
|
||||||
|
|
||||||
await handler["function"](message, **data_copy)
|
await handler["function"](message, **data_copy)
|
||||||
break
|
break
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
handler_error = e
|
|
||||||
|
|
||||||
if self.exception_handler:
|
if self.exception_handler:
|
||||||
self.exception_handler.handle(e)
|
self.exception_handler.handle(e)
|
||||||
else: logger.error(str(e))
|
else:
|
||||||
break
|
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