Skip to content

Python: [Bug]: hosting-telegram merges all topics of a chat into one session and cannot reply inside a topic #10350

Open

Description

Observed Behavior

Telegram has topics in forum supergroups and, since Bot API 9.3, in private chats with bots that enable forum topic mode. Each topic is a separate conversation, but agent-framework-hosting-telegram treats the whole chat as one:

  1. telegram_session_id returns telegram:<bot_id>:<chat_id> for every message in a forum group (and telegram:<bot_id>:<user_id> for every message in a private chat), so messages from different topics share one agent session and one history. A question asked in topic "Project A" is answered with context from topic "Project B".
  2. telegram_from_run (and the sendPhoto operations from telegram_from_streaming_run) build payloads with only chat_id and have no way to set message_thread_id, which the Bot API uses to address a topic. The agent's reply is not posted in the topic the user wrote in.

Nothing in the package or in the samples under python/samples/04-hosting/ reads message_thread_id or is_topic_message.

Expected Behavior

  • Messages from different topics of the same forum get different session ids, while messages in the General topic and in ordinary groups keep today's telegram:<bot_id>:<chat_id>.
  • A reply to a topic message can be sent back into that topic.

Steps to Reproduce

  1. Build two message updates for the same forum supergroup (chat.is_forum: true) with is_topic_message: true and different message_thread_id values.
  2. Call telegram_session_id on both.
  3. Call telegram_from_run with the group's chat id.

Minimal Reproduction

from agent_framework import AgentResponse, Content, Message
from agent_framework_hosting_telegram import telegram_from_run, telegram_session_id

def update(thread_id: int, text: str) -> dict:
    return {
        "update_id": 1,
        "message": {
            "message_id": 10 + thread_id,
            "message_thread_id": thread_id,
            "is_topic_message": True,
            "chat": {"id": -1001234567890, "type": "supergroup", "is_forum": True},
            "from": {"id": 42, "is_bot": False, "first_name": "A"},
            "text": text,
        },
    }

print(telegram_session_id(update(3, "plan for project A"), bot_id=1))
print(telegram_session_id(update(7, "plan for project B"), bot_id=1))
print(telegram_from_run(AgentResponse(messages=[Message("assistant", [Content.from_text(text="ok")])]), chat_id=-1001234567890))

Output on main (92838b6):

telegram:1:-1001234567890
telegram:1:-1001234567890
{'method': 'sendMessage', 'payload': {'chat_id': -1001234567890, 'text': 'ok'}}

Package Versions

agent-framework-core: 1.26.0, agent-framework-hosting-telegram: 1.0.0a261106

Python Version

Python 3.12

Operating System

Windows

Regression

No, it has never handled topics.

Additional Context

The topic should be detected with is_topic_message, not with message_thread_id alone: the Bot API describes message_thread_id as the id of "a message thread or forum topic", so it is also present on ordinary supergroup reply threads, which should keep the chat-level session.

A possible direction, for discussion only:

  • telegram_session_id appends the thread id for topic messages: telegram:<bot_id>:<chat_or_user_id>:<message_thread_id>. This changes session ids for bots already running in topic chats, so it may need to be opt-in.
  • telegram_from_run and telegram_from_streaming_run accept an optional message_thread_id and add it to sendMessage / sendPhoto payloads (editMessageText and deleteMessage address a message id and need no change).
  • A small telegram_message_thread_id(update) helper next to telegram_chat_id.

I would be glad to implement this after agreement on the direction.

AI assistance: the analysis, the reproduction and this report were prepared with an AI coding assistant; I ran the reproduction and checked the output above.

Acknowledgements

  • I searched existing issues and did not find a duplicate.
  • I personally verified this behavior and the reproduction details are authentic.
  • I will wait for explicit maintainer agreement before starting implementation of a non-trivial change.

Activity

  1. added
    python
    triage
    on Nov 13, 2026
  2. added
    reproduced
    on Nov 13, 2026
  3. github-actions commented on Nov 13, 2026

    @github-actions
    Contributor

    Reproduced on main (92838b6) with a locally constructed test based on the reported behavior: two forum-topic updates with different message_thread_id yield the same telegram_session_id, and telegram_from_run emits a sendMessage payload with only chat_id/text. Run: https://github.com/microsoft/agent-framework/actions/runs/43523082062

    Support boundary: unclear. I found no maintained spec, ADR, or public doc stating whether Telegram topics are in scope for agent-framework-hosting-telegram, so whether this is a bug or a scope change (and whether session-id changes should be opt-in) is a maintainer decision. Routed to the package owner.

    Generated by Issue Routing for #10350 · copilot · auto · ~19 AIC · ⌖ ~1 AIC · ⊞ 5.2K · ◷

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

pythonreproducedtriage

Type

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions