Skip to content

fix(telegram): Preserve Telegram forum topic for message tool sends#2772

Open
bogdanovich wants to merge 1 commit intosipeed:mainfrom
bogdanovich:fix/message-tool-telegram-topic
Open

fix(telegram): Preserve Telegram forum topic for message tool sends#2772
bogdanovich wants to merge 1 commit intosipeed:mainfrom
bogdanovich:fix/message-tool-telegram-topic

Conversation

@bogdanovich
Copy link
Copy Markdown
Contributor

Summary

This PR preserves Telegram forum topic routing for messages sent through the message tool.

When an agent is handling a Telegram forum topic, normal final assistant responses already preserve the inbound TopicID. However, interim messages sent through the message tool could lose the topic when the model supplied an explicit chat_id without a forum thread suffix.

Problem

For Telegram forum groups, this sequence could happen:

  1. User sends a message in topic 6.
  2. The agent calls the message tool with an explicit target like:
    {"channel":"telegram","chat_id":"-1001234567890","content":"Working on it..."}
  3. The message tool builds a fresh outbound context with only channel/chat/reply fields.
  4. The Telegram channel sends sendMessage without message_thread_id.
  5. Telegram delivers the interim message to the default topic instead of the active topic.

Final assistant responses did not have this issue because they are built from the inbound context and keep TopicID.

Fix

The message tool send callback now inherits the current topic when:

  • the tool is sending to the same channel/chat as the current turn,
  • the outbound context does not already specify a topic,
  • and the current session scope contains a topic.

It supports both scope forms PicoClaw already uses:

  • explicit topic dimension values like topic:6, and
  • Telegram forum isolation encoded in the chat scope value, e.g. group:-1001234567890/6.

The fix does not override an explicitly provided topic and does not apply when sending to a different chat.

Validation

Added a regression test covering a Telegram forum message where the model calls message with an explicit chat_id; the outbound message now keeps Context.TopicID == "6".

Ran:

go test ./pkg/agent -run 'TestProcessMessage_MessageTool|TestProcessMessageSync_PreservesInboundTopicOnFinalResponse'

@bogdanovich bogdanovich changed the title Preserve Telegram forum topic for message tool sends fix(telegram): Preserve Telegram forum topic for message tool sends May 5, 2026
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.

1 participant