Add [messages & callbacks]: add the bot menu keyboard & update register router

This commit is contained in:
Kirill Samoylenkov 2025-10-24 17:06:24 +05:00
parent ff74a5d03f
commit c690226d8a
7 changed files with 60 additions and 27 deletions

View file

@ -0,0 +1,21 @@
from aiogram.types import CallbackQuery, Message
from bot.handlers import registry
from bot.handlers.utils.keyboards import get_menu_markup
from bot.handlers.utils.types import ChatType
@registry.register(
commands="menu",
chat_types=ChatType.PRIVATE,
description="Menu Callback Function Description",
is_callback=True,
)
async def cmd_menu(call: CallbackQuery) -> None:
if not isinstance(call.message, Message):
return
await call.message.edit_text(
"Menu Callback Function Answer Text",
reply_markup=get_menu_markup(),
)

View file

@ -5,9 +5,9 @@ from bot.handlers.utils.types import ChatType
@registry.register(
command="help",
commands="help",
chat_types=ChatType.PRIVATE,
description="Test Help Function Description",
description="Help Command Function Description",
)
async def cmd_help(message: Message) -> None:
await message.answer("Test Help Function Answer Text")
await message.answer("Help Command Function Answer Text")

View file

@ -1,13 +1,17 @@
from aiogram.types import Message
from bot.handlers import registry
from bot.handlers.utils.keyboards import get_menu_markup
from bot.handlers.utils.types import ChatType
@registry.register(
command="start",
commands=["menu", "start"],
chat_types=ChatType.PRIVATE,
description="Test Start Function Description",
description="Menu Command Function Description",
)
async def cmd_menu(message: Message) -> None:
await message.answer("Test Start Function Answer Text")
await message.answer(
"Menu Command Function Answer Text",
reply_markup=get_menu_markup(),
)

View file

@ -1,13 +0,0 @@
from aiogram.types import Message
from bot.handlers import registry
from bot.handlers.utils.types import ChatType
@registry.register(
command="group",
chat_types=ChatType.GROUP,
description="Test Group Function Description",
)
async def cmd_group(message: Message) -> None:
await message.answer("Test Group Function Answer Text")

View file

@ -1,4 +1,19 @@
__all__ = []
__all__ = ["get_menu_markup"]
# TODO: Add reply keyboard markups for bot here.
from aiogram.utils.keyboard import (
InlineKeyboardBuilder,
InlineKeyboardButton,
InlineKeyboardMarkup,
)
def get_menu_markup() -> InlineKeyboardMarkup:
"""
Reply markup for the bot callback menu & message menu.
"""
builder = InlineKeyboardBuilder()
builder.add(
InlineKeyboardButton(text="Test Menu", callback_data="menu"),
)
return builder.as_markup()

View file

@ -54,7 +54,7 @@ class RouterRegistry:
description: str,
chat_types: Union[ChatType, list[ChatType]],
filters: Union[BaseFilter, list[BaseFilter]] = [],
command: str,
commands: str | list[str],
is_callback: bool = False,
) -> Callable[[Any], HandlerType]:
"""
@ -84,8 +84,10 @@ class RouterRegistry:
handler description for Telegram bot menu.
chat_types (Union[ChatType, list[ChatType]]):
list of Telegram chat types.
command (str):
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):
List of a handler's filters.
Defaults to [].
@ -99,12 +101,16 @@ class RouterRegistry:
if isinstance(filters, BaseFilter):
filters = [filters]
if isinstance(commands, str):
commands = [commands]
def decorator(func: HandlerType) -> HandlerType:
meta = HandlerMeta(
func=func,
description=description,
chat_types=chat_types,
command=command,
command=commands[0],
is_callback=is_callback,
)
self._handlers.append(meta)
@ -113,14 +119,14 @@ class RouterRegistry:
func,
*filters,
ChatTypeFilter(chat_types=chat_types),
F.data == command,
F.data.in_(commands),
)
else:
self._router.message.register(
func,
*filters,
ChatTypeFilter(chat_types=chat_types),
Command(commands=[command]),
Command(commands=commands),
)
return func
@ -140,7 +146,7 @@ class RouterRegistry:
description=meta.description,
)
for meta in self._handlers
if meta.command is not None
if meta.is_callback is False
and ChatType.PRIVATE in meta.chat_types
],
scope=BotCommandScopeAllPrivateChats(),