diff --git a/src/bot/handlers/unverified/callbacks/menu.py b/src/bot/handlers/unverified/callbacks/menu.py index f642dd9..7ecea40 100644 --- a/src/bot/handlers/unverified/callbacks/menu.py +++ b/src/bot/handlers/unverified/callbacks/menu.py @@ -6,7 +6,7 @@ from bot.utils.types import ChatType @unverified_registry.register( - commands="menu", + triggers="menu", chat_types=ChatType.PRIVATE, description="Menu Callback Unverified Description", is_callback=True, diff --git a/src/bot/handlers/unverified/commands/menu.py b/src/bot/handlers/unverified/commands/menu.py index 469e416..a1432f9 100644 --- a/src/bot/handlers/unverified/commands/menu.py +++ b/src/bot/handlers/unverified/commands/menu.py @@ -6,7 +6,6 @@ from bot.utils.types import ChatType @unverified_registry.register( - commands=["menu", "start"], chat_types=ChatType.PRIVATE, description="Menu Message Unverified Description", ) diff --git a/src/bot/handlers/verified/callbacks/menu.py b/src/bot/handlers/verified/callbacks/menu.py index 867757c..65f9d1f 100644 --- a/src/bot/handlers/verified/callbacks/menu.py +++ b/src/bot/handlers/verified/callbacks/menu.py @@ -6,7 +6,7 @@ from bot.utils.types import ChatType @verified_registry.register( - commands="menu", + triggers="menu", chat_types=ChatType.PRIVATE, description="Menu Callback Verified Description", is_callback=True, diff --git a/src/bot/handlers/verified/commands/help.py b/src/bot/handlers/verified/commands/help.py index f76ca40..57fa93a 100644 --- a/src/bot/handlers/verified/commands/help.py +++ b/src/bot/handlers/verified/commands/help.py @@ -6,7 +6,7 @@ from database import TelegramUser @verified_registry.register( - commands="help", + triggers="help", chat_types=ChatType.PRIVATE, description="Help Command Verified Description", ) diff --git a/src/bot/handlers/verified/commands/menu.py b/src/bot/handlers/verified/commands/menu.py index 3fa37cd..6d2db9a 100644 --- a/src/bot/handlers/verified/commands/menu.py +++ b/src/bot/handlers/verified/commands/menu.py @@ -6,7 +6,7 @@ from bot.utils.types import ChatType @verified_registry.register( - commands=["menu", "start"], + triggers=["menu", "start"], chat_types=ChatType.PRIVATE, description="Menu Command Verified Description", ) diff --git a/src/bot/utils/registry/__init__.py b/src/bot/utils/registry/__init__.py index 9fd1c21..504ec7d 100644 --- a/src/bot/utils/registry/__init__.py +++ b/src/bot/utils/registry/__init__.py @@ -53,8 +53,8 @@ class RouterRegistry: *, description: str, chat_types: Union[ChatType, list[ChatType]], - filters: Union[BaseFilter, list[BaseFilter]] = [], - commands: str | list[str], + filters: Union[BaseFilter, list[BaseFilter], None] = None, + triggers: Union[str, list[str], None] = None, is_callback: bool = False, ) -> Callable[[Any], HandlerType]: """ @@ -69,7 +69,7 @@ class RouterRegistry: @registry.register( - command="start", + triggers="start", chat_types=ChatType.PRIVATE, description="Test Start Function Description", ) @@ -84,13 +84,13 @@ class RouterRegistry: handler description for Telegram bot menu. chat_types (Union[ChatType, list[ChatType]]): list of Telegram chat types. - commands (str | list[str]): - specifying handler command trigger (data for callback). - It can be a list of str for callbacks & messages. - Use first command as main command for bot menu. - filters (Union[BaseFilter, list[BaseFilter]], optional): + triggers (Union[str, list[str], None]): + specifying handler commands or callbacks data triggers. + Use not-callback first trigger as command for bot menu. + Defaults to None. + filters (Union[BaseFilter, list[BaseFilter]], None): List of a handler's filters. - Defaults to []. + Defaults to None. is_callback (bool, optional): If the func is for callback, then True. Defaults to False. @@ -100,36 +100,40 @@ class RouterRegistry: if isinstance(filters, BaseFilter): filters = [filters] + elif filters is None: + filters = list() - if isinstance(commands, str): - commands = [commands] + if isinstance(triggers, str): + triggers = [triggers] + + if triggers is not None: + filters.append( + F.data.in_(triggers) + if is_callback + else Command(commands=triggers) + ) + + filters.append(ChatTypeFilter(chat_types=chat_types)) def decorator(func: HandlerType) -> HandlerType: meta = HandlerMeta( func=func, description=description, chat_types=chat_types, - command=commands[0], + menu_command=( + triggers[0] + if (triggers is not None) and (not is_callback) + else None + ), is_callback=is_callback, ) self._handlers.append(meta) if is_callback: - # FIXME: - # https://github.com/orgs/dibuildo/projects/2?pane=issue&itemId=135588043 - self._router.callback_query.register( - func, - *filters, - ChatTypeFilter(chat_types=chat_types), - F.data.in_(commands), - ) + self._router.callback_query.register(func, *filters) else: - self._router.message.register( - func, - *filters, - ChatTypeFilter(chat_types=chat_types), - Command(commands=commands), - ) + self._router.message.register(func, *filters) + return func return decorator @@ -144,11 +148,11 @@ class RouterRegistry: await bot.set_my_commands( [ BotCommand( - command=meta.command, + command=meta.menu_command, description=meta.description, ) for meta in self._handlers - if meta.is_callback is False + if meta.menu_command is not None and ChatType.PRIVATE in meta.chat_types ], scope=BotCommandScopeAllPrivateChats(), diff --git a/src/bot/utils/types/handler.py b/src/bot/utils/types/handler.py index d589bb9..90bca21 100644 --- a/src/bot/utils/types/handler.py +++ b/src/bot/utils/types/handler.py @@ -20,8 +20,8 @@ class HandlerMeta: handler description for Telegram bot menu. chat_types (list[ChatType]): list of Telegram chat types. - command (str): - specifying handler command trigger (data for callback). + menu_command (str): + specifying menu command for message handler. is_callback (bool): If the func is for callback, then True. Defaults to False. @@ -30,5 +30,5 @@ class HandlerMeta: func: Callable[[Any], Awaitable[Any]] description: str chat_types: list[ChatType] - command: str + menu_command: str | None = None is_callback: bool = False