fix: upload binary files to Telegram with source payload

This commit is contained in:
MythEclipse
2026-05-18 08:18:55 +07:00
parent 9aedc59b69
commit 91986d1d51
2 changed files with 35 additions and 7 deletions
+7 -7
View File
@@ -24,17 +24,17 @@ export const forwardToStorage = async (
): Promise<ForwardResult> => {
try {
const caption = forceDocument ? `📁 ${fileName}` : fileName;
const input: any = forceDocument
? { document: fileChunk, caption }
: { photo: [fileChunk], caption };
const result = await bot.telegram.sendPhoto(config.storageChatId, input);
const filePayload = { source: fileChunk, filename: fileName };
const result: any = forceDocument
? await bot.telegram.sendDocument(config.storageChatId, filePayload, { caption })
: await bot.telegram.sendPhoto(config.storageChatId, filePayload, { caption });
const uploadedFile = forceDocument ? result.document : result.photo?.slice(-1)[0];
logger.info('File forwarded to storage', { fileName, message: result.message_id });
return {
telegramFileId: result.photo?.slice(-1)[0]?.file_id || '',
telegramFileUniqueId: result.photo?.slice(-1)[0]?.file_unique_id || '',
telegramFileId: uploadedFile?.file_id || '',
telegramFileUniqueId: uploadedFile?.file_unique_id || '',
storageMessageId: result.message_id,
};
} catch (error: any) {
+28
View File
@@ -18,6 +18,15 @@ mock.module('telegraf', () => {
],
}),
),
sendDocument: mock(() =>
Promise.resolve({
message_id: 54321,
document: {
file_id: 'document_id',
file_unique_id: 'document_unique_id',
},
}),
),
};
}
},
@@ -71,6 +80,25 @@ describe('Telegram API Utilities', () => {
});
});
it('should forward documents with source and filename payload', async () => {
const bot = getBot();
const chunk = Buffer.from('fake document data');
const fileName = 'document.pdf';
const result = await forwardToStorage(chunk, fileName, true);
expect(bot.telegram.sendDocument).toHaveBeenCalledWith(
-1003996572954,
{ source: chunk, filename: fileName },
{ caption: `📁 ${fileName}` },
);
expect(result).toEqual({
telegramFileId: 'document_id',
telegramFileUniqueId: 'document_unique_id',
storageMessageId: 54321,
});
});
it('should handle error when forwarding fails', async () => {
const bot = getBot();
bot.telegram.sendPhoto = mock(() => Promise.reject(new Error('Telegram send failed')));