#8368: fix(telegram): preserve forwarded message metadata during debounce batching
channel: telegram
stale
Cluster:
Network Error Handling Improvements
## Problem
When multiple Telegram messages arrive within the `messages.inbound.debounceMs` window and get batched, forward metadata (`forward_origin`, `forward_from`, `forward_from_chat`, etc.) is lost for all messages except the first one.
### Root Cause
In `bot-handlers.ts`, the `onFlush` callback builds a `syntheticMessage` by spreading only `first.msg`, so forward metadata from `entries[1..n]` is discarded.
## Fix
- **Extract shared `buildForwardPrefix()` helper** into `bot/helpers.ts` — avoids format duplication between `bot-message-context.ts` and `bot-handlers.ts`
- **Inline forward attribution per entry:** Before joining texts, each entry is checked for forward metadata via `normalizeForwardedContext()`. If present, a `[Forwarded from ...]` prefix is prepended (including forwarded messages without text/caption).
- **Strip forward fields from synthetic message:** Forward fields are destructured out of `first.msg` to prevent `bot-message-context.ts` from double-prefixing the first entry.
## Testing
Forward 3+ messages from different senders quickly (within debounce window). Before this fix, only the first message retained its `[Forwarded from ...]` header. After the fix, all messages preserve their attribution.
Relates to #6263
<!-- greptile_comment -->
<h2>Greptile Overview</h2>
<h3>Greptile Summary</h3>
This PR fixes Telegram inbound debounce batching so forwarded-message attribution is preserved for each message in a batch instead of only the first one. It does this by extracting a shared `buildForwardPrefix()` helper into `src/telegram/bot/helpers.ts`, using `normalizeForwardedContext()` per entry when composing the batched `combinedText`, and stripping `forward_*` fields from the synthetic message to avoid double-prefixing when the synthetic message is later processed by `buildTelegramMessageContext()`.
These changes fit into the existing Telegram pipeline by keeping the synthetic-message approach (batched messages are still processed as a single `TelegramMessage`), while ensuring the user-visible forward attribution survives batching.
<h3>Confidence Score: 4/5</h3>
- This PR is likely safe to merge and should fix the reported forwarded-metadata loss in debounced Telegram batches.
- Changes are narrowly scoped to text composition and forward-prefix formatting, and they intentionally strip forward fields from the synthetic message to avoid double-prefixing. Remaining concerns are mainly about helper ergonomics (newline handling) and ensuring debounce batching invariants hold.
- src/telegram/bot-handlers.ts; src/telegram/bot/helpers.ts
<!-- greptile_other_comments_section -->
<!-- /greptile_comment -->
Most Similar PRs
#17435: fix(debounce): retry flush with exponential backoff to prevent sile...
by widingmarcus-cyber · 2026-02-15
79.8%
#15985: fix(telegram): defer buffer deletion until processing succeeds
by coygeek · 2026-02-14
78.5%
#11347: fix: scope Telegram update offset to bot token
by anooprdawar · 2026-02-07
77.6%
#7261: fix(telegram): preserve DM topic thread id for outbound media
by ViffyGwaanl · 2026-02-02
77.0%
#15467: feat(messages): add debounceMedia option for inbound debouncing
by tangcruz · 2026-02-13
76.6%
#9734: fix(telegram): correct sender identification for channel messages (...
by divol89 · 2026-02-05
76.2%
#8166: fix(telegram): lifecycle fixes for duplicate messages and auto-reco...
by cheenu1092-oss · 2026-02-03
76.0%
#19673: fix(telegram): avoid starting streaming replies with only 1-2 words
by emanuelst · 2026-02-18
75.8%
#17243: fix(telegram): catch getFile network failures to prevent gateway cr...
by robbyczgw-cla · 2026-02-15
75.7%
#21276: fix(telegram): stabilize partial finalization and MEDIA dedupe (AI-...
by HOYALIM · 2026-02-19
75.7%