Skip to content

send_message fails to deliver to Telegram forum topics (telegram:chat_id:thread_id) #27012

@HECer

Description

@HECer

Problem

The send_message tool cannot reliably deliver messages to specific topics within Telegram forum supergroups. When the agent is asked to send a file or message to a specific topic, the delivery ends up in the wrong location (General topic, home channel, or DM) instead of the requested topic.

Reproduction

  1. Configure Telegram gateway with a forum supergroup as home channel
  2. Agent receives a request like "schick mir die Datei auf Telegram"
  3. Agent calls send_message(target="telegram:-100XXXXXX:YYYYY", message="... MEDIA:/path/to/file")
  4. Message arrives in the wrong topic or the General channel instead of the specified topic

Root Cause Analysis

After reviewing the codebase, I identified multiple layers where thread/topic targeting can fail:

1. _send_telegram standalone path (tools/send_message_tool.py:757-912)

The standalone send path (used when gateway is not available or CLI sends directly) correctly extracts thread_id from the target and passes it as message_thread_id:

# Line 791-812
if thread_id is not None:
    effective_thread_id = TelegramAdapter._message_thread_id_for_send(str(thread_id))
    if effective_thread_id is not None:
        thread_kwargs["message_thread_id"] = effective_thread_id

However, this path uses a one-shot Bot instance (line 787: bot = Bot(token=token)) which doesn't share state with the gateway's connected adapter. If the thread_id is stale or invalid, there's no fallback mechanism.

2. Gateway live adapter path (gateway/platforms/telegram.py:1491-1630)

The gateway's send() method does have a fallback for "thread not found" errors (line 1595-1599):

if self._is_thread_not_found_error(send_err) and effective_thread_id is not None:
    # Thread doesn't exist — retry without message_thread_id

But this fallback silently delivers to the General topic instead of the correct one, and the user sees no error. The message appears to send successfully but lands in the wrong topic.

3. No thread_id validation before send

Neither path validates whether the thread_id is actually valid for the given chat_id before attempting to send. The Bot API returns "Message thread not found" for invalid thread IDs, but:

  • The standalone path (_send_telegram) surfaces this as an error
  • The gateway path falls back to the General topic silently

4. Session isolation exacerbates the problem

Each Telegram topic creates a separate agent session. When a session restarts:

  • The agent loses context about which topic/thread it was in
  • thread_id metadata may not be propagated correctly
  • The agent may not know the correct thread_id for a topic name

Expected Behavior

  • send_message(target="telegram:-100XXXXXX:YYYYY", ...) should deliver to the specified topic
  • If the thread_id is invalid, an error should be returned (not silently fallback)
  • The agent should be able to resolve topic names to thread_ids

Environment

  • Hermes Agent version: current (as of May 2026)
  • Platform: Telegram gateway

Metadata

Metadata

Assignees

No one assigned

    Labels

    P1High — major feature broken, no workaroundcomp/gatewayGateway runner, session dispatch, deliveryplatform/telegramTelegram bot adaptertool/webWeb search and extractiontype/bugSomething isn't working

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions