Skip to content

Commit 30aaf8d

Browse files
authored
Support ContinueHandling
1 parent 82ad37f commit 30aaf8d

File tree

3 files changed

+24
-22
lines changed

3 files changed

+24
-22
lines changed

telebot/__init__.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,10 @@
3737
logger.setLevel(logging.ERROR)
3838

3939
from telebot import apihelper, util, types
40-
from telebot.handler_backends import HandlerBackend, MemoryHandlerBackend, FileHandlerBackend, BaseMiddleware, CancelUpdate, SkipHandler, State
40+
from telebot.handler_backends import (
41+
HandlerBackend, MemoryHandlerBackend, FileHandlerBackend, BaseMiddleware,
42+
CancelUpdate, SkipHandler, State, ContinueHandling
43+
)
4144
from telebot.custom_filters import SimpleCustomFilter, AdvancedCustomFilter
4245

4346

@@ -6111,13 +6114,14 @@ def _run_middlewares_and_handler(self, message, handlers, middlewares, update_ty
61116114
if not process_handler: continue
61126115
for i in inspect.signature(handler['function']).parameters:
61136116
params.append(i)
6117+
result = None
61146118
if len(params) == 1:
6115-
handler['function'](message)
6119+
result = handler['function'](message)
61166120
elif "data" in params:
61176121
if len(params) == 2:
6118-
handler['function'](message, data)
6122+
result = handler['function'](message, data)
61196123
elif len(params) == 3:
6120-
handler['function'](message, data=data, bot=self)
6124+
result = handler['function'](message, data=data, bot=self)
61216125
else:
61226126
logger.error("It is not allowed to pass data and values inside data to the handler. Check your handler: {}".format(handler['function']))
61236127
return
@@ -6132,8 +6136,9 @@ def _run_middlewares_and_handler(self, message, handlers, middlewares, update_ty
61326136
if len(data_copy) > len(params) - 1: # remove the message parameter
61336137
logger.error("You are passing more parameters than the handler needs. Check your handler: {}".format(handler['function']))
61346138
return
6135-
handler["function"](message, **data_copy)
6136-
break
6139+
result = handler["function"](message, **data_copy)
6140+
if not isinstance(result, ContinueHandling):
6141+
break
61376142
except Exception as e:
61386143
handler_error = e
61396144
if self.exception_handler:

telebot/async_telebot.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
# storages
1616
from telebot.asyncio_storage import StateMemoryStorage, StatePickleStorage, StateStorageBase
17-
from telebot.asyncio_handler_backends import BaseMiddleware, CancelUpdate, SkipHandler, State
17+
from telebot.asyncio_handler_backends import BaseMiddleware, CancelUpdate, SkipHandler, State, ContinueHandling
1818

1919
from inspect import signature
2020

@@ -493,16 +493,14 @@ async def _run_middlewares_and_handlers(self, message, handlers, middlewares, up
493493
if not process_update: continue
494494
for i in signature(handler['function']).parameters:
495495
params.append(i)
496+
result = None
496497
if len(params) == 1:
497-
await handler['function'](message)
498-
break
498+
result = await handler['function'](message)
499499
elif "data" in params:
500500
if len(params) == 2:
501-
await handler['function'](message, data)
502-
break
501+
result = await handler['function'](message, data)
503502
elif len(params) == 3:
504-
await handler['function'](message, data=data, bot=self)
505-
break
503+
result = await handler['function'](message, data=data, bot=self)
506504
else:
507505
logger.error("It is not allowed to pass data and values inside data to the handler. Check your handler: {}".format(handler['function']))
508506
return
@@ -517,7 +515,8 @@ async def _run_middlewares_and_handlers(self, message, handlers, middlewares, up
517515
if len(data_copy) > len(params) - 1: # remove the message parameter
518516
logger.error("You are passing more data than the handler needs. Check your handler: {}".format(handler['function']))
519517
return
520-
await handler["function"](message, **data_copy)
518+
result = await handler["function"](message, **data_copy)
519+
if not isinstance(result, ContinueHandling):
521520
break
522521
except Exception as e:
523522
if self.exception_handler:

telebot/handler_backends.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,6 @@ def __init__(self) -> None:
174174
def __str__(self) -> str:
175175
return self.name
176176

177-
178177

179178
class StatesGroup:
180179
"""
@@ -192,9 +191,6 @@ def __init_subclass__(cls) -> None:
192191
value.name = ':'.join((cls.__name__, name))
193192
value.group = cls
194193

195-
196-
197-
198194

199195
class BaseMiddleware:
200196
"""
@@ -254,8 +250,6 @@ class SkipHandler:
254250
but will skip execution of handler.
255251
"""
256252

257-
def __init__(self) -> None:
258-
pass
259253

260254
class CancelUpdate:
261255
"""
@@ -266,5 +260,9 @@ class CancelUpdate:
266260
of post_process in middlewares.
267261
"""
268262

269-
def __init__(self) -> None:
270-
pass
263+
class ContinueHandling:
264+
"""
265+
Class for continue updates in handlers.
266+
Just return instance of this class
267+
in handlers to continue process.
268+
"""

0 commit comments

Comments
 (0)