Add asynchronous context manager for chat actions by 0zd0 · Pull Request #77 · hydrogram/hydrogram
Description
This PR introduces an asynchronous context manager (action) to the Messages mixin, allowing users to continuously broadcast a chat action (e.g., typing, uploading) without manually managing the loop.
It creates a background task that resends the action every delay seconds and automatically sends enums.ChatAction.CANCEL upon exiting the context block.
Example usage:
import asyncio from hydrogram import enums async with app.action(chat_id, enums.ChatAction.TYPING): await asyncio.sleep(10) await app.send_message(chat_id, "Done!")
Type of change
- New feature (non-breaking change which adds functionality)
- This change requires a documentation update
How Has This Been Tested?
Manually tested by running a bot and invoking the context manager with a 10-second sleep to verify the typing indicator remains active beyond the standard 5-second Telegram limit, and disappears exactly after the block exits.
- Tested
enums.ChatAction.TYPINGwithasyncio.sleep - Verified
enums.ChatAction.CANCELis properly dispatched on__aexit__
Test Configuration
- Operating System: macOS
- Python Version: 3.14
Checklist
- My code follows the style guidelines of this project
- I have performed a self-review of my own code
- I have made corresponding changes to the documentation
- I have added tests that prove my fix is effective or that my feature works
- My changes generate no new warnings
- New and existing unit tests pass locally with my changes