2026-05-18 07:06:21 +07:00
|
|
|
import { Telegraf } from 'telegraf';
|
2026-05-18 07:27:06 +07:00
|
|
|
import { config } from '../env';
|
2026-05-18 07:29:01 +07:00
|
|
|
import logger from './logger';
|
2026-05-18 07:06:21 +07:00
|
|
|
|
|
|
|
|
const bot = new Telegraf(config.botToken);
|
|
|
|
|
const TELEGRAM_API_URL = `https://api.telegram.org/bot${config.botToken}/`;
|
|
|
|
|
|
2026-05-18 21:02:38 +07:00
|
|
|
const withRetry = async <T>(fn: () => Promise<T>, retries = 5): Promise<T> => {
|
|
|
|
|
try {
|
|
|
|
|
return await fn();
|
|
|
|
|
} catch (error: any) {
|
|
|
|
|
const errorStr = error.message || String(error);
|
|
|
|
|
const match = errorStr.match(/retry after (\d+)/i);
|
|
|
|
|
if (match && retries > 0) {
|
|
|
|
|
const seconds = parseInt(match[1], 10);
|
|
|
|
|
logger.warn(`Telegram 429 Too Many Requests detected. Retrying after ${seconds} seconds...`, {
|
|
|
|
|
error: errorStr,
|
|
|
|
|
});
|
|
|
|
|
await new Promise((resolve) => setTimeout(resolve, seconds * 1000));
|
|
|
|
|
return withRetry(fn, retries - 1);
|
|
|
|
|
}
|
|
|
|
|
throw error;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2026-05-18 07:27:06 +07:00
|
|
|
interface ForwardResult {
|
|
|
|
|
telegramFileId: string;
|
|
|
|
|
telegramFileUniqueId: string;
|
|
|
|
|
storageMessageId: number;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
interface TelegramFileInfo {
|
|
|
|
|
file_size: number;
|
|
|
|
|
mime_type: string;
|
|
|
|
|
file_path: string;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export const forwardToStorage = async (
|
|
|
|
|
fileChunk: any,
|
|
|
|
|
fileName: string,
|
2026-05-18 19:52:46 +07:00
|
|
|
fileType: string,
|
2026-05-18 07:27:06 +07:00
|
|
|
): Promise<ForwardResult> => {
|
2026-05-18 07:06:21 +07:00
|
|
|
try {
|
2026-05-18 08:18:55 +07:00
|
|
|
const filePayload = { source: fileChunk, filename: fileName };
|
2026-05-18 19:52:46 +07:00
|
|
|
let result: any;
|
|
|
|
|
|
|
|
|
|
if (fileType === 'photo') {
|
2026-05-18 21:02:38 +07:00
|
|
|
result = await withRetry(() => bot.telegram.sendPhoto(config.storageChatId, filePayload, {
|
2026-05-18 19:52:46 +07:00
|
|
|
caption: fileName,
|
2026-05-18 21:02:38 +07:00
|
|
|
}));
|
2026-05-18 19:52:46 +07:00
|
|
|
} else if (fileType === 'audio') {
|
2026-05-18 21:02:38 +07:00
|
|
|
result = await withRetry(() => bot.telegram.sendAudio(config.storageChatId, filePayload, {
|
2026-05-18 19:52:46 +07:00
|
|
|
caption: fileName,
|
2026-05-18 21:02:38 +07:00
|
|
|
}));
|
2026-05-18 19:52:46 +07:00
|
|
|
} else if (fileType === 'video') {
|
2026-05-18 21:02:38 +07:00
|
|
|
result = await withRetry(() => bot.telegram.sendVideo(config.storageChatId, filePayload, {
|
2026-05-18 19:52:46 +07:00
|
|
|
caption: fileName,
|
2026-05-18 21:02:38 +07:00
|
|
|
}));
|
2026-05-18 19:52:46 +07:00
|
|
|
} else if (fileType === 'voice') {
|
2026-05-18 21:02:38 +07:00
|
|
|
result = await withRetry(() => bot.telegram.sendVoice(config.storageChatId, filePayload, {
|
2026-05-18 19:52:46 +07:00
|
|
|
caption: fileName,
|
2026-05-18 21:02:38 +07:00
|
|
|
}));
|
2026-05-18 19:52:46 +07:00
|
|
|
} else if (fileType === 'animation') {
|
2026-05-18 21:02:38 +07:00
|
|
|
result = await withRetry(() => bot.telegram.sendAnimation(config.storageChatId, filePayload, {
|
2026-05-18 19:52:46 +07:00
|
|
|
caption: fileName,
|
2026-05-18 21:02:38 +07:00
|
|
|
}));
|
2026-05-18 19:52:46 +07:00
|
|
|
} else if (fileType === 'sticker') {
|
2026-05-18 21:02:38 +07:00
|
|
|
result = await withRetry(() => bot.telegram.sendSticker(config.storageChatId, filePayload));
|
2026-05-18 19:52:46 +07:00
|
|
|
} else {
|
2026-05-18 21:02:38 +07:00
|
|
|
result = await withRetry(() => bot.telegram.sendDocument(config.storageChatId, filePayload, {
|
2026-05-18 19:52:46 +07:00
|
|
|
caption: `📁 ${fileName}`,
|
2026-05-18 21:02:38 +07:00
|
|
|
}));
|
2026-05-18 19:52:46 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let uploadedFile: any;
|
|
|
|
|
if (result.document) uploadedFile = result.document;
|
|
|
|
|
else if (result.photo) uploadedFile = result.photo?.slice(-1)[0];
|
|
|
|
|
else if (result.video) uploadedFile = result.video;
|
|
|
|
|
else if (result.audio) uploadedFile = result.audio;
|
|
|
|
|
else if (result.voice) uploadedFile = result.voice;
|
|
|
|
|
else if (result.animation) uploadedFile = result.animation;
|
|
|
|
|
else if (result.sticker) uploadedFile = result.sticker;
|
|
|
|
|
else if (result.video_note) uploadedFile = result.video_note;
|
|
|
|
|
else uploadedFile = result[fileType];
|
2026-05-18 07:06:21 +07:00
|
|
|
|
|
|
|
|
logger.info('File forwarded to storage', { fileName, message: result.message_id });
|
|
|
|
|
|
|
|
|
|
return {
|
2026-05-18 08:18:55 +07:00
|
|
|
telegramFileId: uploadedFile?.file_id || '',
|
|
|
|
|
telegramFileUniqueId: uploadedFile?.file_unique_id || '',
|
2026-05-18 07:29:01 +07:00
|
|
|
storageMessageId: result.message_id,
|
2026-05-18 07:06:21 +07:00
|
|
|
};
|
2026-05-18 07:27:06 +07:00
|
|
|
} catch (error: any) {
|
2026-05-18 07:06:21 +07:00
|
|
|
logger.error('Failed to forward file to storage', { fileName, error: error.message });
|
|
|
|
|
throw error;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2026-05-18 07:27:06 +07:00
|
|
|
export const getFileInfo = async (
|
|
|
|
|
telegramFileId: string,
|
2026-05-18 07:29:01 +07:00
|
|
|
telegramFileUniqueId: string,
|
2026-05-18 07:27:06 +07:00
|
|
|
): Promise<TelegramFileInfo> => {
|
2026-05-18 07:06:21 +07:00
|
|
|
try {
|
|
|
|
|
const result = await fetch(`${TELEGRAM_API_URL}getFile`);
|
2026-05-18 07:27:06 +07:00
|
|
|
const data: any = await result.json();
|
2026-05-18 07:06:21 +07:00
|
|
|
|
|
|
|
|
if (!data.ok) {
|
|
|
|
|
throw new Error(data.description || 'Telegram API error');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const fileId = data.result.file_id === telegramFileId ? telegramFileId : telegramFileUniqueId;
|
|
|
|
|
const fileResult = await fetch(`${TELEGRAM_API_URL}getInfo`, {
|
|
|
|
|
method: 'POST',
|
|
|
|
|
headers: { 'Content-Type': 'application/json' },
|
2026-05-18 07:29:01 +07:00
|
|
|
body: JSON.stringify({ file_id: fileId }),
|
2026-05-18 07:06:21 +07:00
|
|
|
});
|
2026-05-18 07:27:06 +07:00
|
|
|
const fileInfo: any = await fileResult.json();
|
2026-05-18 07:06:21 +07:00
|
|
|
|
|
|
|
|
if (!fileInfo.ok) {
|
|
|
|
|
throw new Error(fileInfo.description || 'Telegram info error');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
file_size: fileInfo.result.file_size,
|
|
|
|
|
mime_type: fileInfo.result.mime_type,
|
2026-05-18 07:29:01 +07:00
|
|
|
file_path: fileInfo.result.file_path,
|
2026-05-18 07:06:21 +07:00
|
|
|
};
|
2026-05-18 07:27:06 +07:00
|
|
|
} catch (error: any) {
|
2026-05-18 07:06:21 +07:00
|
|
|
logger.error('Failed to get file info', { error: error.message });
|
|
|
|
|
throw error;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2026-05-18 07:27:06 +07:00
|
|
|
export const getBot = (): Telegraf => bot;
|