34 lines
1 KiB
TypeScript
34 lines
1 KiB
TypeScript
import axios from 'axios';
|
|
|
|
const BOT_TOKEN = process.env.TELEGRAM_BOT_TOKEN;
|
|
const CHAT_ID = process.env.TELEGRAM_CHAT_ID;
|
|
|
|
/**
|
|
* Send a Telegram message with Markdown formatting.
|
|
* Silently logs on failure (don't crash the polling loop on a Telegram hiccup).
|
|
*/
|
|
export async function sendTelegram(text: string): Promise<void> {
|
|
if (!BOT_TOKEN || !CHAT_ID) {
|
|
console.warn('[telegram] BOT_TOKEN or CHAT_ID not set, skipping notification');
|
|
return;
|
|
}
|
|
|
|
try {
|
|
await axios.post(`https://api.telegram.org/bot${BOT_TOKEN}/sendMessage`, {
|
|
chat_id: CHAT_ID,
|
|
text,
|
|
parse_mode: 'Markdown',
|
|
});
|
|
} catch (err) {
|
|
const msg = err instanceof Error ? err.message : String(err);
|
|
console.error(`[telegram] Send failed: ${msg}`);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Send a startup/shutdown notification
|
|
*/
|
|
export async function sendServiceNotification(event: 'start' | 'stop'): Promise<void> {
|
|
const emoji = event === 'start' ? '🚀' : '🛑';
|
|
await sendTelegram(`${emoji} *Position Tracker* ${event === 'start' ? 'started' : 'stopped'}`);
|
|
}
|