From 30aaf8d0f10377978dce33fa6e050dc717121eb2 Mon Sep 17 00:00:00 2001 From: byehack Date: Sun, 2 Oct 2022 03:27:06 +0330 Subject: [PATCH] Support ContinueHandling --- telebot/__init__.py | 17 +++++++++++------ telebot/async_telebot.py | 15 +++++++-------- telebot/handler_backends.py | 14 ++++++-------- 3 files changed, 24 insertions(+), 22 deletions(-) diff --git a/telebot/__init__.py b/telebot/__init__.py index 12ecf73..72d244c 100644 --- a/telebot/__init__.py +++ b/telebot/__init__.py @@ -37,7 +37,10 @@ logger.addHandler(console_output_handler) logger.setLevel(logging.ERROR) from telebot import apihelper, util, types -from telebot.handler_backends import HandlerBackend, MemoryHandlerBackend, FileHandlerBackend, BaseMiddleware, CancelUpdate, SkipHandler, State +from telebot.handler_backends import ( + HandlerBackend, MemoryHandlerBackend, FileHandlerBackend, BaseMiddleware, + CancelUpdate, SkipHandler, State, ContinueHandling +) from telebot.custom_filters import SimpleCustomFilter, AdvancedCustomFilter @@ -6111,13 +6114,14 @@ class TeleBot: if not process_handler: continue for i in inspect.signature(handler['function']).parameters: params.append(i) + result = None if len(params) == 1: - handler['function'](message) + result = handler['function'](message) elif "data" in params: if len(params) == 2: - handler['function'](message, data) + result = handler['function'](message, data) elif len(params) == 3: - handler['function'](message, data=data, bot=self) + result = handler['function'](message, data=data, bot=self) else: logger.error("It is not allowed to pass data and values inside data to the handler. Check your handler: {}".format(handler['function'])) return @@ -6132,8 +6136,9 @@ class TeleBot: if len(data_copy) > len(params) - 1: # remove the message parameter logger.error("You are passing more parameters than the handler needs. Check your handler: {}".format(handler['function'])) return - handler["function"](message, **data_copy) - break + result = handler["function"](message, **data_copy) + if not isinstance(result, ContinueHandling): + break except Exception as e: handler_error = e if self.exception_handler: diff --git a/telebot/async_telebot.py b/telebot/async_telebot.py index 0aa1097..d2552b7 100644 --- a/telebot/async_telebot.py +++ b/telebot/async_telebot.py @@ -14,7 +14,7 @@ import telebot.types # storages from telebot.asyncio_storage import StateMemoryStorage, StatePickleStorage, StateStorageBase -from telebot.asyncio_handler_backends import BaseMiddleware, CancelUpdate, SkipHandler, State +from telebot.asyncio_handler_backends import BaseMiddleware, CancelUpdate, SkipHandler, State, ContinueHandling from inspect import signature @@ -493,16 +493,14 @@ class AsyncTeleBot: if not process_update: continue for i in signature(handler['function']).parameters: params.append(i) + result = None if len(params) == 1: - await handler['function'](message) - break + result = await handler['function'](message) elif "data" in params: if len(params) == 2: - await handler['function'](message, data) - break + result = await handler['function'](message, data) elif len(params) == 3: - await handler['function'](message, data=data, bot=self) - break + result = await handler['function'](message, data=data, bot=self) else: logger.error("It is not allowed to pass data and values inside data to the handler. Check your handler: {}".format(handler['function'])) return @@ -517,7 +515,8 @@ class AsyncTeleBot: 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) + result = await handler["function"](message, **data_copy) + if not isinstance(result, ContinueHandling): break except Exception as e: if self.exception_handler: diff --git a/telebot/handler_backends.py b/telebot/handler_backends.py index 42c5804..70ab67d 100644 --- a/telebot/handler_backends.py +++ b/telebot/handler_backends.py @@ -174,7 +174,6 @@ class State: def __str__(self) -> str: return self.name - class StatesGroup: """ @@ -192,9 +191,6 @@ class StatesGroup: value.name = ':'.join((cls.__name__, name)) value.group = cls - - - class BaseMiddleware: """ @@ -254,8 +250,6 @@ class SkipHandler: but will skip execution of handler. """ - def __init__(self) -> None: - pass class CancelUpdate: """ @@ -266,5 +260,9 @@ class CancelUpdate: of post_process in middlewares. """ - def __init__(self) -> None: - pass \ No newline at end of file +class ContinueHandling: + """ + Class for continue updates in handlers. + Just return instance of this class + in handlers to continue process. + """