fix: upload binary files to Telegram with source payload
This commit is contained in:
@@ -24,17 +24,17 @@ export const forwardToStorage = async (
|
|||||||
): Promise<ForwardResult> => {
|
): Promise<ForwardResult> => {
|
||||||
try {
|
try {
|
||||||
const caption = forceDocument ? `📁 ${fileName}` : fileName;
|
const caption = forceDocument ? `📁 ${fileName}` : fileName;
|
||||||
const input: any = forceDocument
|
const filePayload = { source: fileChunk, filename: fileName };
|
||||||
? { document: fileChunk, caption }
|
const result: any = forceDocument
|
||||||
: { photo: [fileChunk], caption };
|
? await bot.telegram.sendDocument(config.storageChatId, filePayload, { caption })
|
||||||
|
: await bot.telegram.sendPhoto(config.storageChatId, filePayload, { caption });
|
||||||
const result = await bot.telegram.sendPhoto(config.storageChatId, input);
|
const uploadedFile = forceDocument ? result.document : result.photo?.slice(-1)[0];
|
||||||
|
|
||||||
logger.info('File forwarded to storage', { fileName, message: result.message_id });
|
logger.info('File forwarded to storage', { fileName, message: result.message_id });
|
||||||
|
|
||||||
return {
|
return {
|
||||||
telegramFileId: result.photo?.slice(-1)[0]?.file_id || '',
|
telegramFileId: uploadedFile?.file_id || '',
|
||||||
telegramFileUniqueId: result.photo?.slice(-1)[0]?.file_unique_id || '',
|
telegramFileUniqueId: uploadedFile?.file_unique_id || '',
|
||||||
storageMessageId: result.message_id,
|
storageMessageId: result.message_id,
|
||||||
};
|
};
|
||||||
} catch (error: any) {
|
} catch (error: any) {
|
||||||
|
|||||||
@@ -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 () => {
|
it('should handle error when forwarding fails', async () => {
|
||||||
const bot = getBot();
|
const bot = getBot();
|
||||||
bot.telegram.sendPhoto = mock(() => Promise.reject(new Error('Telegram send failed')));
|
bot.telegram.sendPhoto = mock(() => Promise.reject(new Error('Telegram send failed')));
|
||||||
|
|||||||
Reference in New Issue
Block a user