#16548: fix(telegram): enhance chat_id validation and diagnostics
channel: telegram
agents
stale
size: L
Cluster:
Messaging Platform Improvements
Fixes #15206. Adds specific error messages when attempting to send messages to private users via @username, which is not supported by the Telegram Bot API. Also adds defensive checks for non-numeric chat IDs in the message context builder. Includes a new regression test file.
<!-- greptile_comment -->
<h3>Greptile Summary</h3>
This PR enhances Telegram `chat_id` validation and error diagnostics. The changes add specific error messages when attempting to send messages to private users via `@username` (which Telegram Bot API doesn't support), tighten the username regex to be more accurate (usernames must start with a letter), and add defensive logging for non-numeric chat IDs in the message context builder. Includes comprehensive test coverage for the new validation logic.
**Key changes:**
- Refactored `wrapChatNotFoundError()` to provide clearer error messages, specifically calling out the `@username` limitation for private users
- Tightened `normalizeChatId()` regex to properly distinguish numeric IDs from usernames (usernames must start with a letter, not a digit)
- Added diagnostic logging in `buildTelegramMessageContext()` for non-numeric or unsafe integer `chat_id` values
- Added comprehensive regression test file `send.chat-id-validation.test.ts` covering various chat ID formats and error scenarios
- Moved `sendWithThreadFallback()` to module scope to eliminate duplication across functions
- Added new workflow documentation in `.agent/workflows/issue_resolution.md`
<h3>Confidence Score: 3/5</h3>
- This PR is mostly safe but has a known test issue that was already flagged
- The code changes are solid and improve error handling, but the test file is missing required mocks (as noted in previous review threads). The test will likely fail when `sendMessageTelegram` tries to call unmocked functions like `recordChannelActivity`, `loadWebMedia`, or other infra utilities. The core logic changes are good, but the incomplete test setup reduces confidence that the changes are fully validated.
- Pay close attention to `src/telegram/send.chat-id-validation.test.ts` - it needs additional mocks before it can pass
<sub>Last reviewed commit: 2338147</sub>
<!-- greptile_other_comments_section -->
<!-- /greptile_comment -->
Most Similar PRs
#9734: fix(telegram): correct sender identification for channel messages (...
by divol89 · 2026-02-05
82.9%
#12936: fix(telegram): omit message_thread_id for private DM chats
by omair445 · 2026-02-09
82.9%
#19050: fix(telegram): skip message_thread_id for private chats to prevent ...
by Limitless2023 · 2026-02-17
82.2%
#7261: fix(telegram): preserve DM topic thread id for outbound media
by ViffyGwaanl · 2026-02-02
82.2%
#14443: fix(telegram): skip General topic thread ID for all chat types (#14...
by lailoo · 2026-02-12
82.0%
#17432: fix(telegram): skip message_thread_id for private chats in sticker/...
by clawinho · 2026-02-15
81.6%
#20236: fix(telegram): make reaction handling soft-fail and message-id resi...
by PeterShanxin · 2026-02-18
80.9%
#19213: Telegram: preserve DM topic thread in direct replies
by Kemalau · 2026-02-17
79.8%
#18533: fix(telegram): handle 'file is too big' error for large files
by MisterGuy420 · 2026-02-16
79.4%
#3186: fix(telegram): sanitize update offset + lock polling
by daxiong888 · 2026-01-28
79.1%