-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
102 lines (96 loc) · 2.73 KB
/
index.js
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
const Mirai = require('node-mirai-sdk');
const express = require('express');
const bodyParser = require('body-parser');
const config = require('./config.json');
const bot = new Mirai(config.mirai);
const server = express();
const {Plain, Image} = Mirai.MessageComponent;
const getTime = () => new Date().toLocaleString();
const ckeckToken = (token = '') =>
config.webhook.token.length && config.webhook.token !== token;
const responseText = [
'success', // 成功
'token invalid', // token 失效
'unsupported method', // 不支持的请求方式
'server error', // 服务器错误
];
bot.onSignal('authed', () => {
console.log(`${getTime()} 通过:${bot.sessionKey} 认证中···`);
bot.verify();
});
bot.onSignal('verified', () => {
sendRawMessage('Webhook 已启用', null);
console.log(`${getTime()} 通过:${bot.sessionKey} 认证成功!\n`);
});
/**
* 发送 QQ 消息
* @param {string} text 文本消息
* @param {string} image 图片消息(图片 URL)
*/
function sendRawMessage(text, image) {
let messageChain = [];
if (text && text.length) {
messageChain.push(Plain(text));
}
if (image && image.length) {
messageChain.push(Image({url: image}));
}
if (config.bot.isGroup) {
bot.sendGroupMessage(messageChain, config.bot.target);
} else {
bot.sendFriendMessage(messageChain, config.bot.target);
}
}
/**
* 根据错误码获取响应内容
* @param {number} err 错误状态(0-成功; 1-token 失效; 2-不支持的请求方式; 3-服务器错误)
* @returns {string} 响应内容
*/
function getResponse(err) {
return `{err: ${err}, msg: "${responseText[err]}"}`;
}
server
.use(bodyParser.text({type: 'text/plain'}))
.use(bodyParser.json({type: 'application/json'}))
.use((err, _req, res, next) => {
res.type('json').end(getResponse(3));
next(err);
})
.get('/', (req, res) => {
const {token, text, image} = req.query;
if (ckeckToken(token)) {
res.type('json').end(getResponse(1));
return;
}
sendRawMessage(text, image);
res.type('json').end(getResponse(0));
console.log(`${getTime()} webhook 调用成功`);
})
.post('/', (req, res) => {
const type = req.get('Content-Type');
let token = req.query.token || '';
let image = '';
let text = '';
switch (type) {
case 'text/plain':
token = req.query.token;
text = req.body;
break;
case 'application/json':
token = token || req.body.token;
text = req.body.text;
image = req.body.image;
break;
default:
res.type('json').end(getResponse(2));
return;
}
if (ckeckToken(token)) {
res.type('json').end(getResponse(1));
return;
}
sendRawMessage(text, image);
res.type('json').end(getResponse(0));
console.log(`${getTime()} webhook 调用成功`);
})
.listen(config.webhook.port, '0.0.0.0');