Skip to content

feat: event-driven wait_for_new_message + debounced wait_for_settled_message#130

Merged
chigwell merged 1 commit into
chigwell:mainfrom
artgas1:feat/event-driven-wait-tools
May 26, 2026
Merged

feat: event-driven wait_for_new_message + debounced wait_for_settled_message#130
chigwell merged 1 commit into
chigwell:mainfrom
artgas1:feat/event-driven-wait-tools

Conversation

@artgas1
Copy link
Copy Markdown
Contributor

@artgas1 artgas1 commented May 25, 2026

Summary

Lets MCP agents react to incoming messages instead of polling get_messages/list_chats on 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 for settle_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 in max_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

  • New module telegram_mcp/tools/events.py, registered via the telegram_mcp.tools package import (decorator pattern, like the other tool modules).
  • In-process state (single asyncio loop), no new deps.
  • Handler attached to all clients in clients (multi-account safe).

Tests / checks

  • black + flake8 clean, pytest green locally.
  • Package imports cleanly; handler registers on the default client.

🤖 Generated with Claude Code

…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 chigwell merged commit 2816a4e into chigwell:main May 26, 2026
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants