fix(telegram): omit message_thread_id in DM topic fallback sends with reply anchor (#35739)#35759
Conversation
… reply anchor (NousResearch#35739) The Bot API rejects message_thread_id in private chats. Previously, _thread_kwargs_for_send included it for DM topic fallback sends with a reply anchor, causing the API to reject media sends. The retry path (_send_with_dm_topic_reply_anchor_retry) then stripped both thread_id AND the reply anchor, dropping media into the root chat. Text sends had a workaround via used_thread_fallback, but media methods lacked it. Centralize the fix in _thread_kwargs_for_send so it applies to all message types: return message_thread_id=None when the reply anchor alone provides sufficient routing.
…ad_id omission (NousResearch#35739) The centralized _thread_kwargs_for_send fix returns message_thread_id=None for private-chat DM topic fallback sends with reply anchors. 15 existing tests in test_telegram_thread_fallback.py still asserted the old behavior (message_thread_id == 20197). Updated all assertions to expect None, matching the correct Bot API behavior for private chats.
|
I think there is an adjacent This is slightly different from the reply-anchor/media retry path described here. Current return {
"message_thread_id": None,
"direct_messages_topic_id": int(direct_topic_id),
}Locations on current
In practice this can produce a Bot API payload equivalent to: {"message_thread_id": null, "direct_messages_topic_id": 306210}For private Telegram DM topics, The minimal fix is to omit return {
- "message_thread_id": None,
"direct_messages_topic_id": int(direct_topic_id),
}The existing tests currently encode the old behavior in at least these places:
Those assertions should check that We applied this locally and confirmed the live gateway payload no longer includes |
What
Fixes #35739 — Media messages (images, screenshots, documents) in Telegram DM topics drop into root chat instead of staying in the topic thread.
Root Cause
When
telegram_dm_topic_reply_fallbackis active and areply_to_message_idexists,_thread_kwargs_for_send()returned a resolvedmessage_thread_id. The Telegram Bot API rejectsmessage_thread_idin private chats withBadRequest: message thread not found. The retry path (_send_with_dm_topic_reply_anchor_retry) strips bothmessage_thread_idANDreply_to_message_id, causing media to land in the root chat instead of the topic thread.Text messages didn't have this bug because their
sendmethod had an explicit safeguard — but media send methods (send_multiple_images,send_image_file,send_document) lacked it.Fix
Centralized the fix in
_thread_kwargs_for_send()— when DM topic fallback is active with a reply anchor, returnmessage_thread_id: Noneinstead of the resolved thread ID. Thereply_to_message_idanchor alone provides sufficient topic routing. This applies globally across all message types (text + media).Changes
gateway/platforms/telegram.py: Changed_thread_kwargs_for_send()to return{"message_thread_id": None}for the DM topic fallback + reply anchor case (was:{"message_thread_id": cls._message_thread_id_for_send(thread_id)})tests/gateway/test_telegram_reply_mode.py: Updated 2 existing test expectations to match corrected behaviorVerification
TestDMTopicFallbackReplyToMode—test_thread_kwargs_suppressed_reply_anchor_when_off,test_thread_kwargs_returns_full_when_first,test_thread_kwargs_no_mode_backward_compat)pytest-asyncioin this environment), unrelated to fixgateway/platforms/telegram.py,tests/gateway/test_telegram_reply_mode.py)