Skip to content

fix: prevent reply_to_message_id errors when /newchat creates new topic#22

Closed
Copilot wants to merge 5 commits intomainfrom
copilot/fix-newchat-thread-reply
Closed

fix: prevent reply_to_message_id errors when /newchat creates new topic#22
Copilot wants to merge 5 commits intomainfrom
copilot/fix-newchat-thread-reply

Conversation

Copy link
Contributor

Copilot AI commented Jan 11, 2026

The /newchat command creates a new Topic but Consumer attempts to reply to the original message ID, which doesn't exist in the new Topic. Telegram API silently fails without error or delivery.

Changes

  • Added reply thread validation: Only set reply_to_message_id when target thread matches original message thread
  • Applied consistently: Updated all message sends (responses, errors, timeouts, local commands)
  • Extracted helper: _get_reply_to_id() encapsulates thread-matching logic
  • Improved logging: Enabled INFO level, added context (chat_id, thread_id, reply_to_id) to debug message delivery

Implementation

def _get_reply_to_id(message_id: int, thread_id: int | None, message_thread_id: int | None) -> int | None:
    """Only reply to original message if we're in the same thread."""
    return message_id if thread_id == message_thread_id else None

# Usage
reply_to_id = _get_reply_to_id(message.message_id, thread_id, message.message_thread_id)
await bot.send_message(
    message_thread_id=thread_id,        # New topic ID from /newchat
    reply_to_message_id=reply_to_id,    # None when threads differ
)

Before: Always replied to original message → Silent failure in new topics
After: Conditional reply based on thread match → Messages deliver correctly

Original prompt

This section details on the original issue you should resolve

<issue_title>fix: /newchat 命令回复消息发送到错误的 thread</issue_title>
<issue_description>## 问题描述

/newchat 命令创建新 Topic 后,Consumer 尝试使用原始消息的 message_id 作为 reply_to_message_id,但该消息不存在于新创建的 Topic 中,导致 Telegram API 静默失败,用户收不到回复。

复现步骤

  1. 在群组中执行 /newchat <消息内容>
  2. Bot 成功创建新 Topic
  3. 新 Topic 中没有收到回复

根本原因

consumer.py 中的 send_message 调用:

await bot.send_message(
    chat_id=message.chat_id,
    text=telegram_text,
    parse_mode=ParseMode.MARKDOWN_V2,
    message_thread_id=thread_id,  # 新 Topic 的 thread_id
    reply_to_message_id=message.message_id,  # 原始消息的 ID,在新 Topic 中不存在
)

建议修复

只有当 thread_id 与原始消息的 message_thread_id 相同时才设置 reply_to_message_id

reply_to_id = (
    message.message_id
    if thread_id == message.message_thread_id
    else None
)

await bot.send_message(
    chat_id=message.chat_id,
    text=telegram_text,
    parse_mode=ParseMode.MARKDOWN_V2,
    message_thread_id=thread_id,
    reply_to_message_id=reply_to_id,
)

附加建议

  1. consumer.py 添加 logger.setLevel(logging.INFO) 以启用日志输出
  2. 添加发送消息成功/失败的日志记录便于调试</issue_description>

Comments on the Issue (you are @copilot in this section)


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot AI and others added 4 commits January 11, 2026 16:52
Co-authored-by: BukeLy <19304666+BukeLy@users.noreply.github.com>
Co-authored-by: BukeLy <19304666+BukeLy@users.noreply.github.com>
…comments

Co-authored-by: BukeLy <19304666+BukeLy@users.noreply.github.com>
Co-authored-by: BukeLy <19304666+BukeLy@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix /newchat command reply to wrong thread fix: prevent reply_to_message_id errors when /newchat creates new topic Jan 11, 2026
Copilot AI requested a review from BukeLy January 11, 2026 16:58
@BukeLy BukeLy closed this Feb 4, 2026
@BukeLy BukeLy deleted the copilot/fix-newchat-thread-reply branch February 4, 2026 08:52
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.

fix: /newchat 命令回复消息发送到错误的 thread

2 participants