initial commit
This commit is contained in:
@@ -0,0 +1,25 @@
|
||||
from .base import BaseHandler, BaseHandlerMixin
|
||||
from .callback_query import CallbackQueryHandler
|
||||
from .chat_member import ChatMemberHandler
|
||||
from .chosen_inline_result import ChosenInlineResultHandler
|
||||
from .error import ErrorHandler
|
||||
from .inline_query import InlineQueryHandler
|
||||
from .message import MessageHandler, MessageHandlerCommandMixin
|
||||
from .poll import PollHandler
|
||||
from .pre_checkout_query import PreCheckoutQueryHandler
|
||||
from .shipping_query import ShippingQueryHandler
|
||||
|
||||
__all__ = (
|
||||
"BaseHandler",
|
||||
"BaseHandlerMixin",
|
||||
"CallbackQueryHandler",
|
||||
"ChatMemberHandler",
|
||||
"ChosenInlineResultHandler",
|
||||
"ErrorHandler",
|
||||
"InlineQueryHandler",
|
||||
"MessageHandler",
|
||||
"MessageHandlerCommandMixin",
|
||||
"PollHandler",
|
||||
"PreCheckoutQueryHandler",
|
||||
"ShippingQueryHandler",
|
||||
)
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
46
venv/lib/python3.12/site-packages/aiogram/handlers/base.py
Normal file
46
venv/lib/python3.12/site-packages/aiogram/handlers/base.py
Normal file
@@ -0,0 +1,46 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from abc import ABC, abstractmethod
|
||||
from typing import TYPE_CHECKING, Any, Dict, Generic, TypeVar, cast
|
||||
|
||||
from aiogram.types import Update
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from aiogram import Bot
|
||||
|
||||
T = TypeVar("T")
|
||||
|
||||
|
||||
class BaseHandlerMixin(Generic[T]):
|
||||
if TYPE_CHECKING:
|
||||
event: T
|
||||
data: Dict[str, Any]
|
||||
|
||||
|
||||
class BaseHandler(BaseHandlerMixin[T], ABC):
|
||||
"""
|
||||
Base class for all class-based handlers
|
||||
"""
|
||||
|
||||
def __init__(self, event: T, **kwargs: Any) -> None:
|
||||
self.event: T = event
|
||||
self.data: Dict[str, Any] = kwargs
|
||||
|
||||
@property
|
||||
def bot(self) -> Bot:
|
||||
from aiogram import Bot
|
||||
|
||||
if "bot" in self.data:
|
||||
return cast(Bot, self.data["bot"])
|
||||
raise RuntimeError("Bot instance not found in the context")
|
||||
|
||||
@property
|
||||
def update(self) -> Update:
|
||||
return cast(Update, self.data.get("update", self.data.get("event_update")))
|
||||
|
||||
@abstractmethod
|
||||
async def handle(self) -> Any: # pragma: no cover
|
||||
pass
|
||||
|
||||
def __await__(self) -> Any:
|
||||
return self.handle().__await__()
|
||||
@@ -0,0 +1,43 @@
|
||||
from abc import ABC
|
||||
from typing import Optional
|
||||
|
||||
from aiogram.handlers import BaseHandler
|
||||
from aiogram.types import CallbackQuery, MaybeInaccessibleMessage, User
|
||||
|
||||
|
||||
class CallbackQueryHandler(BaseHandler[CallbackQuery], ABC):
|
||||
"""
|
||||
There is base class for callback query handlers.
|
||||
|
||||
Example:
|
||||
.. code-block:: python
|
||||
|
||||
from aiogram.handlers import CallbackQueryHandler
|
||||
|
||||
...
|
||||
|
||||
@router.callback_query()
|
||||
class MyHandler(CallbackQueryHandler):
|
||||
async def handle(self) -> Any: ...
|
||||
"""
|
||||
|
||||
@property
|
||||
def from_user(self) -> User:
|
||||
"""
|
||||
Is alias for `event.from_user`
|
||||
"""
|
||||
return self.event.from_user
|
||||
|
||||
@property
|
||||
def message(self) -> Optional[MaybeInaccessibleMessage]:
|
||||
"""
|
||||
Is alias for `event.message`
|
||||
"""
|
||||
return self.event.message
|
||||
|
||||
@property
|
||||
def callback_data(self) -> Optional[str]:
|
||||
"""
|
||||
Is alias for `event.data`
|
||||
"""
|
||||
return self.event.data
|
||||
@@ -0,0 +1,14 @@
|
||||
from abc import ABC
|
||||
|
||||
from aiogram.handlers import BaseHandler
|
||||
from aiogram.types import ChatMemberUpdated, User
|
||||
|
||||
|
||||
class ChatMemberHandler(BaseHandler[ChatMemberUpdated], ABC):
|
||||
"""
|
||||
Base class for chat member updated events
|
||||
"""
|
||||
|
||||
@property
|
||||
def from_user(self) -> User:
|
||||
return self.event.from_user
|
||||
@@ -0,0 +1,18 @@
|
||||
from abc import ABC
|
||||
|
||||
from aiogram.handlers import BaseHandler
|
||||
from aiogram.types import ChosenInlineResult, User
|
||||
|
||||
|
||||
class ChosenInlineResultHandler(BaseHandler[ChosenInlineResult], ABC):
|
||||
"""
|
||||
Base class for chosen inline result handlers
|
||||
"""
|
||||
|
||||
@property
|
||||
def from_user(self) -> User:
|
||||
return self.event.from_user
|
||||
|
||||
@property
|
||||
def query(self) -> str:
|
||||
return self.event.query
|
||||
17
venv/lib/python3.12/site-packages/aiogram/handlers/error.py
Normal file
17
venv/lib/python3.12/site-packages/aiogram/handlers/error.py
Normal file
@@ -0,0 +1,17 @@
|
||||
from abc import ABC
|
||||
|
||||
from aiogram.handlers.base import BaseHandler
|
||||
|
||||
|
||||
class ErrorHandler(BaseHandler[Exception], ABC):
|
||||
"""
|
||||
Base class for errors handlers
|
||||
"""
|
||||
|
||||
@property
|
||||
def exception_name(self) -> str:
|
||||
return self.event.__class__.__name__
|
||||
|
||||
@property
|
||||
def exception_message(self) -> str:
|
||||
return str(self.event)
|
||||
@@ -0,0 +1,18 @@
|
||||
from abc import ABC
|
||||
|
||||
from aiogram.handlers import BaseHandler
|
||||
from aiogram.types import InlineQuery, User
|
||||
|
||||
|
||||
class InlineQueryHandler(BaseHandler[InlineQuery], ABC):
|
||||
"""
|
||||
Base class for inline query handlers
|
||||
"""
|
||||
|
||||
@property
|
||||
def from_user(self) -> User:
|
||||
return self.event.from_user
|
||||
|
||||
@property
|
||||
def query(self) -> str:
|
||||
return self.event.query
|
||||
@@ -0,0 +1,28 @@
|
||||
from abc import ABC
|
||||
from typing import Optional, cast
|
||||
|
||||
from aiogram.filters import CommandObject
|
||||
from aiogram.handlers.base import BaseHandler, BaseHandlerMixin
|
||||
from aiogram.types import Chat, Message, User
|
||||
|
||||
|
||||
class MessageHandler(BaseHandler[Message], ABC):
|
||||
"""
|
||||
Base class for message handlers
|
||||
"""
|
||||
|
||||
@property
|
||||
def from_user(self) -> Optional[User]:
|
||||
return self.event.from_user
|
||||
|
||||
@property
|
||||
def chat(self) -> Chat:
|
||||
return self.event.chat
|
||||
|
||||
|
||||
class MessageHandlerCommandMixin(BaseHandlerMixin[Message]):
|
||||
@property
|
||||
def command(self) -> Optional[CommandObject]:
|
||||
if "command" in self.data:
|
||||
return cast(CommandObject, self.data["command"])
|
||||
return None
|
||||
19
venv/lib/python3.12/site-packages/aiogram/handlers/poll.py
Normal file
19
venv/lib/python3.12/site-packages/aiogram/handlers/poll.py
Normal file
@@ -0,0 +1,19 @@
|
||||
from abc import ABC
|
||||
from typing import List
|
||||
|
||||
from aiogram.handlers import BaseHandler
|
||||
from aiogram.types import Poll, PollOption
|
||||
|
||||
|
||||
class PollHandler(BaseHandler[Poll], ABC):
|
||||
"""
|
||||
Base class for poll handlers
|
||||
"""
|
||||
|
||||
@property
|
||||
def question(self) -> str:
|
||||
return self.event.question
|
||||
|
||||
@property
|
||||
def options(self) -> List[PollOption]:
|
||||
return self.event.options
|
||||
@@ -0,0 +1,14 @@
|
||||
from abc import ABC
|
||||
|
||||
from aiogram.handlers import BaseHandler
|
||||
from aiogram.types import PreCheckoutQuery, User
|
||||
|
||||
|
||||
class PreCheckoutQueryHandler(BaseHandler[PreCheckoutQuery], ABC):
|
||||
"""
|
||||
Base class for pre-checkout handlers
|
||||
"""
|
||||
|
||||
@property
|
||||
def from_user(self) -> User:
|
||||
return self.event.from_user
|
||||
@@ -0,0 +1,14 @@
|
||||
from abc import ABC
|
||||
|
||||
from aiogram.handlers import BaseHandler
|
||||
from aiogram.types import ShippingQuery, User
|
||||
|
||||
|
||||
class ShippingQueryHandler(BaseHandler[ShippingQuery], ABC):
|
||||
"""
|
||||
Base class for shipping query handlers
|
||||
"""
|
||||
|
||||
@property
|
||||
def from_user(self) -> User:
|
||||
return self.event.from_user
|
||||
Reference in New Issue
Block a user