50 lines
1.5 KiB
TypeScript
50 lines
1.5 KiB
TypeScript
import axios from 'axios';
|
|
import { isDevMode, telegramBotToken, telegramChatIds } from './env-exporter';
|
|
|
|
const BOT_TOKEN = telegramBotToken;
|
|
const CHAT_IDS = telegramChatIds;
|
|
const TELEGRAM_API_URL = `https://api.telegram.org/bot${BOT_TOKEN}`;
|
|
|
|
/**
|
|
* Send a message to Telegram bot
|
|
* @param message The message text to send
|
|
* @returns Promise<boolean | null> indicating success, failure, or null if dev mode
|
|
*/
|
|
export const sendTelegramMessage = async (message: string): Promise<boolean | null> => {
|
|
if (isDevMode) {
|
|
return null;
|
|
}
|
|
try {
|
|
const results = await Promise.all(
|
|
CHAT_IDS.map(async (chatId) => {
|
|
try {
|
|
const response = await axios.post(`${TELEGRAM_API_URL}/sendMessage`, {
|
|
chat_id: chatId,
|
|
text: message,
|
|
parse_mode: 'HTML',
|
|
});
|
|
|
|
if (response.data && response.data.ok) {
|
|
console.log(`Telegram message sent successfully to ${chatId}`);
|
|
return true;
|
|
} else {
|
|
console.error(`Telegram API error for ${chatId}:`, response.data);
|
|
return false;
|
|
}
|
|
} catch (error) {
|
|
console.error(`Failed to send Telegram message to ${chatId}:`, error);
|
|
return false;
|
|
}
|
|
})
|
|
);
|
|
|
|
console.log('sent telegram message successfully')
|
|
|
|
|
|
// Return true if at least one message was sent successfully
|
|
return results.some((result) => result);
|
|
} catch (error) {
|
|
console.error('Failed to send Telegram message:', error);
|
|
return false;
|
|
}
|
|
};
|