generated from rochacbruno/python-project-template
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implement step_handler and register_next_step_handler
- Loading branch information
1 parent
e91a99a
commit 64347d8
Showing
3 changed files
with
73 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
from typing import Callable | ||
from telebot.async_telebot import AsyncTeleBot,logger | ||
|
||
class HAsyncTeleBot(AsyncTeleBot): | ||
|
||
def step_handler(self): | ||
""" | ||
This decorator should be on top of a method for being used as step handeler | ||
Example: | ||
@bot.step_handler() | ||
def test(message): | ||
pass | ||
""" | ||
def decorator(handler): | ||
handler_name = handler.__name__ | ||
module_name = handler.__module__ | ||
signature = f"{hash(handler.__code__.co_filename)}:{module_name}.{handler_name}:{handler.__code__.co_firstlineno}" | ||
async def wrapper(*args, **kwargs): | ||
msg=args[0] | ||
async with self.retrieve_data(msg.from_user.id,msg.chat.id) as data: | ||
step_kwargs=data.get('__step_handler_kwargs__',{}) | ||
step_args=data.get('__step_handler_args__',()) | ||
if '__step_handler_kwargs__' in data: | ||
del data['__step_handler_kwargs__'] | ||
if '__step_handler_args__' in data: | ||
del data['__step_handler_args__'] | ||
await self.set_state(msg.from_user.id, None, msg.chat.id) | ||
return await handler(*args, *step_args, **kwargs, **step_kwargs) | ||
self.message_handlers.insert(0,{ | ||
'function': wrapper, | ||
'pass_bot': False, | ||
'filters': {'state': signature} | ||
}) | ||
wrapper.signature = signature | ||
return wrapper | ||
return decorator | ||
|
||
async def register_next_step_handler(self, user_id: int, chat_id: int, callback: Callable, *args, **kwargs) -> None: | ||
""" | ||
Registers a callback function to be notified when new message arrives after `message`. | ||
Warning: In case `callback` as lambda function, saving next step handlers will not work. | ||
:param message: The message for which we want to handle new message in the same chat. | ||
:type message: :class:`telebot.types.Message` | ||
:param callback: The callback function which next new message arrives. | ||
:type callback: :obj:`Callable[[telebot.types.Message], None]` | ||
:param args: Args to pass in callback func | ||
:param kwargs: Args to pass in callback func | ||
:return: None | ||
""" | ||
|
||
if not hasattr(callback,'signature'): | ||
raise ValueError("Do not forget to add @bot.step_handler() before function") | ||
await self.set_state(user_id, callback.signature, chat_id) | ||
await self.add_data(user_id, chat_id, __step_handler_args__=args, __step_handler_kwargs__=kwargs ) | ||
|
||
|
||
def callback_query_handler(self, func=None,**kwargs): | ||
if func is None: func=lambda call:True | ||
return super().callback_query_handler(func,**kwargs) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters