-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnotification.ts
77 lines (67 loc) · 1.83 KB
/
notification.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import axios from "axios";
// 定义按钮接口
interface TelegramButton {
text: string;
url: string;
}
export async function sendBarkNotification(title: string, content: string) {
const BARK_KEY = process.env.BARK_KEY;
if (!BARK_KEY) {
console.error("BARK_KEY not found in environment variables");
return;
}
try {
const response = await axios.post(`https://api.day.app/${BARK_KEY}`, {
title: title,
body: content,
group: "排行榜更新",
});
if (response.status === 200) {
console.log("Bark notification sent successfully");
}
} catch (error) {
console.error("Failed to send Bark notification:", error);
}
}
export async function sendTelegramNotification(
message: string,
buttons?: TelegramButton[],
chatId?: string | number
) {
const TELEGRAM_BOT_TOKEN = process.env.TELEGRAM_BOT_TOKEN;
if (!TELEGRAM_BOT_TOKEN) {
console.error("TELEGRAM_BOT_TOKEN not found in environment variables");
return;
}
try {
const TELEGRAM_CHAT_ID = chatId || process.env.TELEGRAM_CHAT_ID;
if (!TELEGRAM_CHAT_ID) {
console.error("TELEGRAM_CHAT_ID not found");
return;
}
const requestData: any = {
chat_id: TELEGRAM_CHAT_ID,
text: message,
parse_mode: "Markdown",
};
if (buttons && buttons.length > 0) {
requestData.reply_markup = {
inline_keyboard: [
buttons.map((button) => ({
text: button.text,
url: button.url,
})),
],
};
}
const response = await axios.post(
`https://api.telegram.org/bot${TELEGRAM_BOT_TOKEN}/sendMessage`,
requestData
);
if (response.status === 200) {
console.log("✅ Telegram notification sent successfully");
}
} catch (error) {
console.error("❌ Failed to send Telegram notification:", error);
}
}