Skip to content

fix(telegram): preserve original filename for inbound document/audio/video#31837

Merged
steipete merged 1 commit intoopenclaw:mainfrom
Kay-051:fix/telegram-preserve-original-filename
Mar 2, 2026
Merged

fix(telegram): preserve original filename for inbound document/audio/video#31837
steipete merged 1 commit intoopenclaw:mainfrom
Kay-051:fix/telegram-preserve-original-filename

Conversation

@Kay-051
Copy link
Contributor

@Kay-051 Kay-051 commented Mar 2, 2026

Summary

  • Problem: When users send documents, audio, or video through Telegram, the original filename (e.g. business-plan.pdf) is lost and replaced by the Telegram server-side name (e.g. file_42.pdf).
  • Why it matters: Downstream tools, agents, and users rely on meaningful filenames to understand file content. Losing the original name degrades UX and can break filename-dependent workflows.
  • What changed: downloadAndSaveTelegramFile now accepts an optional telegramFileName parameter. resolveMedia extracts file_name from msg.document, msg.audio, msg.video, and msg.animation, and passes it through. The resolution order is: Telegram metadata → fetch response → server path.
  • What did NOT change (scope boundary): No changes to fetchRemoteMedia, saveMediaBuffer, or any other media pipeline. Photo handling is unchanged (photos don't carry file_name).

Change Type (select all)

  • Bug fix

Scope (select all touched areas)

  • Integrations

Linked Issue/PR

User-visible / Behavior Changes

Inbound Telegram documents, audio files, videos, and animations now retain their original filename when saved locally, instead of being renamed to the Telegram server-side path.

Security Impact (required)

  • New permissions/capabilities? No
  • Secrets/tokens handling changed? No
  • New/changed network calls? No
  • Command/tool execution surface changed? No
  • Data access scope changed? No

Repro + Verification

Environment

  • OS: macOS / Linux
  • Runtime/container: Node.js
  • Integration/channel: Telegram

Steps

  1. Send a document named business-plan.pdf to the bot via Telegram
  2. Check the saved file on disk

Expected

  • File is saved with original name business-plan.pdf (or a sanitized variant)

Actual (before fix)

  • File is saved as file_42.pdf (Telegram server-side name)

Evidence

  • Failing test/log before + passing after
  • 5 new test cases added covering document, audio, video, and fallback scenarios
  • All 16 tests in delivery.resolve-media-retry.test.ts pass

Human Verification (required)

  • Verified scenarios: Ran all 16 tests (11 existing + 5 new) via pnpm test -- --run src/telegram/bot/delivery.resolve-media-retry.test.ts — all pass
  • Edge cases checked: Fallback when file_name is absent (uses fetched.fileName); fallback when both are absent (uses filePath); each media type (document, audio, video) tested individually
  • What I did not verify: Live Telegram bot end-to-end (no test instance available)

Compatibility / Migration

  • Backward compatible? Yes
  • Config/env changes? No
  • Migration needed? No

Failure Recovery (if this breaks)

  • How to disable/revert this change quickly: Revert the single commit; the telegramFileName parameter is optional and defaults to the previous behavior
  • Files/config to restore: src/telegram/bot/delivery.resolve-media.ts
  • Known bad symptoms reviewers should watch for: Filenames appearing as undefined in saved media

Risks and Mitigations

  • Risk: A Telegram file_name could contain path separators or special characters
    • Mitigation: saveMediaBuffer already sanitizes filenames downstream; this change only passes the name through

🤖 AI-assisted (Claude) · Fully tested · I understand what this code does.

…o/video uploads

The downloadAndSaveTelegramFile inner function only used the server-side
file path (e.g. "documents/file_42.pdf") or the Content-Disposition
header (which Telegram doesn't send) to derive the saved filename.
The original filename provided by Telegram via msg.document.file_name,
msg.audio.file_name, msg.video.file_name, and msg.animation.file_name
was never passed through, causing all inbound files to lose their
user-provided names.

Now downloadAndSaveTelegramFile accepts an optional telegramFileName
parameter that takes priority over the fetched/server-side name.
The resolveMedia call site extracts the original name from the message
and passes it through.

Closes openclaw#31768

Made-with: Cursor
@openclaw-barnacle openclaw-barnacle bot added channel: telegram Channel integration: telegram size: S labels Mar 2, 2026
@greptile-apps
Copy link
Contributor

greptile-apps bot commented Mar 2, 2026

Greptile Summary

Preserves original Telegram filenames for document/audio/video attachments by passing file_name metadata through the download pipeline.

Key changes:

  • Added optional telegramFileName parameter to downloadAndSaveTelegramFile with fallback chain: Telegram metadata → fetch response → server path
  • Extracts file_name from msg.document, msg.audio, msg.video, and msg.animation
  • 5 new test cases verify the feature and fallback behavior

Verified:

  • Filename sanitization handled by existing saveMediaBuffer logic (removes unsafe characters, limits length to 60 chars)
  • Backward compatible (optional parameter maintains existing behavior when metadata absent)
  • Photos, stickers, and voice messages correctly excluded (Telegram API doesn't provide file_name for these types)

Confidence Score: 5/5

  • Safe to merge with minimal risk - focused bug fix with comprehensive test coverage
  • Clean implementation with proper fallback handling, existing sanitization prevents security issues, backward compatible, and 5 new tests verify behavior thoroughly
  • No files require special attention

Last reviewed commit: ee4832c

@steipete steipete merged commit da05395 into openclaw:main Mar 2, 2026
27 of 29 checks passed
@steipete
Copy link
Contributor

steipete commented Mar 2, 2026

Landed via temp rebase onto main.

  • Gate: pnpm -s vitest run src/telegram/bot/delivery.resolve-media-retry.test.ts
  • Land commit: 63a97de
  • Merge commit: da05395

Thanks @Kay-051!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

channel: telegram Channel integration: telegram size: S

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: Telegram inbound documents ignore msg.document.file_name — original filename lost

2 participants