#17952: fix(telegram): support message_thread_id for DM topics
channel: telegram
stale
size: XS
Cluster:
Messaging Platform Improvements
Fixes #17977
## Summary
`buildTelegramThreadParams()` unconditionally returns `undefined` for `scope: "dm"`, stripping `message_thread_id` from all DM sends since `19b8416a8`. This breaks reply targeting for bots with Telegram DM topics enabled (Bot API 9.3, Dec 31 2025).
## Fix
Pass through `message_thread_id` when present (`> 0`) for DM scope. Only omit it for regular DMs without topics (where thread ID would be 0 or absent). Invalid values (≤ 0) are filtered at the source rather than relying on the send-path fallback to catch API rejections.
## Changes
- `src/telegram/bot/helpers.ts`: Allow positive thread IDs through for DM scope
- `src/telegram/bot/helpers.test.ts`: Updated tests for DM topic behavior
- `src/telegram/draft-stream.test.ts`: Updated draft stream test for DM topic threading
- `src/telegram/bot/delivery.test.ts`: Updated delivery test to expect `message_thread_id` for DM topics
<!-- greptile_comment -->
<h3>Greptile Summary</h3>
This PR restores DM topic support that was unintentionally broken by a previous fix. The change in `buildTelegramThreadParams()` now correctly passes through `message_thread_id` for DMs when a topic ID is present (> 0), while filtering out invalid values (≤ 0) for regular DMs without topics. This aligns with Telegram Bot API 9.3's DM topics feature.
- Updated `src/telegram/bot/helpers.ts:133-136` to conditionally return thread params for DM scope based on thread ID value
- Updated all related tests to expect `message_thread_id` for DM topics instead of omitting it
- The fix properly handles edge cases: filters out 0 and negative IDs, truncates decimals, and preserves existing forum topic behavior
<h3>Confidence Score: 5/5</h3>
- This PR is safe to merge with minimal risk
- The fix is surgical, well-tested, and restores previously working functionality. The logic change is simple (adding a conditional check for positive thread IDs), all tests have been updated to match the new behavior, and the fix aligns with documented Telegram Bot API features. The change only affects DM scope while preserving existing forum and regular group behavior.
- No files require special attention
<sub>Last reviewed commit: e65d964</sub>
<!-- greptile_other_comments_section -->
<sub>(2/5) Greptile learns from your feedback when you react with thumbs up/down!</sub>
<!-- /greptile_comment -->
Most Similar PRs
#17968: fix(telegram): restore DM topic thread ids with send-path fallback
by Leonccaa · 2026-02-16
91.5%
#19213: Telegram: preserve DM topic thread in direct replies
by Kemalau · 2026-02-17
90.5%
#7261: fix(telegram): preserve DM topic thread id for outbound media
by ViffyGwaanl · 2026-02-02
88.7%
#14443: fix(telegram): skip General topic thread ID for all chat types (#14...
by lailoo · 2026-02-12
88.1%
#12936: fix(telegram): omit message_thread_id for private DM chats
by omair445 · 2026-02-09
87.0%
#3368: fix: sessions navigation, DM thread display, and DM thread delivery...
by Lukavyi · 2026-01-28
86.8%
#6192: Telegram: fix DM Topics thread routing
by ViffyGwaanl · 2026-02-01
84.4%
#12758: fix(telegram): drop message_thread_id=1 for DM chats
by yukihamada · 2026-02-09
82.5%
#19050: fix(telegram): skip message_thread_id for private chats to prevent ...
by Limitless2023 · 2026-02-17
82.1%
#8985: fix(telegram): preserve forum topic context for pairing auto-replies
by lailoo · 2026-02-04
81.9%