diff --git a/src/routes/files.ts b/src/routes/files.ts index 401899c..c1234a8 100644 --- a/src/routes/files.ts +++ b/src/routes/files.ts @@ -5,7 +5,7 @@ import { findFileByPublicId } from '../db/files'; import { fileInfoCache } from '../utils/cache'; import { formatCreatedAt, getErrorMessage } from '../utils/file'; import logger from '../utils/logger'; -import { getBot } from '../utils/telegram'; +import { getFileInfo } from '../utils/telegram'; import { locateZipEntry } from '../utils/zip'; type RequestWithParams = Request & { @@ -19,13 +19,7 @@ const getTelegramFileInfo = async (telegramFileId: string, public_id: string) => let fileInfo = fileInfoCache.get(cacheKey); if (!fileInfo) { - const bot = getBot(); - const apiFileInfo = await bot.telegram.getFile(telegramFileId); - fileInfo = { - file_size: (apiFileInfo as any).file_size || 0, - mime_type: (apiFileInfo as any).mime_type || 'application/octet-stream', - file_path: (apiFileInfo as any).file_path || '', - }; + fileInfo = await getFileInfo(telegramFileId); fileInfoCache.set(cacheKey, fileInfo); logger.debug('File info cached', { public_id, cacheKey }); } else { @@ -35,8 +29,8 @@ const getTelegramFileInfo = async (telegramFileId: string, public_id: string) => return fileInfo; }; -const buildTelegramFileUrl = (filePath: string): string => - `https://api.telegram.org/file/bot${process.env.BOT_TOKEN}/${filePath}`; +const buildTelegramFileUrl = (filePath: string, botToken: string): string => + `https://api.telegram.org/file/bot${botToken}/${filePath}`; const cleanupTempFile = async (tempPath: string): Promise => { try { @@ -69,7 +63,9 @@ export const handleFileRedirect = async (req: RequestWithParams): Promise(3600); // Cleanup expired cache entries every 5 minutes diff --git a/src/utils/telegram.ts b/src/utils/telegram.ts index ff892b1..6ed37ef 100644 --- a/src/utils/telegram.ts +++ b/src/utils/telegram.ts @@ -5,6 +5,11 @@ import { enqueueUpload } from './telegramQueue'; const botTokens = Array.from(new Set([config.botToken, ...config.additionalBotTokens])); +type FileInfoResult = { + result: unknown; + botToken: string; +}; + const bots = botTokens.map((token) => new Telegraf(token)); let nextBotIndex = 0; @@ -20,14 +25,15 @@ const sleep = (seconds: number): Promise => { }; const executeWithBotRetry = async ( - action: (botInstance: Telegraf) => Promise, + action: (botInstance: Telegraf, botToken: string) => Promise, retries = 5, attemptedBots = 0, ): Promise => { const botIndex = claimBotIndex(); const currentBot = bots[botIndex]; + const currentToken = botTokens[botIndex]; try { - return await action(currentBot); + return await action(currentBot, currentToken); } catch (error: unknown) { const errorStr = error instanceof Error ? error.message : String(error); const match = errorStr.match(/retry after (\d+)/i); @@ -62,10 +68,11 @@ interface ForwardResult { storageMessageId: number; } -interface TelegramFileInfo { +export interface TelegramFileInfo { file_size: number; mime_type: string; file_path: string; + bot_token: string; } interface UploadedTelegramFile { @@ -94,7 +101,7 @@ type SendMethod = ( payload?: SendPayload, ) => Promise; -const sendMethodMap: Record = { +const sendMethodMap: Record = { photo: 'sendPhoto', audio: 'sendAudio', video: 'sendVideo', @@ -235,8 +242,11 @@ export const forwardMediaGroupToStorage = async ( export const getFileInfo = async (telegramFileId: string): Promise => { try { - const result = await executeWithBotRetry((activeBot) => - activeBot.telegram.getFile(telegramFileId), + const { result, botToken } = await executeWithBotRetry( + async (activeBot, activeToken) => ({ + result: await activeBot.telegram.getFile(telegramFileId), + botToken: activeToken, + }), ); const fileData = result as unknown as TelegramFileInfo; @@ -244,6 +254,7 @@ export const getFileInfo = async (telegramFileId: string): Promise