diff --git a/src/telegram/bot/delivery.ts b/src/telegram/bot/delivery.ts index a70e554f38d..4e4fe670601 100644 --- a/src/telegram/bot/delivery.ts +++ b/src/telegram/bot/delivery.ts @@ -309,6 +309,16 @@ export async function resolveMedia( stickerMetadata?: StickerMetadata; } | null> { const msg = ctx.message; + const downloadAndSaveTelegramFile = async (filePath: string, fetchImpl: typeof fetch) => { + const url = `https://api.telegram.org/file/bot${token}/${filePath}`; + const fetched = await fetchRemoteMedia({ + url, + fetchImpl, + filePathHint: filePath, + }); + const originalName = fetched.fileName ?? filePath; + return saveMediaBuffer(fetched.buffer, fetched.contentType, "inbound", maxBytes, originalName); + }; // Handle stickers separately - only static stickers (WEBP) are supported if (msg.sticker) { @@ -333,20 +343,7 @@ export async function resolveMedia( logVerbose("telegram: fetch not available for sticker download"); return null; } - const url = `https://api.telegram.org/file/bot${token}/${file.file_path}`; - const fetched = await fetchRemoteMedia({ - url, - fetchImpl, - filePathHint: file.file_path, - }); - const originalName = fetched.fileName ?? file.file_path; - const saved = await saveMediaBuffer( - fetched.buffer, - fetched.contentType, - "inbound", - maxBytes, - originalName, - ); + const saved = await downloadAndSaveTelegramFile(file.file_path, fetchImpl); // Check sticker cache for existing description const cached = sticker.file_unique_id ? getCachedSticker(sticker.file_unique_id) : null; @@ -431,20 +428,7 @@ export async function resolveMedia( if (!fetchImpl) { throw new Error("fetch is not available; set channels.telegram.proxy in config"); } - const url = `https://api.telegram.org/file/bot${token}/${file.file_path}`; - const fetched = await fetchRemoteMedia({ - url, - fetchImpl, - filePathHint: file.file_path, - }); - const originalName = fetched.fileName ?? file.file_path; - const saved = await saveMediaBuffer( - fetched.buffer, - fetched.contentType, - "inbound", - maxBytes, - originalName, - ); + const saved = await downloadAndSaveTelegramFile(file.file_path, fetchImpl); let placeholder = ""; if (msg.photo) { placeholder = "";