mirror of
https://github.com/aiogram/aiogram.git
synced 2025-12-12 02:03:04 +00:00
More class based handlers
This commit is contained in:
parent
b144332287
commit
895b727ddf
24 changed files with 506 additions and 30 deletions
|
|
@ -7,11 +7,16 @@ There are some base class based handlers what you need to use in your own handle
|
|||
|
||||
- [BaseHandler](#basehandler)
|
||||
- [MessageHandler](message.md)
|
||||
|
||||
- [CallbackQueryHandler](callback_query.md)
|
||||
- [ChosenInlineResultHandler](chosen_inline_result.md)
|
||||
- [InlineQueryHandler](inline_query.md)
|
||||
- [PollHandler](poll.md)
|
||||
- [PreCheckoutQueryHandler](pre_checkout_query.md)
|
||||
- [ShippingQueryHandler](shipping_query.md)
|
||||
|
||||
## BaseHandler
|
||||
|
||||
Base handler is abstract class and should be used in all other class-based handlers.
|
||||
Base handler is generic abstract class and should be used in all other class-based handlers.
|
||||
|
||||
Import: `#!python3 from aiogram.hanler import BaseHandler`
|
||||
|
||||
|
|
@ -21,3 +26,11 @@ This class is also have an default initializer and you don't need to change it.
|
|||
Initializer accepts current event and all contextual data and which can be accessed from the handler through attributes: `event: TelegramEvent` and `data: Dict[Any, str]`
|
||||
|
||||
If instance of the bot is specified in context data or current context it can be accessed through `bot` class attribute.
|
||||
|
||||
|
||||
### For example:
|
||||
```python3
|
||||
class MyHandler(BaseHandler[Message]):
|
||||
async def handle(self) -> Any:
|
||||
await self.event.answer("Hello!")
|
||||
```
|
||||
|
|
|
|||
29
docs/dispatcher/class_based_handlers/callback_query.md
Normal file
29
docs/dispatcher/class_based_handlers/callback_query.md
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
# CallbackQueryHandler
|
||||
|
||||
There is base class for callback query handlers.
|
||||
|
||||
## Simple usage:
|
||||
```pyhton3
|
||||
from aiogram.handlers import CallbackQueryHandler
|
||||
|
||||
...
|
||||
|
||||
@router.callback_query_handler()
|
||||
class MyHandler(CallbackQueryHandler):
|
||||
async def handle(self) -> Any: ...
|
||||
|
||||
```
|
||||
|
||||
## Extension
|
||||
|
||||
This base handler is subclass of [BaseHandler](basics.md#basehandler) with some extensions:
|
||||
|
||||
- `self.from_user` is alias for `self.event.from_user`
|
||||
- `self.message` is alias for `self.event.message`
|
||||
- `self.callback_data` is alias for `self.event.data`
|
||||
|
||||
## Related pages
|
||||
|
||||
- [BaseHandler](basics.md#basehandler)
|
||||
- [CallbackQuery](../../api/types/callback_query.md)
|
||||
- [Router.callback_query_handler](../router.md#callback-query)
|
||||
28
docs/dispatcher/class_based_handlers/chosen_inline_result.md
Normal file
28
docs/dispatcher/class_based_handlers/chosen_inline_result.md
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
# ChosenInlineResultHandler
|
||||
|
||||
There is base class for chosen inline result handlers.
|
||||
|
||||
## Simple usage:
|
||||
```pyhton3
|
||||
from aiogram.handlers import ChosenInlineResultHandler
|
||||
|
||||
...
|
||||
|
||||
@router.chosen_inline_result_handler()
|
||||
class MyHandler(ChosenInlineResultHandler):
|
||||
async def handle(self) -> Any: ...
|
||||
|
||||
```
|
||||
|
||||
## Extension
|
||||
|
||||
This base handler is subclass of [BaseHandler](basics.md#basehandler) with some extensions:
|
||||
|
||||
- `self.chat` is alias for `self.event.chat`
|
||||
- `self.from_user` is alias for `self.event.from_user`
|
||||
|
||||
## Related pages
|
||||
|
||||
- [BaseHandler](basics.md#basehandler)
|
||||
- [ChosenInlineResult](../../api/types/chosen_inline_result.md)
|
||||
- [Router.chosen_inline_result_handler](../router.md#chosen-inline-query)
|
||||
27
docs/dispatcher/class_based_handlers/inline_query.md
Normal file
27
docs/dispatcher/class_based_handlers/inline_query.md
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
# InlineQueryHandler
|
||||
There is base class for inline query handlers.
|
||||
|
||||
## Simple usage:
|
||||
```pyhton3
|
||||
from aiogram.handlers import InlineQueryHandler
|
||||
|
||||
...
|
||||
|
||||
@router.inline_query_handler()
|
||||
class MyHandler(InlineQueryHandler):
|
||||
async def handle(self) -> Any: ...
|
||||
|
||||
```
|
||||
|
||||
## Extension
|
||||
|
||||
This base handler is subclass of [BaseHandler](basics.md#basehandler) with some extensions:
|
||||
|
||||
- `self.chat` is alias for `self.event.chat`
|
||||
- `self.query` is alias for `self.event.query`
|
||||
|
||||
## Related pages
|
||||
|
||||
- [BaseHandler](basics.md#basehandler)
|
||||
- [InlineQuery](../../api/types/inline_query.md)
|
||||
- [Router.inline_query_handler](../router.md#inline-query)
|
||||
|
|
@ -9,8 +9,8 @@ from aiogram.handlers import MessageHandler
|
|||
...
|
||||
|
||||
@router.message_handler()
|
||||
class MyTestMessageHandler(MessageHandler):
|
||||
async def handle() -> Any:
|
||||
class MyHandler(MessageHandler):
|
||||
async def handle(self) -> Any:
|
||||
return SendMessage(chat_id=self.chat.id, text="PASS")
|
||||
|
||||
```
|
||||
|
|
@ -21,3 +21,12 @@ This base handler is subclass of [BaseHandler](basics.md#basehandler) with some
|
|||
|
||||
- `self.chat` is alias for `self.event.chat`
|
||||
- `self.from_user` is alias for `self.event.from_user`
|
||||
|
||||
## Related pages
|
||||
|
||||
- [BaseHandler](basics.md#basehandler)
|
||||
- [Message](../../api/types/message.md)
|
||||
- [Router.message_handler](../router.md#message)
|
||||
- [Router.edited_message_handler](../router.md#edited-message)
|
||||
- [Router.channel_post_handler](../router.md#channel-post)
|
||||
- [Router.edited_channel_post_handler](../router.md#edited-channel-post)
|
||||
|
|
|
|||
28
docs/dispatcher/class_based_handlers/poll.md
Normal file
28
docs/dispatcher/class_based_handlers/poll.md
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
# PollHandler
|
||||
|
||||
There is base class for poll handlers.
|
||||
|
||||
## Simple usage:
|
||||
```pyhton3
|
||||
from aiogram.handlers import PollHandler
|
||||
|
||||
...
|
||||
|
||||
@router.poll_handler()
|
||||
class MyHandler(PollHandler):
|
||||
async def handle(self) -> Any: ...
|
||||
|
||||
```
|
||||
|
||||
## Extension
|
||||
|
||||
This base handler is subclass of [BaseHandler](basics.md#basehandler) with some extensions:
|
||||
|
||||
- `self.question` is alias for `self.event.question`
|
||||
- `self.options` is alias for `self.event.options`
|
||||
|
||||
## Related pages
|
||||
|
||||
- [BaseHandler](basics.md#basehandler)
|
||||
- [Poll](../../api/types/poll.md)
|
||||
- [Router.poll_handler](../router.md#poll)
|
||||
27
docs/dispatcher/class_based_handlers/pre_checkout_query.md
Normal file
27
docs/dispatcher/class_based_handlers/pre_checkout_query.md
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
# PreCheckoutQueryHandler
|
||||
|
||||
There is base class for callback query handlers.
|
||||
|
||||
## Simple usage:
|
||||
```pyhton3
|
||||
from aiogram.handlers import PreCheckoutQueryHandler
|
||||
|
||||
...
|
||||
|
||||
@router.pre_checkout_query_handler()
|
||||
class MyHandler(PreCheckoutQueryHandler):
|
||||
async def handle(self) -> Any: ...
|
||||
|
||||
```
|
||||
|
||||
## Extension
|
||||
|
||||
This base handler is subclass of [BaseHandler](basics.md#basehandler) with some extensions:
|
||||
|
||||
- `self.from_user` is alias for `self.event.from_user`
|
||||
|
||||
## Related pages
|
||||
|
||||
- [BaseHandler](basics.md#basehandler)
|
||||
- [PreCheckoutQuery](../../api/types/pre_checkout_query.md)
|
||||
- [Router.pre_checkout_query_handler](../router.md#pre-checkout-query)
|
||||
27
docs/dispatcher/class_based_handlers/shipping_query.md
Normal file
27
docs/dispatcher/class_based_handlers/shipping_query.md
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
# ShippingQueryHandler
|
||||
|
||||
There is base class for callback query handlers.
|
||||
|
||||
## Simple usage:
|
||||
```pyhton3
|
||||
from aiogram.handlers import ShippingQueryHandler
|
||||
|
||||
...
|
||||
|
||||
@router.shipping_query_handler()
|
||||
class MyHandler(ShippingQueryHandler):
|
||||
async def handle(self) -> Any: ...
|
||||
|
||||
```
|
||||
|
||||
## Extension
|
||||
|
||||
This base handler is subclass of [BaseHandler](basics.md#basehandler) with some extensions:
|
||||
|
||||
- `self.from_user` is alias for `self.event.from_user`
|
||||
|
||||
## Related pages
|
||||
|
||||
- [BaseHandler](basics.md#basehandler)
|
||||
- [ShippingQuery](../../api/types/shipping_query.md)
|
||||
- [Router.shipping_query_handler](../router.md#shipping-query)
|
||||
Loading…
Add table
Add a link
Reference in a new issue