generated from geekiot/python-template
Add [router registry]: add normal callback registration
This commit is contained in:
parent
fb38655010
commit
a12de8a7a5
2 changed files with 9 additions and 6 deletions
|
|
@ -1,9 +1,9 @@
|
||||||
__all__ = ["RouterRegistry"]
|
__all__ = ["RouterRegistry"]
|
||||||
|
|
||||||
|
|
||||||
from typing import Any, Callable, Optional, Union
|
from typing import Any, Callable, Union
|
||||||
|
|
||||||
from aiogram import Bot, Router
|
from aiogram import Bot, F, Router
|
||||||
from aiogram.filters import BaseFilter, Command
|
from aiogram.filters import BaseFilter, Command
|
||||||
from aiogram.types.bot_command import BotCommand
|
from aiogram.types.bot_command import BotCommand
|
||||||
from aiogram.types.bot_command_scope_all_private_chats import (
|
from aiogram.types.bot_command_scope_all_private_chats import (
|
||||||
|
|
@ -33,7 +33,8 @@ class RouterRegistry:
|
||||||
description: str,
|
description: str,
|
||||||
chat_types: Union[ChatType, list[ChatType]],
|
chat_types: Union[ChatType, list[ChatType]],
|
||||||
filters: Union[BaseFilter, list[BaseFilter]] = [],
|
filters: Union[BaseFilter, list[BaseFilter]] = [],
|
||||||
command: Optional[str] = None,
|
command: str,
|
||||||
|
is_callback: bool = False,
|
||||||
) -> Callable[[Any], HandlerType]:
|
) -> Callable[[Any], HandlerType]:
|
||||||
if isinstance(chat_types, ChatType):
|
if isinstance(chat_types, ChatType):
|
||||||
chat_types = [chat_types]
|
chat_types = [chat_types]
|
||||||
|
|
@ -50,11 +51,12 @@ class RouterRegistry:
|
||||||
)
|
)
|
||||||
self._handlers.append(meta)
|
self._handlers.append(meta)
|
||||||
|
|
||||||
if command is None:
|
if is_callback:
|
||||||
self._router.callback_query.register(
|
self._router.callback_query.register(
|
||||||
func,
|
func,
|
||||||
*filters,
|
*filters,
|
||||||
ChatTypeFilter(chat_types=chat_types),
|
ChatTypeFilter(chat_types=chat_types),
|
||||||
|
F.data == command,
|
||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
self._router.message.register(
|
self._router.message.register(
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
from dataclasses import dataclass
|
from dataclasses import dataclass
|
||||||
from typing import Any, Awaitable, Callable, Optional, TypeVar
|
from typing import Any, Awaitable, Callable, TypeVar
|
||||||
|
|
||||||
from bot.handlers.utils.types import ChatType
|
from bot.handlers.utils.types import ChatType
|
||||||
|
|
||||||
|
|
@ -13,4 +13,5 @@ class HandlerMeta:
|
||||||
func: Callable[[Any], Awaitable[Any]]
|
func: Callable[[Any], Awaitable[Any]]
|
||||||
description: str
|
description: str
|
||||||
chat_types: list[ChatType]
|
chat_types: list[ChatType]
|
||||||
command: Optional[str] = None
|
command: str
|
||||||
|
is_callback: bool = False
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue