Skip to content

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

@moonbees01

Description

@moonbees01

Summary

When a Telegram user sends a document, OpenClaw's resolveMedia() function has msg.document.file_name (the original filename from the Telegram Bot API) available but never uses it, causing the original filename to be permanently lost.

Root Cause (Code Analysis)

File: dist/pi-embedded-CQnl8oWA.jsresolveMedia()

// ctx.getFile() returns the server-side path only — not the original filename
file = await ctx.getFile();
// file.file_path = "documents/file_<server-id>.pdf"  <- server-side path, NOT original

// msg.document.file_name is available here but never read
const saved = await downloadAndSaveTelegramFile(file.file_path, fetchImpl);
//   msg.document?.file_name is ignored

File: dist/local-roots-BHi_eOQs.jsfetchRemoteMedia()

const headerFileName = parseContentDispositionFileName(res.headers.get("content-disposition"));
// -> null: Telegram's download server sends no Content-Disposition header

let fileName = headerFileName || fileNameFromUrl || path.basename(filePathHint);
// fileNameFromUrl = path.basename(".../documents/file_<server-id>.pdf")
// -> "file_<server-id>.pdf"  <- server-side name, not the original

File: dist/store-Q3603hr3.jssaveMediaBuffer()

// originalFilename = "file_<server-id>.pdf" (server-side name wrongly treated as original)
const base = path.parse(originalFilename).name;  // -> "file_<server-id>"
const sanitized = sanitizeFilename(base);
id = `${sanitized}---${uuid}${ext}`;
// Final saved name: "file_<server-id>---<new-uuid>.pdf"
// Double-UUID pattern — original name (e.g., "business-plan.docx") is gone

Expected Behavior

When a user sends a file named business-plan.docx, the file should be saved as business-plan---<uuid>.docx, preserving the original name with a UUID suffix for uniqueness.

Actual Behavior

The saved filename is derived from the Telegram server-side path, resulting in a double-UUID pattern like file_<server-id>---<new-uuid>.pdf. The user's original filename is permanently lost.

Minimal Fix

// Step 1: Update downloadAndSaveTelegramFile to accept an optional name override
const downloadAndSaveTelegramFile = async (filePath, fetchImpl, telegramFileName) => {
    const fetched = await fetchRemoteMedia({ ... });
    const originalName = telegramFileName ?? fetched.fileName ?? filePath;
    return saveMediaBuffer(fetched.buffer, fetched.contentType, "inbound", maxBytes, originalName);
};

// Step 2: Pass msg.document.file_name at the call site
const originalFileName = msg.document?.file_name ?? msg.audio?.file_name ?? msg.video?.file_name;
const saved = await downloadAndSaveTelegramFile(file.file_path, fetchImpl, originalFileName);

Impact

  • Agent cannot identify files by their original name when multiple documents are sent
  • Breaks workflows where filename carries semantic meaning (numbered annexes, versioned reports, etc.)
  • User must manually re-specify filenames, adding friction to every document-based task

Environment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions