Add [trigger]: add triggers for router registry class

This commit is contained in:
Kirill Samoylenkov 2025-11-04 15:59:56 +05:00
parent b1d0284cfa
commit ede1124d8a
7 changed files with 39 additions and 36 deletions

View file

@ -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,

View file

@ -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",
)

View file

@ -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,

View file

@ -6,7 +6,7 @@ from database import TelegramUser
@verified_registry.register(
commands="help",
triggers="help",
chat_types=ChatType.PRIVATE,
description="Help Command Verified Description",
)

View file

@ -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",
)

View file

@ -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(),

View file

@ -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