Skip to content

fix: forward thread_id metadata for Telegram forum topic routing - #656

Closed
Bitstreamono wants to merge 1 commit into
NousResearch:mainfrom
Bitstreamono:fix/telegram-forum-thread-routing
Closed

fix: forward thread_id metadata for Telegram forum topic routing#656
Bitstreamono wants to merge 1 commit into
NousResearch:mainfrom
Bitstreamono:fix/telegram-forum-thread-routing

Conversation

@Bitstreamono

Copy link
Copy Markdown

Summary

When using Telegram forum topics (supergroups with topics enabled), incoming messages correctly capture message_thread_id as event.source.thread_id, but responses are sent without metadata={"thread_id": ...}, causing all replies to land in "General" instead of the originating topic thread.

Root Cause

_process_message_background() in base.py never constructs metadata from the event source when calling self.send() and media methods. The TelegramAdapter.send() method already supports message_thread_id via metadata — it's just never called with it.

Additionally, all media methods (send_voice, send_image, send_animation) and send_typing/send_chat_action in the Telegram adapter are missing message_thread_id entirely.

Changes

gateway/platforms/base.py

  • Build _response_metadata from event.source.thread_id in _process_message_background()
  • Pass it to all send calls: text, images, animations, voice, video, documents
  • Add metadata kwarg to base signatures: send_image, send_animation, send_voice, send_video, send_document, send_image_file, send_typing
  • Thread metadata through _keep_typing

gateway/platforms/telegram.py

  • Accept metadata in send_voice, send_image, send_animation, send_typing
  • Extract thread_id and pass as message_thread_id to all Bot API calls

gateway/run.py

  • Build _progress_metadata from source.thread_id in _run_agent()
  • Pass to progress/streaming adapter.send() calls

gateway/platforms/{discord,slack,whatsapp,homeassistant}.py

  • Update send_typing signature for compatibility (metadata=None)

Impact

  • Affected: Telegram forum topic users (supergroups with topics)
  • Not affected: DM users, non-forum groups (thread_id is None, metadata stays empty)
  • Tested: Verified on live Telegram supergroup with multiple forum topics

When using Telegram forum topics, incoming messages correctly capture
message_thread_id as event.source.thread_id, but responses are sent
without metadata={"thread_id": ...}, causing all replies to land in
"General" instead of the originating topic thread.

Changes:
- base.py: Build _response_metadata from event.source.thread_id and
  pass to all send calls (text, images, voice, video, documents).
  Add metadata kwarg to all media method signatures. Thread metadata
  through _keep_typing and send_typing.
- telegram.py: Accept metadata in send_voice, send_image,
  send_animation, send_typing. Extract thread_id and pass as
  message_thread_id to Bot API calls.
- run.py: Build _progress_metadata from source.thread_id and pass
  to progress/streaming adapter.send() calls.
- discord/slack/whatsapp/homeassistant: Update send_typing signature
  for compatibility.
@Bitstreamono
Bitstreamono force-pushed the fix/telegram-forum-thread-routing branch from 3ff7e25 to 0537822 Compare March 8, 2026 22:08
teknium1 added a commit that referenced this pull request Mar 10, 2026
Replies in Telegram forum topics (supergroups with topics) now land in
the correct topic thread instead of 'General'.

- base.py: build thread_id metadata from event.source, pass to all
  send/media calls; add metadata param to send_typing, send_image,
  send_animation, send_voice, send_video, send_document, send_image_file,
  _keep_typing
- telegram.py: extract thread_id from metadata and pass as
  message_thread_id to all Bot API calls (send_photo, send_voice,
  send_audio, send_animation, send_chat_action)
- run.py: pass thread_id metadata to progress/streaming send calls
- discord/slack/whatsapp/homeassistant: update send_typing signature

Based on the fix proposed by @Bitstreamono in PR #656.
teknium1 added a commit that referenced this pull request Mar 10, 2026
…tures

Part 2 of thread_id forum topic fix: add metadata param to
send_voice, send_image, send_animation, send_typing in Telegram
adapter and pass message_thread_id to all Bot API calls. Update
send_typing signature in Discord, Slack, WhatsApp, HomeAssistant
for compatibility.

Based on the fix proposed by @Bitstreamono in PR #656.
@teknium1

Copy link
Copy Markdown
Contributor

Thanks for identifying this bug @Bitstreamono — the thread_id forwarding fix is correct and needed. We've applied the metadata forwarding changes in commits a630ca1 and 928bb16, crediting you in both commit messages.

However, we couldn't merge the PR directly because it also removed two important features:

  1. Session hygiene auto-compression (~160 lines deleted from run.py) — this was added in Gateway session hygiene: pathological messenger transcripts need auto-compact/warn/reset #628 to prevent long-lived sessions from hitting context limits. Removing it would cause regressions for gateway users with long conversations.

  2. Image upload fallback (httpx download path deleted from telegram.py) — the existing two-stage send (URL → file upload → text fallback) handles images >5MB. Your version only has URL → text fallback, which would break large image delivery.

  3. Logging downgrades — several logger.error/info calls were changed to print(), losing structured logging and stack traces.

The thread_id fix itself is great work — just for future PRs, try to keep changes focused on the fix without removing unrelated code. Thanks again!

@teknium1 teknium1 closed this Mar 10, 2026
angelburgosrosado pushed a commit to angelburgosrosado/hermes-agent that referenced this pull request Apr 27, 2026
Replies in Telegram forum topics (supergroups with topics) now land in
the correct topic thread instead of 'General'.

- base.py: build thread_id metadata from event.source, pass to all
  send/media calls; add metadata param to send_typing, send_image,
  send_animation, send_voice, send_video, send_document, send_image_file,
  _keep_typing
- telegram.py: extract thread_id from metadata and pass as
  message_thread_id to all Bot API calls (send_photo, send_voice,
  send_audio, send_animation, send_chat_action)
- run.py: pass thread_id metadata to progress/streaming send calls
- discord/slack/whatsapp/homeassistant: update send_typing signature

Based on the fix proposed by @Bitstreamono in PR NousResearch#656.
angelburgosrosado pushed a commit to angelburgosrosado/hermes-agent that referenced this pull request Apr 27, 2026
…tures

Part 2 of thread_id forum topic fix: add metadata param to
send_voice, send_image, send_animation, send_typing in Telegram
adapter and pass message_thread_id to all Bot API calls. Update
send_typing signature in Discord, Slack, WhatsApp, HomeAssistant
for compatibility.

Based on the fix proposed by @Bitstreamono in PR NousResearch#656.
02356abc pushed a commit to 02356abc/hermes-agent that referenced this pull request May 14, 2026
Replies in Telegram forum topics (supergroups with topics) now land in
the correct topic thread instead of 'General'.

- base.py: build thread_id metadata from event.source, pass to all
  send/media calls; add metadata param to send_typing, send_image,
  send_animation, send_voice, send_video, send_document, send_image_file,
  _keep_typing
- telegram.py: extract thread_id from metadata and pass as
  message_thread_id to all Bot API calls (send_photo, send_voice,
  send_audio, send_animation, send_chat_action)
- run.py: pass thread_id metadata to progress/streaming send calls
- discord/slack/whatsapp/homeassistant: update send_typing signature

Based on the fix proposed by @Bitstreamono in PR NousResearch#656.
02356abc pushed a commit to 02356abc/hermes-agent that referenced this pull request May 14, 2026
…tures

Part 2 of thread_id forum topic fix: add metadata param to
send_voice, send_image, send_animation, send_typing in Telegram
adapter and pass message_thread_id to all Bot API calls. Update
send_typing signature in Discord, Slack, WhatsApp, HomeAssistant
for compatibility.

Based on the fix proposed by @Bitstreamono in PR NousResearch#656.
waefrebeorn pushed a commit to waefrebeorn/slermes that referenced this pull request Jul 2, 2026
Replies in Telegram forum topics (supergroups with topics) now land in
the correct topic thread instead of 'General'.

- base.py: build thread_id metadata from event.source, pass to all
  send/media calls; add metadata param to send_typing, send_image,
  send_animation, send_voice, send_video, send_document, send_image_file,
  _keep_typing
- telegram.py: extract thread_id from metadata and pass as
  message_thread_id to all Bot API calls (send_photo, send_voice,
  send_audio, send_animation, send_chat_action)
- run.py: pass thread_id metadata to progress/streaming send calls
- discord/slack/whatsapp/homeassistant: update send_typing signature

Based on the fix proposed by @Bitstreamono in PR NousResearch#656.
waefrebeorn pushed a commit to waefrebeorn/slermes that referenced this pull request Jul 2, 2026
…tures

Part 2 of thread_id forum topic fix: add metadata param to
send_voice, send_image, send_animation, send_typing in Telegram
adapter and pass message_thread_id to all Bot API calls. Update
send_typing signature in Discord, Slack, WhatsApp, HomeAssistant
for compatibility.

Based on the fix proposed by @Bitstreamono in PR NousResearch#656.
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