feat: event-driven wait_for_new_message + debounced wait_for_settled_message#130
Merged
Merged
Conversation
…message Adds a Telethon NewMessage(incoming=True) handler (attached to every configured client) that records incoming private, non-bot, non-self messages, plus two tools so agents can react to events instead of polling: - wait_for_new_message(timeout): block until a new incoming user message arrives. - wait_for_settled_message(settle_ms, max_wait_ms): debounced wait — returns a chat only after it has gone quiet for settle_ms, so a user typing several messages in a row is delivered as ONE settled burst (no per-message wakeups). Consumes the burst so the next call returns the next settled chat; timeout result if none settles. Lives in telegram_mcp/tools/events.py and registers via the tools package import. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
chigwell
approved these changes
May 26, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Lets MCP agents react to incoming messages instead of polling
get_messages/list_chatson a timer.A Telethon
NewMessage(incoming=True)handler (attached to every configured client) records incoming private, non-bot, non-self messages per chat. Two new tools expose them:wait_for_new_message(timeout=50)— block until a new incoming user message arrives; returns the chats with pending messages. Does not consume them.wait_for_settled_message(settle_ms=6000, max_wait_ms=50000)— debounced wait. Returns a chat only after it has gone quiet forsettle_ms, so a user who types several messages (or sends file + text) in a row is delivered as one settled burst instead of waking the agent on every message. Each new message resets the timer. Consumes the burst (next call returns the next settled chat); returns{"event": false, "reason": "timeout"}if nothing settles inmax_wait_ms.Why
Per-timer polling is wasteful and reacts on partial input (replying to message 1 of 3). This makes agents event-driven and naturally batches multi-message bursts.
Implementation
telegram_mcp/tools/events.py, registered via thetelegram_mcp.toolspackage import (decorator pattern, like the other tool modules).clients(multi-account safe).Tests / checks
black+flake8clean,pytestgreen locally.🤖 Generated with Claude Code