Skip to content

fix(telegram): avoid treating normal replies as topic threads#6174

Open
sakarie9 wants to merge 1 commit intoAstrBotDevs:masterfrom
sakarie9:fix-telegram-relpy
Open

fix(telegram): avoid treating normal replies as topic threads#6174
sakarie9 wants to merge 1 commit intoAstrBotDevs:masterfrom
sakarie9:fix-telegram-relpy

Conversation

@sakarie9
Copy link

@sakarie9 sakarie9 commented Mar 13, 2026

Error message when reply a message in whitelisted group:

[Core] [INFO] [whitelist_check.stage:65]: 会话 ID telegram:GroupMessage:-1001234567890#602671 不在会话白名单中,已终止事件传播。请在配置文件中添加该会话 ID 到白名单。

When replying to messages in Telegram groups, the bot incorrectly appended the reply message ID or thread ID to the group_id and session_id, creating a new session for each reply. This caused whitelist verification to fail because:

  • Whitelist contained: telegram:GroupMessage:-1001234567890
  • Actual session ID: telegram:GroupMessage:-1001234567890#602671

The mismatch occurred because message_thread_id was present in replied messages, but the code unconditionally appended it to the session ID without checking if it was actually a Topic Group message.

Additionally, an incorrect session ID might also prevent the bot from correctly retrieving the context, or cause it to store data under the wrong ID.

Modifications / 改动点

In astrbot/core/platform/sources/telegram/tg_adapter.py (line ~292).

Add an additional check for is_topic_message property to distinguish:

  • True Topic Group messages: Include message_thread_id in session ID for proper topic isolation

  • Regular reply messages: Keep the base group session ID unchanged

  • This is NOT a breaking change. / 这不是一个破坏性变更。

Screenshots or Test Results / 运行截图或测试结果

Reply messages from whitelist group telegram:GroupMessage:-1001234567890 will work correctly now.


Checklist / 检查清单

  • 😊 如果 PR 中有新加入的功能,已经通过 Issue / 邮件等方式和作者讨论过。/ If there are new features added in the PR, I have discussed it with the authors through issues/emails, etc.
  • 👀 我的更改经过了良好的测试,并已在上方提供了“验证步骤”和“运行截图”。/ My changes have been well-tested, and "Verification Steps" and "Screenshots" have been provided above.
  • 🤓 我确保没有引入新依赖库,或者引入了新依赖库的同时将其添加到了 requirements.txtpyproject.toml 文件相应位置。/ I have ensured that no new dependencies are introduced, OR if new dependencies are introduced, they have been added to the appropriate locations in requirements.txt and pyproject.toml.
  • 😮 我的更改没有引入恶意代码。/ My changes do not introduce malicious code.

Summary by Sourcery

Bug Fixes:

  • 通过仅为话题消息附加 message_thread_id,防止在普通回复消息中创建新的 Telegram 群组会话。
Original summary in English

Summary by Sourcery

Bug Fixes:

  • Prevent creation of new Telegram group sessions for normal reply messages by only appending message_thread_id for topic messages.

@dosubot dosubot bot added the size:XS This PR changes 0-9 lines, ignoring generated files. label Mar 13, 2026
@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request addresses a critical bug in how Telegram session IDs were generated, particularly for messages within groups. Previously, the bot would mistakenly treat normal replies as topic messages, leading to an incorrect session ID that failed whitelist checks and could disrupt context management. The change ensures that session IDs are accurately constructed, distinguishing between standard group messages and actual topic-based conversations, thereby resolving whitelisting issues and improving the bot's ability to maintain correct conversational context.

Highlights

  • Telegram Session ID Correction: Fixed an issue where the bot incorrectly appended message_thread_id to group_id for regular replies in Telegram, causing whitelist failures and incorrect session identification.
  • Improved Topic Group Handling: Ensured that message_thread_id is only appended to the session ID when the message is explicitly identified as a is_topic_message, correctly isolating sessions for true Telegram Topic Groups.
Changelog
  • astrbot/core/platform/sources/telegram/tg_adapter.py
    • Modified the logic for generating group_id and session_id to include an is_topic_message check.
Activity
  • No human activity has been recorded on this pull request since its creation.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

嗨——我已经审查了你的修改,看起来非常棒!


Sourcery 对开源项目是免费的——如果你觉得我们的代码审查有帮助,请考虑分享给更多人 ✨
帮我变得更有用!请在每条评论上点 👍 或 👎,我会根据你的反馈改进之后的代码审查。
Original comment in English

Hey - I've reviewed your changes and they look great!


Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

@dosubot dosubot bot added the area:platform The bug / feature is about IM platform adapter, such as QQ, Lark, Telegram, WebChat and so on. label Mar 13, 2026
Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request fixes a bug where replies in regular groups were incorrectly treated as topic messages, leading to incorrect session IDs. The change correctly adds a check for is_topic_message. I've kept the suggestion to improve the robustness of the logic by logging a warning for an edge case where a topic message might be missing a thread ID, as it does not contradict any existing rules.

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

Labels

area:platform The bug / feature is about IM platform adapter, such as QQ, Lark, Telegram, WebChat and so on. size:XS This PR changes 0-9 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant