Skip to content

Add asynchronous context manager for chat actions#77

Open
0zd0 wants to merge 1 commit intohydrogram:devfrom
0zd0:feat/chat-action
Open

Add asynchronous context manager for chat actions#77
0zd0 wants to merge 1 commit intohydrogram:devfrom
0zd0:feat/chat-action

Conversation

@0zd0
Copy link
Contributor

@0zd0 0zd0 commented Mar 17, 2026

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.TYPING with asyncio.sleep
  • Verified enums.ChatAction.CANCEL is 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

@HitaloM HitaloM added the enhancement New feature, request or code enhancement. label Mar 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature, request or code enhancement.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants