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
| const Application = require('@waline/vercel');
const FormData = require('form-data');
const fetch = require('node-fetch');
module.exports = Application({
plugins: [],
async postSave(comment) {
// do what ever you want after comment saved
console.log('New comment received:', comment);
// 获取环境变量
const { SC_KEY, SITE_NAME, SITE_URL, PUSH_PLUS_KEY } = process.env;
// 准备数据
const data = {
self: comment,
site: {
name: SITE_NAME || 'Default Site Name',
url: SITE_URL || 'https://default.site.url',
postUrl: (SITE_URL || 'https://default.site.url') + comment.url + '#' + comment.objectId,
},
};
const contentTemplate = `
💬 LengM
----------------------------
评论页面: ${data.site.postUrl}
评论内容: ${data.self.comment}
评论者昵称: ${data.self.nick}
评论者邮箱: ${data.self.mail || '未提供'}
IP 地址: ${data.self.ip || '未知'}
评论时间: ${new Date(data.self.createdAt).toLocaleString()}
浏览器信息: ${data.self.ua || '未知'}
`;
const title = `LengM - 新评论通知`;
let success = false;
// Server酱通知
if (SC_KEY) {
const form = new FormData();
form.append('text', title);
form.append('desp', contentTemplate);
try {
const response = await fetch(`https://sctapi.ftqq.com/${SC_KEY}.send`, {
method: 'POST',
headers: form.getHeaders(),
body: form,
});
const result = await response.json();
if (response.ok && result.code === 0) {
console.log('Server酱通知成功:', result);
success = true;
} else {
console.error('Server酱通知失败:', result);
}
} catch (error) {
console.error('Error sending Server酱 notification:', error.message);
}
} else {
console.error('SC_KEY not defined in environment variables.');
}
// PushPlus通知
if (PUSH_PLUS_KEY) {
try {
const pushplusResponse = await fetch('http://www.pushplus.plus/send/', {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
body: `token=${PUSH_PLUS_KEY}&title=${encodeURIComponent(title)}&content=${encodeURIComponent(contentTemplate)}&template=html`,
});
const pushplusResult = await pushplusResponse.json();
if (pushplusResponse.ok && pushplusResult.code === 200) {
console.log('PushPlus通知成功:', pushplusResult);
success = true;
} else {
console.error('PushPlus通知失败:', pushplusResult);
}
} catch (error) {
console.error('Error sending PushPlus notification:', error.message);
}
} else {
console.error('PUSH_PLUS_KEY not defined in environment variables.');
}
// 检查是否有任何通知成功
if (!success) {
console.error('两种通知方式均失败。');
return false;
}
},
});
|