{"record":{"id":"c976adac9535cbf2","repo":"Mintplex-Labs/anything-llm","slug":"failed-to-download-file-from-telegram","errorCode":null,"errorMessage":"Failed to download file from Telegram","messagePattern":"Failed to download file from Telegram","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"server/utils/telegramBot/utils/media.js","lineNumber":12,"sourceCode":"const { getAudioFileInfo } = require(\"../../TextToSpeech/audioFormat\");\n\n/**\n * Download a file from Telegram by file ID.\n * @param {TelegramBot} bot\n * @param {string} fileId\n * @returns {Promise<Buffer>}\n */\nasync function downloadTelegramFile(bot, fileId) {\n  const fileLink = await bot.getFileLink(fileId);\n  const response = await fetch(fileLink);\n  if (!response.ok) throw new Error(\"Failed to download file from Telegram\");\n  return Buffer.from(await response.arrayBuffer());\n}\n\n/**\n * Get appropriate file extension from MIME type.\n * @param {string} mimeType\n * @returns {string}\n */\nfunction getExtensionFromMime(mimeType) {\n  const mimeToExt = {\n    \"audio/ogg\": \".ogg\",\n    \"audio/oga\": \".ogg\",\n    \"audio/opus\": \".opus\",\n    \"audio/mp3\": \".mp3\",\n    \"audio/mpeg\": \".mp3\",\n    \"audio/wav\": \".wav\",\n    \"audio/x-wav\": \".wav\",\n    \"audio/mp4\": \".m4a\",","sourceCodeStart":1,"sourceCodeEnd":30,"githubUrl":"https://github.com/Mintplex-Labs/anything-llm/blob/526360e320da9d1b36074be5ed64fe76e5bbfbbd/server/utils/telegramBot/utils/media.js#L1-L30","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Retry the download after a short backoff (Telegram links are eventually consistent).","Verify the bot token is current and the bot can still access the file via getFile.","If the fileId is older than Telegram's retention (typically hours), tell the user to resend the file.","Wrap in try/catch and reply to the user with a 'please resend' message instead of failing the chat."],"exampleFix":"// before\nconst buf = await downloadTelegramFile(bot, fileId); // one-shot, throws on 5xx\n\n// after\nlet buf;\nfor (let attempt = 0; attempt < 3; attempt++) {\n  try { buf = await downloadTelegramFile(bot, fileId); break; }\n  catch (e) { await sleep(2 ** attempt * 500); }\n}\nif (!buf) return ctx.reply('Could not fetch the file, please resend it.');","handlingStrategy":"retry","validationCode":"async function safeDownloadTelegramFile(bot, fileId, attempts = 3) {\n  for (let i = 0; i < attempts; i++) {\n    try { return await downloadTelegramFile(bot, fileId); }\n    catch (e) { if (i === attempts - 1) throw e; await new Promise(r => setTimeout(r, 2 ** i * 500)); }\n  }\n}","typeGuard":null,"tryCatchPattern":"try {\n  const buf = await downloadTelegramFile(bot, fileId);\n} catch (e) {\n  if (e.message === 'Failed to download file from Telegram')\n    return ctx.reply('Could not fetch the file, please resend it.');\n  throw e;\n}","preventionTips":["Process Telegram files soon after arrival (links expire).","Verify the bot token is current and has getFile access.","Retry with exponential backoff for transient Telegram 5xx/429."],"tags":["telegram","network","retry","bot"],"backgroundTag":null,"analyzedSha":"526360e320da9d1b36074be5ed64fe76e5bbfbbd","analyzedAt":"2026-08-13T01:45:47.170Z","schemaVersion":2},"datasetVersion":"2026-08-13T04:17:16.726Z"}