Skip to content

Commit

Permalink
implement step_handler and register_next_step_handler
Browse files Browse the repository at this point in the history
  • Loading branch information
hiddify-com committed Jul 12, 2024
1 parent e91a99a commit 64347d8
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 12 deletions.
14 changes: 4 additions & 10 deletions hiddifypanel_bot/basebot/bot.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import logging
import os
from telebot.async_telebot import AsyncTeleBot,logger
from telebot.async_telebot import logger
from telebot import TeleBot, ExceptionHandler,types
from telebot import asyncio_filters
from telebot.asyncio_storage import StateMemoryStorage
from .storage_filter import StorageFilter
from .call_action_filter import CallActionFilter
from .role_filter import RoleFilter
from .middleware import Middleware
from .hasync_telebot import HAsyncTeleBot
logger.setLevel(logging.INFO) # Outputs debug messages to console.


Expand All @@ -21,19 +22,12 @@ def handle(self, exception):
logger.error(exception)
raise exception

bot: AsyncTeleBot = AsyncTeleBot(
bot: HAsyncTeleBot = HAsyncTeleBot(
BOT_TOKEN,
# exception_handler=MyExceptionHandler(),

exception_handler=MyExceptionHandler(),
state_storage=state_storage,
)

def callback(func=None,**kwargs):
if func is None: func=lambda call:True
return bot.callback_query_handler_orig(func,**kwargs)
bot.callback_query_handler_orig=bot.callback_query_handler
bot.callback_query_handler=callback


bot.add_custom_filter(asyncio_filters.StateFilter(bot))
bot.add_custom_filter(asyncio_filters.TextMatchFilter())
Expand Down
67 changes: 67 additions & 0 deletions hiddifypanel_bot/basebot/hasync_telebot.py
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)

4 changes: 2 additions & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
# You can also run `make switch-to-poetry` to use the poetry package manager.

i18nice==0.15.5
#pyTelegramBotAPI==4.21.0
git+https://github.com/hiddify-com/pyTelegramBotAPI.git@async_register_next_step_handler#egg=pyTelegramBotAPI
pyTelegramBotAPI==4.21.0
#git+https://github.com/hiddify-com/pyTelegramBotAPI.git@async_register_next_step_handler#egg=pyTelegramBotAPI
aiohttp
asyncio
watchdog
Expand Down

0 comments on commit 64347d8

Please sign in to comment.