Відправник дій у чаті#
Відправник#
- class aiogram.utils.chat_action.ChatActionSender(*, bot: Bot, chat_id: str | int, message_thread_id: int | None = None, action: str = 'typing', interval: float = 5.0, initial_sleep: float = 0.0)[source]#
Ця утиліта допомагає автоматично надсилати дії чату, допоки виконуються тривалі дії боттом,щоб повідомити користувачів бота про те що бот щось робить і не завершив роботу аварійно.
Надає простий для використання контекстний менеджер.
Технічно, відправник запускає фонову завдачу з нескінченним циклом, який працює до завершення дії та надсилає дію чату кожні 5 секунд.
- __init__(*, bot: Bot, chat_id: str | int, message_thread_id: int | None = None, action: str = 'typing', interval: float = 5.0, initial_sleep: float = 0.0) None [source]#
- Параметри:
bot – екземпляр бота, необов’язковий параметр
chat_id – ідентифікатор цільового чату
message_thread_id – unique identifier for the target message thread; supergroups only
action – тип дії
interval – інтервал між ітераціями
initial_sleep – sleep before first sending of the action
- classmethod choose_sticker(chat_id: int | str, bot: Bot, message_thread_id: int | None = None, interval: float = 5.0, initial_sleep: float = 0.0) ChatActionSender [source]#
Створення екземпляру відправника з дією choose_sticker
- classmethod find_location(chat_id: int | str, bot: Bot, message_thread_id: int | None = None, interval: float = 5.0, initial_sleep: float = 0.0) ChatActionSender [source]#
Створення екземпляру відправника з дією find_location
- classmethod record_video(chat_id: int | str, bot: Bot, message_thread_id: int | None = None, interval: float = 5.0, initial_sleep: float = 0.0) ChatActionSender [source]#
Створення екземпляру відправника з дією record_video
- classmethod record_video_note(chat_id: int | str, bot: Bot, message_thread_id: int | None = None, interval: float = 5.0, initial_sleep: float = 0.0) ChatActionSender [source]#
Створення екземпляру відправника з дією record_video_note
- classmethod record_voice(chat_id: int | str, bot: Bot, message_thread_id: int | None = None, interval: float = 5.0, initial_sleep: float = 0.0) ChatActionSender [source]#
Створення екземпляру відправника з дією record_voice
- classmethod typing(chat_id: int | str, bot: Bot, message_thread_id: int | None = None, interval: float = 5.0, initial_sleep: float = 0.0) ChatActionSender [source]#
Створення екземпляру відправника з дією typing
- classmethod upload_document(chat_id: int | str, bot: Bot, message_thread_id: int | None = None, interval: float = 5.0, initial_sleep: float = 0.0) ChatActionSender [source]#
Створення екземпляру відправника з дією upload_document
- classmethod upload_photo(chat_id: int | str, bot: Bot, message_thread_id: int | None = None, interval: float = 5.0, initial_sleep: float = 0.0) ChatActionSender [source]#
Створення екземпляру відправника з дією upload_photo
- classmethod upload_video(chat_id: int | str, bot: Bot, message_thread_id: int | None = None, interval: float = 5.0, initial_sleep: float = 0.0) ChatActionSender [source]#
Створення екземпляру відправника з дією `upload_video
- classmethod upload_video_note(chat_id: int | str, bot: Bot, message_thread_id: int | None = None, interval: float = 5.0, initial_sleep: float = 0.0) ChatActionSender [source]#
Створення екземпляру відправника з дією upload_video_note
- classmethod upload_voice(chat_id: int | str, bot: Bot, message_thread_id: int | None = None, interval: float = 5.0, initial_sleep: float = 0.0) ChatActionSender [source]#
Створення екземпляру відправника з дією upload_voice
Використання#
async with ChatActionSender.typing(bot=bot, chat_id=message.chat.id):
# Do something...
# Perform some long calculations
await message.answer(result)
Проміжні програми#
- class aiogram.utils.chat_action.ChatActionMiddleware[source]#
Допомагає автоматично використовувати відправника дій чату для всіх обробників повідомлень
Використання#
Перед використанням слід зареєструвати для події message
<router or dispatcher>.message.middleware(ChatActionMiddleware())
Після цього всі обробники, що працюють довше за initial_sleep, виконуватимуть дію „typing“ чату
Також відправник може бути налаштованим за допомогою функції міток для певного обробника.
Зміна лише типу дії:
@router.message(...)
@flags.chat_action("sticker")
async def my_handler(message: Message): ...
Зміна конфігурації відправника:
@router.message(...)
@flags.chat_action(initial_sleep=2, action="upload_document", interval=3)
async def my_handler(message: Message): ...