#3368: fix: sessions navigation, DM thread display, and DM thread delivery regression
channel: telegram
app: web-ui
Cluster:
Messaging Platform Improvements
## Summary
<img width="3656" height="2560" alt="CleanShot 2026-01-28 at 16 27 14@2x" src="https://github.com/user-attachments/assets/b97801c3-9536-4b06-8c67-38a31ef88ea8" />
Fixes #3367
Three related fixes for Telegram DM topics:
1. **Display**: Adds `ThreadLabel` so DM topic sessions show their thread ID
2. **Navigation**: Fixes clicking a session in Sessions tab redirecting to Main instead of the selected session
3. **Delivery**: Fixes bot replies going to General instead of the originating DM thread (regression from commit 9154971)
## Changes
- **src/telegram/bot-message-context.ts**: Set `ThreadLabel: "Thread: N"` for non-group messages with `messageThreadId`; introduce `effectiveThreadId` for correct outbound delivery in DM threads
- **src/telegram/bot-message-context.dm-threads.test.ts**: Add tests for ThreadLabel in DM threads
- **ui/src/ui/app-lifecycle.ts**: Read URL session param before `syncTabWithLocation` overwrites it with localStorage value
## Problem 1: Display
All DM topic sessions showed the same display name. Now each shows its thread ID (`Thread: 42`, `Thread: 99`).
## Problem 2: Navigation
Clicking a session in the Sessions tab redirected to Main instead of the clicked session.
**Root cause**: `syncTabWithLocation()` was called before `applySettingsFromUrl()`, overwriting the URL `?session=` param with localStorage.
**Fix**: Call `applySettingsFromUrl()` first.
## Problem 3: DM Thread Delivery (regression)
Commit 9154971 (`fix(telegram): ignore message_thread_id for non-forum group sessions`) changed `resolveTelegramForumThreadId` to return `undefined` when `is_forum` is false. This correctly fixed reply-chain pollution in regular groups, but broke DM thread delivery:
- Telegram Bot API **never** sets `is_forum=true` for private chats (only supergroups)
- `context.resolvedThreadId` (used by `bot-message-dispatch.ts` for draft streaming and reply delivery) became `undefined` for all DM threads
- Replies, typing indicators, and voice recording indicators all went to General
**Fix**: Introduce `effectiveThreadId = isGroup ? resolvedThreadId : messageThreadId` — uses raw `messageThreadId` for DMs (bypassing the forum gate) while preserving forum-resolved value for groups.
## Testing
```bash
pnpm test src/telegram/bot-message-context.dm-threads.test.ts
```
All 7 tests pass, including 2 new tests for ThreadLabel.
<!-- greptile_comment -->
<h2>Greptile Overview</h2>
<h3>Greptile Summary</h3>
This PR fixes three related Telegram DM-topic regressions: (1) adds a `ThreadLabel` payload field so DM topic sessions can be distinguished in the Sessions UI, (2) ensures URL settings (including `?session=`) are applied before `syncTabWithLocation()` overwrites state from localStorage, and (3) restores correct DM-thread outbound routing by using a DM-specific thread id (`message_thread_id`) for typing indicators and dispatch context when `is_forum` is absent (private chats never set `is_forum=true`).
The changes integrate into the existing Telegram context builder (`buildTelegramMessageContext`) by adjusting how thread IDs are computed and emitted, and into the UI lifecycle by reordering initialization to preserve URL-driven navigation.
<h3>Confidence Score: 4/5</h3>
- This PR is generally safe to merge, with one edge-case routing concern around non-group non-DM chat types.
- Changes are localized and covered by targeted tests for DM thread session keys/labels; the main remaining risk is that `effectiveThreadId` is applied to all non-group chats (e.g., channels), which may not support `message_thread_id` semantics and could cause misrouting or Telegram API errors.
- src/telegram/bot-message-context.ts
<!-- greptile_other_comments_section -->
<!-- /greptile_comment -->
---
lobster-biscuit
Most Similar PRs
#19213: Telegram: preserve DM topic thread in direct replies
by Kemalau · 2026-02-17
88.7%
#7261: fix(telegram): preserve DM topic thread id for outbound media
by ViffyGwaanl · 2026-02-02
87.5%
#17952: fix(telegram): support message_thread_id for DM topics
by timbrinded · 2026-02-16
86.8%
#6192: Telegram: fix DM Topics thread routing
by ViffyGwaanl · 2026-02-01
86.2%
#14443: fix(telegram): skip General topic thread ID for all chat types (#14...
by lailoo · 2026-02-12
86.1%
#17968: fix(telegram): restore DM topic thread ids with send-path fallback
by Leonccaa · 2026-02-16
86.0%
#12936: fix(telegram): omit message_thread_id for private DM chats
by omair445 · 2026-02-09
85.5%
#13580: fix(telegram): skip updateLastRoute when dmScope isolates DM sessions
by lailoo · 2026-02-10
84.1%
#6457: fix(telegram): register commands for group scope + preserve topic t...
by dae-sun · 2026-02-01
81.5%
#19050: fix(telegram): skip message_thread_id for private chats to prevent ...
by Limitless2023 · 2026-02-17
80.8%