generated from geekiot/python-template
feature/handlers - add base utils for handlers #4
2 changed files with 104 additions and 2 deletions
|
|
@ -1 +1,36 @@
|
||||||
__all__ = []
|
__all__ = ["ChatTypeFilter"]
|
||||||
|
|
||||||
|
|
||||||
|
from typing import Union
|
||||||
|
|
||||||
|
from aiogram.filters import BaseFilter
|
||||||
|
from aiogram.types import CallbackQuery, Message
|
||||||
|
|
||||||
|
from bot.handlers.utils.types import ChatType
|
||||||
|
|
||||||
|
|
||||||
|
class ChatTypeFilter(BaseFilter):
|
||||||
|
def __init__(
|
||||||
|
self,
|
||||||
|
chat_types: Union[ChatType, list[ChatType]],
|
||||||
|
) -> None:
|
||||||
|
if isinstance(chat_types, ChatType):
|
||||||
|
self.chat_type = [chat_types.value]
|
||||||
|
else:
|
||||||
|
self.chat_type = [chat.value for chat in chat_types]
|
||||||
|
|
||||||
|
async def __call__(
|
||||||
|
self,
|
||||||
|
event: Union[Message, CallbackQuery],
|
||||||
|
) -> bool:
|
||||||
|
current_chat_type: str
|
||||||
|
|
||||||
|
if isinstance(event, Message):
|
||||||
|
current_chat_type = event.chat.type
|
||||||
|
else:
|
||||||
|
if event.message is None:
|
||||||
|
return False
|
||||||
|
|
||||||
|
current_chat_type = event.message.chat.type
|
||||||
|
|
||||||
|
return current_chat_type in self.chat_type
|
||||||
|
|
|
||||||
|
|
@ -1,15 +1,82 @@
|
||||||
__all__ = ["RouterRegistry"]
|
__all__ = ["RouterRegistry"]
|
||||||
|
|
||||||
|
|
||||||
|
from typing import Any, Callable, Optional, Union
|
||||||
|
|
||||||
from aiogram import Bot, Router
|
from aiogram import Bot, Router
|
||||||
|
from aiogram.filters import BaseFilter, Command
|
||||||
|
from aiogram.types.bot_command import BotCommand
|
||||||
|
from aiogram.types.bot_command_scope_all_private_chats import (
|
||||||
|
BotCommandScopeAllPrivateChats,
|
||||||
|
)
|
||||||
|
|
||||||
|
from bot.handlers.utils.filters import ChatTypeFilter
|
||||||
|
from bot.handlers.utils.types import (
|
||||||
|
ChatType,
|
||||||
|
HandlerMeta,
|
||||||
|
HandlerType,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
class RouterRegistry:
|
class RouterRegistry:
|
||||||
def __init__(self) -> None:
|
def __init__(self) -> None:
|
||||||
self._router = Router()
|
self._router = Router()
|
||||||
|
self._handlers: list[HandlerMeta] = list()
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def router(self) -> Router:
|
def router(self) -> Router:
|
||||||
return self._router
|
return self._router
|
||||||
|
|
||||||
async def set_menu(self, bot: Bot) -> None: ...
|
def register(
|
||||||
|
self,
|
||||||
|
*,
|
||||||
|
description: str,
|
||||||
|
chat_types: Union[ChatType, list[ChatType]],
|
||||||
|
filters: Union[BaseFilter, list[BaseFilter]] = [],
|
||||||
|
command: Optional[str] = None,
|
||||||
|
) -> Callable[[Any], HandlerType]:
|
||||||
|
if isinstance(chat_types, ChatType):
|
||||||
|
chat_types = [chat_types]
|
||||||
|
|
||||||
|
if isinstance(filters, BaseFilter):
|
||||||
|
filters = [filters]
|
||||||
|
|
||||||
|
def decorator(func: HandlerType) -> HandlerType:
|
||||||
|
meta = HandlerMeta(
|
||||||
|
func=func,
|
||||||
|
description=description,
|
||||||
|
chat_types=chat_types,
|
||||||
|
command=command,
|
||||||
|
)
|
||||||
|
self._handlers.append(meta)
|
||||||
|
|
||||||
|
if command is None:
|
||||||
|
self._router.callback_query.register(
|
||||||
|
func,
|
||||||
|
*filters,
|
||||||
|
ChatTypeFilter(chat_types=chat_types),
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
self._router.message.register(
|
||||||
|
func,
|
||||||
|
*filters,
|
||||||
|
ChatTypeFilter(chat_types=chat_types),
|
||||||
|
Command(commands=[command]),
|
||||||
|
)
|
||||||
|
return func
|
||||||
|
|
||||||
|
return decorator
|
||||||
|
|
||||||
|
async def set_menu(self, bot: Bot) -> None:
|
||||||
|
await bot.set_my_commands(
|
||||||
|
[
|
||||||
|
BotCommand(
|
||||||
|
command=meta.command,
|
||||||
|
description=meta.description,
|
||||||
|
)
|
||||||
|
for meta in self._handlers
|
||||||
|
if meta.command is not None
|
||||||
|
and ChatType.PRIVATE in meta.chat_types
|
||||||
|
],
|
||||||
|
scope=BotCommandScopeAllPrivateChats(),
|
||||||
|
)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue