#19108: fix(telegram): fix send failure in closed forum topics
channel: telegram
channel: voice-call
gateway
size: S
trusted-contributor
Cluster:
Messaging Platform Improvements
When sending to a closed forum topic, Telegram returns a 'topic closed' error.
Fix: Detect the error and attempt to reopen the topic before retrying the send. Falls through to existing thread-not-found fallback if reopen fails.
Recreated from #18149 with only relevant files (telegram send + new test).
<!-- greptile_comment -->
<h3>Greptile Summary</h3>
Adds automatic recovery for closed forum topics in Telegram by detecting "topic closed" errors and attempting to reopen the topic before retrying the send operation. The fix extends the existing `withTelegramThreadFallback` helper to handle this error case before falling back to the existing thread-not-found retry logic.
**Key changes:**
- Added `TOPIC_CLOSED_RE` regex pattern and `isTelegramTopicClosedError` helper to detect closed topic errors
- Added `getMessageThreadId` helper to extract thread ID from params for reopen operation
- Extended `withTelegramThreadFallback` to accept optional `recoverOptions` with `chatId` and `api` for calling `reopenForumTopic`
- Applied recovery logic consistently across all 3 call sites: `sendMessageTelegram`, `sendStickerTelegram`, and `sendPollTelegram`
- Comprehensive test coverage for the reopen logic in new test file
**Issue found:**
- `getMessageThreadId` doesn't validate `Number.parseInt` result, which could return `NaN` for malformed string inputs
<h3>Confidence Score: 4/5</h3>
- This PR is safe to merge with minimal risk once the NaN validation issue is addressed
- The implementation follows existing patterns in the codebase, includes comprehensive test coverage, and consistently applies the fix across all relevant call sites. The recovery logic is properly scoped to only handle topic-closed errors and falls through to existing error handling. One logical issue exists with `Number.parseInt` not validating for `NaN`, which could cause the reopen attempt to be skipped silently for malformed thread IDs.
- Pay attention to `src/telegram/send.ts:376-377` where `Number.parseInt` needs `NaN` validation
<sub>Last reviewed commit: 72079a4</sub>
<!-- greptile_other_comments_section -->
<!-- /greptile_comment -->
Most Similar PRs
#11653: fix(telegram): retry without message_thread_id on stale forum threa...
by liuxiaopai-ai · 2026-02-08
78.5%
#19213: Telegram: preserve DM topic thread in direct replies
by Kemalau · 2026-02-17
78.3%
#12870: fix: recover from telegram fetch errors (issue #12835)
by ambicuity · 2026-02-09
78.0%
#14443: fix(telegram): skip General topic thread ID for all chat types (#14...
by lailoo · 2026-02-12
77.7%
#7261: fix(telegram): preserve DM topic thread id for outbound media
by ViffyGwaanl · 2026-02-02
77.3%
#17968: fix(telegram): restore DM topic thread ids with send-path fallback
by Leonccaa · 2026-02-16
77.2%
#16548: fix(telegram): enhance chat_id validation and diagnostics
by tanujbhaud · 2026-02-14
77.1%
#12936: fix(telegram): omit message_thread_id for private DM chats
by omair445 · 2026-02-09
76.4%
#17432: fix(telegram): skip message_thread_id for private chats in sticker/...
by clawinho · 2026-02-15
76.3%
#7141: fix(telegram): unify network error detection to prevent poll crashes
by hclsys · 2026-02-02
76.2%