← Back to PRs

#8368: fix(telegram): preserve forwarded message metadata during debounce batching

by PatrickBauer open 2026-02-03 23:30 View on GitHub →
channel: telegram stale
## 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