1
0
mirror of https://github.com/eternnoir/pyTelegramBotAPI.git synced 2023-08-10 21:12:57 +03:00

Merge pull request #1742 from byehack/ContinueHandling

Support ContinueHandling
This commit is contained in:
Badiboy 2022-10-11 19:03:44 +03:00 committed by GitHub
commit 31c3a2b2a3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 185 additions and 89 deletions

View File

@ -0,0 +1,27 @@
from telebot.async_telebot import AsyncTeleBot
from telebot.asyncio_handler_backends import ContinueHandling
bot = AsyncTeleBot('TOKEN')
@bot.message_handler(commands=['start'])
async def start(message):
await bot.send_message(message.chat.id, 'Hello World!')
return ContinueHandling()
@bot.message_handler(commands=['start'])
async def start2(message):
"""
This handler comes after the first one, but it will never be called.
But you can call it by returning ContinueHandling() in the first handler.
If you return ContinueHandling() in the first handler, the next
registered handler with appropriate filters will be called.
"""
await bot.send_message(message.chat.id, 'Hello World2!')
import asyncio
asyncio.run(bot.polling()) # just a reminder that infinity polling
# wraps polling into try/except block just as sync version,
# but you can use any of them because neither of them stops if you
# pass non_stop=True

View File

@ -0,0 +1,23 @@
from telebot import TeleBot
from telebot.handler_backends import ContinueHandling
bot = TeleBot('TOKEN')
@bot.message_handler(commands=['start'])
def start(message):
bot.send_message(message.chat.id, 'Hello World!')
return ContinueHandling()
@bot.message_handler(commands=['start'])
def start2(message):
"""
This handler comes after the first one, but it will never be called.
But you can call it by returning ContinueHandling() in the first handler.
If you return ContinueHandling() in the first handler, the next
registered handler with appropriate filters will be called.
"""
bot.send_message(message.chat.id, 'Hello World2!')
bot.infinity_polling()

View File

@ -38,7 +38,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
@ -6078,11 +6081,13 @@ class TeleBot:
for handler in handlers:
if self._test_message_handler(handler, message):
if handler.get('pass_bot', False):
handler['function'](message, bot = self)
result = handler['function'](message, bot=self)
else:
handler['function'](message)
result = handler['function'](message)
if not isinstance(result, ContinueHandling):
break
else:
return
data = {}
params =[]
handler_error = None
@ -6105,20 +6110,21 @@ class TeleBot:
elif isinstance(result, SkipHandler):
skip_handlers = True
if handlers and not(skip_handlers):
if handlers and not skip_handlers:
try:
for handler in handlers:
process_handler = self._test_message_handler(handler, message)
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
@ -6133,7 +6139,8 @@ 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)
result = handler["function"](message, **data_copy)
if not isinstance(result, ContinueHandling):
break
except Exception as e:
handler_error = e

View File

@ -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
@ -497,16 +497,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
@ -521,7 +519,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:

View File

@ -3,8 +3,6 @@ File with all middleware classes, states.
"""
class BaseMiddleware:
"""
Base class for middleware.
@ -96,6 +94,7 @@ class SkipHandler:
def __init__(self) -> None:
pass
class CancelUpdate:
"""
Class for canceling updates.
@ -107,3 +106,26 @@ class CancelUpdate:
def __init__(self) -> None:
pass
class ContinueHandling:
"""
Class for continue updates in handlers.
Just return instance of this class
in handlers to continue process.
.. code-block:: python3
:caption: Example of using ContinueHandling
@bot.message_handler(commands=['start'])
async def start(message):
await bot.send_message(message.chat.id, 'Hello World!')
return ContinueHandling()
@bot.message_handler(commands=['start'])
async def start2(message):
await bot.send_message(message.chat.id, 'Hello World2!')
"""
def __init__(self) -> None:
pass

View File

@ -175,7 +175,6 @@ class State:
return self.name
class StatesGroup:
"""
Class representing common states.
@ -193,9 +192,6 @@ class StatesGroup:
value.group = cls
class BaseMiddleware:
"""
Base class for middleware.
@ -253,10 +249,10 @@ class SkipHandler:
Update will go to post_process,
but will skip execution of handler.
"""
def __init__(self) -> None:
pass
class CancelUpdate:
"""
Class for canceling updates.
@ -265,6 +261,28 @@ class CancelUpdate:
Update will skip handler and execution
of post_process in middlewares.
"""
def __init__(self) -> None:
pass
class ContinueHandling:
"""
Class for continue updates in handlers.
Just return instance of this class
in handlers to continue process.
.. code-block:: python3
:caption: Example of using ContinueHandling
@bot.message_handler(commands=['start'])
def start(message):
bot.send_message(message.chat.id, 'Hello World!')
return ContinueHandling()
@bot.message_handler(commands=['start'])
def start2(message):
bot.send_message(message.chat.id, 'Hello World2!')
"""
def __init__(self) -> None:
pass