mirror of
https://github.com/eternnoir/pyTelegramBotAPI.git
synced 2023-08-10 21:12:57 +03:00
Compare commits
6 Commits
726b203724
...
bf415e4bd7
Author | SHA1 | Date | |
---|---|---|---|
|
bf415e4bd7 | ||
|
2fcfdc2584 | ||
|
659501efef | ||
|
92654ee970 | ||
|
a1bcd3c42e | ||
|
b276bfacaf |
|
@ -161,9 +161,9 @@ 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:
|
||||||
logger.warning(
|
self.typed_middleware_handlers = None
|
||||||
'You are using class based middlewares, but you have '
|
logger.error(
|
||||||
'ENABLE_MIDDLEWARE set to True. This is not recommended.'
|
'You are using class based middlewares while having ENABLE_MIDDLEWARE set to True. This is not recommended.'
|
||||||
)
|
)
|
||||||
self.middlewares = [] if use_class_middlewares else None
|
self.middlewares = [] if use_class_middlewares else None
|
||||||
self.threaded = threaded
|
self.threaded = threaded
|
||||||
|
@ -464,7 +464,7 @@ class TeleBot:
|
||||||
new_chat_join_request = None
|
new_chat_join_request = None
|
||||||
|
|
||||||
for update in updates:
|
for update in updates:
|
||||||
if apihelper.ENABLE_MIDDLEWARE:
|
if apihelper.ENABLE_MIDDLEWARE and not self.use_class_middlewares:
|
||||||
try:
|
try:
|
||||||
self.process_middlewares(update)
|
self.process_middlewares(update)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
|
@ -595,6 +595,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 +605,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 +3171,17 @@ 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:
|
if self.use_class_middlewares:
|
||||||
|
logger.error("middleware_handler/register_middleware_handler/add_middleware_handler cannot be used with use_class_middlewares=True. Skipped.")
|
||||||
|
return
|
||||||
|
|
||||||
|
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
|
||||||
|
|
|
@ -141,7 +141,7 @@ class AsyncTeleBot:
|
||||||
return [types.Update.de_json(ju) for ju in json_updates]
|
return [types.Update.de_json(ju) for ju in json_updates]
|
||||||
|
|
||||||
async def polling(self, non_stop: bool=False, skip_pending=False, interval: int=0, timeout: int=20,
|
async def polling(self, non_stop: bool=False, skip_pending=False, interval: int=0, timeout: int=20,
|
||||||
request_timeout: int=20, allowed_updates: Optional[List[str]]=None,
|
request_timeout: int=None, allowed_updates: Optional[List[str]]=None,
|
||||||
none_stop: Optional[bool]=None):
|
none_stop: Optional[bool]=None):
|
||||||
"""
|
"""
|
||||||
This allows the bot to retrieve Updates automatically and notify listeners and message handlers accordingly.
|
This allows the bot to retrieve Updates automatically and notify listeners and message handlers accordingly.
|
||||||
|
@ -174,7 +174,7 @@ class AsyncTeleBot:
|
||||||
await self.skip_updates()
|
await self.skip_updates()
|
||||||
await self._process_polling(non_stop, interval, timeout, request_timeout, allowed_updates)
|
await self._process_polling(non_stop, interval, timeout, request_timeout, allowed_updates)
|
||||||
|
|
||||||
async def infinity_polling(self, timeout: int=20, skip_pending: bool=False, request_timeout: int=20, logger_level=logging.ERROR,
|
async def infinity_polling(self, timeout: int=20, skip_pending: bool=False, request_timeout: int=None, logger_level=logging.ERROR,
|
||||||
allowed_updates: Optional[List[str]]=None, *args, **kwargs):
|
allowed_updates: Optional[List[str]]=None, *args, **kwargs):
|
||||||
"""
|
"""
|
||||||
Wrap polling with infinite loop and exception handling to avoid bot stops polling.
|
Wrap polling with infinite loop and exception handling to avoid bot stops polling.
|
||||||
|
@ -213,7 +213,7 @@ class AsyncTeleBot:
|
||||||
logger.error("Break infinity polling")
|
logger.error("Break infinity polling")
|
||||||
|
|
||||||
async def _process_polling(self, non_stop: bool=False, interval: int=0, timeout: int=20,
|
async def _process_polling(self, non_stop: bool=False, interval: int=0, timeout: int=20,
|
||||||
request_timeout: int=20, allowed_updates: Optional[List[str]]=None):
|
request_timeout: int=None, allowed_updates: Optional[List[str]]=None):
|
||||||
"""
|
"""
|
||||||
Function to process polling.
|
Function to process polling.
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user