-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnotify.js
More file actions
106 lines (90 loc) · 2.64 KB
/
notify.js
File metadata and controls
106 lines (90 loc) · 2.64 KB
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
async function main() {
const data = process.env.data;
if (data && data.trim() !== '') {
const notifyItems = JSON.parse(data);
console.log(`收到 ${notifyItems.length} 条通知消息`);
// 处理每条通知消息
for (const notifyItem of notifyItems) {
const { message, types } = notifyItem;
console.log(`发送消息: ${message}`);
console.log(`通知类型: ${types.join(', ')}`);
// 并发发送所有类型的通知
const promises = types.map(type => sendNotificationByType(message, type));
await Promise.allSettled(promises);
}
} else {
console.log('没有通知消息需要发送');
}
}
// 根据类型发送通知
async function sendNotificationByType(message, type) {
switch (type) {
case 'synology':
await sendSynologyNotification(message);
break;
case 'pushplus':
await sendPushplusNotification(message);
break;
default:
console.log(`未知的通知类型: ${type}`);
}
}
// 发送Synology Chat通知
async function sendSynologyNotification(message) {
const notifyServer = process.env.notify_server;
const notifyToken = process.env.notify_token;
if (!notifyServer || !notifyToken) {
console.log('Synology Chat: NOTIFY_SERVER 或 NOTIFY_TOKEN 环境变量未配置');
return;
}
const data = {
payload: JSON.stringify({
text: message
}),
token: notifyToken
};
const options = {
method: 'POST',
body: Object.entries(data).map(([key, value]) => `${key}=${value}`).join('&')
};
try {
await fetch(`${notifyServer}/webapi/entry.cgi?api=SYNO.Chat.External&method=incoming&version=2`, options);
console.log('Synology Chat notification sent successfully');
} catch (error) {
console.error('Synology Chat notification failed:', error.message);
}
}
// 发送pushplus通知
async function sendPushplusNotification(message) {
const pushplusToken = process.env.pushplus_notify_token;
const pushplusServer = process.env.pushplus_notify_server || 'http://www.pushplus.plus/send';
if (!pushplusToken) {
console.log('Pushplus: PUSHPLUS_NOTIFY_TOKEN 环境变量未配置');
return;
}
const data = {
token: pushplusToken,
title: 'Monitor通知',
content: message,
template: 'txt'
};
const options = {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(data)
};
try {
const response = await fetch(pushplusServer, options);
const result = await response.json();
if (result.code === 200) {
console.log('Pushplus notification sent successfully');
} else {
console.error('Pushplus notification failed:', result.msg);
}
} catch (error) {
console.error('Pushplus notification request failed:', error.message);
}
}
main();