Mintplex-Labs/anything-llm · error · Error

Failed to download file from Telegram

Error message

Failed to download file from Telegram

What it means

Thrown by downloadTelegramFile() when the fetch of the Telegram file link returns a non-OK HTTP status (response.ok is false). The file link is obtained from bot.getFileLink(fileId); a failure here usually means the fileId is stale/expired, Telegram rate-limited the request, or the bot token lacks access. The error is raised before reading the body.

Source

Thrown at server/utils/telegramBot/utils/media.js:12

const { getAudioFileInfo } = require("../../TextToSpeech/audioFormat");

/**
 * Download a file from Telegram by file ID.
 * @param {TelegramBot} bot
 * @param {string} fileId
 * @returns {Promise<Buffer>}
 */
async function downloadTelegramFile(bot, fileId) {
  const fileLink = await bot.getFileLink(fileId);
  const response = await fetch(fileLink);
  if (!response.ok) throw new Error("Failed to download file from Telegram");
  return Buffer.from(await response.arrayBuffer());
}

/**
 * Get appropriate file extension from MIME type.
 * @param {string} mimeType
 * @returns {string}
 */
function getExtensionFromMime(mimeType) {
  const mimeToExt = {
    "audio/ogg": ".ogg",
    "audio/oga": ".ogg",
    "audio/opus": ".opus",
    "audio/mp3": ".mp3",
    "audio/mpeg": ".mp3",
    "audio/wav": ".wav",
    "audio/x-wav": ".wav",
    "audio/mp4": ".m4a",

View on GitHub (pinned to 526360e320)

Solutions

  1. Retry the download after a short backoff (Telegram links are eventually consistent).
  2. Verify the bot token is current and the bot can still access the file via getFile.
  3. If the fileId is older than Telegram's retention (typically hours), tell the user to resend the file.
  4. Wrap in try/catch and reply to the user with a 'please resend' message instead of failing the chat.

Example fix

// before
const buf = await downloadTelegramFile(bot, fileId); // one-shot, throws on 5xx

// after
let buf;
for (let attempt = 0; attempt < 3; attempt++) {
  try { buf = await downloadTelegramFile(bot, fileId); break; }
  catch (e) { await sleep(2 ** attempt * 500); }
}
if (!buf) return ctx.reply('Could not fetch the file, please resend it.');
Defensive patterns

Strategy: retry

Validate before calling

async function safeDownloadTelegramFile(bot, fileId, attempts = 3) {
  for (let i = 0; i < attempts; i++) {
    try { return await downloadTelegramFile(bot, fileId); }
    catch (e) { if (i === attempts - 1) throw e; await new Promise(r => setTimeout(r, 2 ** i * 500)); }
  }
}

Try / catch

try {
  const buf = await downloadTelegramFile(bot, fileId);
} catch (e) {
  if (e.message === 'Failed to download file from Telegram')
    return ctx.reply('Could not fetch the file, please resend it.');
  throw e;
}

Prevention

When it happens

Trigger: A Telegram voice/photo/document message whose fileId has expired (Telegram file links are temporary); Telegram returning 429 (rate limit) or 401/403 (bot token revoked); a network blip producing a 5xx.

Common situations: Processing a message long after it was sent (link TTL elapsed); high-volume bot hitting Telegram rate limits; bot token regenerated but old token cached; regional Telegram outage.

Related errors


AI-assisted analysis of Mintplex-Labs/anything-llm@526360e320 (2026-08-13). Data as JSON: /api/errors/c976adac9535cbf2. Report an issue: GitHub.