-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.mjs
209 lines (196 loc) · 8.47 KB
/
main.mjs
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
import url from 'url';
import http from 'http';
import https from 'https';
import crypto from 'crypto';
import google from 'google';
import { spawn } from 'child_process';
import getAccessToken from './accessToken.mjs';
google.resultsPerPage = 1
const PORT = 3333; // 服务端口
const WX_TOKEN = 'adele'; // 微信公众平台服务器配置中的 Token
const ROKID_SN = "0001121743000214";
const ROKID_WEBHOOK = "rJTmCO9k7";
/**
* 对字符串进行sha1加密
* @param {string} str 需要加密的字符串
* @return {string} 加密后的字符串
**/
function sha1(str) {
var sha1sum = crypto.createHash('sha1');
sha1sum.update(str);
str = sha1sum.digest('hex');
return str;
}
/**
* 验证服务器的有效性
* @param {object} req http 请求
* @param {object} res http 响应
* @return {object} 验证结果
* 整个验证步骤分为三步
* 1. 将token、timestamp、nonce三个参数进行字典序排序
* 2. 将三个参数字符串拼接成一个字符串进行sha1加密
* 3. 开发者获得加密后的字符串可与signature对比,标识该请求来源于微信
**/
function checkSignature(req, res) {
const query = url.parse(req.url, true).query;
console.log('Request URL: ', req.url);
const signature = query.signature;
const timestamp = query.timestamp;
const nonce = query.nonce;
const echostr = query.echostr;
// 将 token/timestamp/nonce 三个参数进行字典序排序
const tmpArr = [WX_TOKEN, timestamp, nonce];
const tmpStr = sha1(tmpArr.sort().join(''));
// 验证排序并加密后的字符串与 signature 是否相等
if (tmpStr === signature) {
if (req.method === 'GET') { //Wechat Service 验证
res.end(echostr);
console.log('Signature Check Success');
} else { // handle message push
republic(req, res);
}
} else {
res.end('signature check failed');
console.log('Signature Check Failed');
}
req.on('error', e => console.log(e));
}
// Rokid 文本消息处理
function rokidTTS(text) {
//执行若琪播放文字
const body = {
type: "tts",
devices: {
sn: ROKID_SN
},
data: {
text
}
};
const options = {
hostname: 'homebase.rokid.com',
port: 443,
path: `/trigger/with/${ROKID_WEBHOOK}`,
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
}
};
var req = https.request(options, (res) => {
var responseString = '';
res.on('data', (data) => {
responseString += data;
});
res.on('end', function () {
var resultObject = JSON.parse(responseString);
console.log(`[Rokid TTS] ${resultObject.message}`)
});
});
req.on('error', e => console.error(e));
req.write(JSON.stringify(body));
req.end();
}
function republic(req, res) {
var body = '';
req.on('data', function (data) {
body += data;
});
req.on('end', function () {
const r_ToUserName = body.match(/<ToUserName><\!\[CDATA\[(.*)\]\]><\/ToUserName>/)[1];
const r_FromUserName = body.match(/<FromUserName><\!\[CDATA\[(.*)\]\]><\/FromUserName>/)[1];
const r_MsgType = body.match(/<MsgType><\!\[CDATA\[(.*)\]\]><\/MsgType>/)[1];
const r_CreateTime = body.match(/<CreateTime>(.*)<\/CreateTime>/)[1];
const s_ToUserName = r_FromUserName;
const s_FromUserName = r_ToUserName;
const s_MsgType = 'text';
const s_CreateTime = parseInt(Date.now() / 1000);
var s_Content = '';
var cmd = spawn('fortune');
switch (r_MsgType) {
case 'text':
var r_MsgId = body.match(/<MsgId>(.*)<\/MsgId>/)[1];
var r_Content = body.match(/<Content><\!\[CDATA\[([\s\S]*)\]\]><\/Content>/)[1];
if (/[\u4E00-\u9FA5\uF900-\uFA2D]/.test(r_Content)) {
cmd = spawn('trans', ['-b', ':en', r_Content]);
} else {
cmd = spawn('trans', ['-b', ':zh', r_Content]);
}
console.log(`[recive ${r_MsgType}] ${r_Content} (from ${r_FromUserName} at ${r_CreateTime})`);
break;
case 'image':
var r_MsgId = body.match(/<MsgId>(.*)<\/MsgId>/)[1];
var r_PicUrl = body.match(/<PicUrl><\!\[CDATA\[(.*)\]\]><\/PicUrl>/)[1];
var r_MediaId = body.match(/<MediaId><\!\[CDATA\[(.*)\]\]><\/MediaId>/)[1];
console.log(`[recive ${r_MsgType}] ${r_PicUrl} ${r_MediaId} (from ${r_FromUserName} at ${r_CreateTime})`);
break;
case 'video':
var r_MsgId = body.match(/<MsgId>(.*)<\/MsgId>/)[1];
var r_ThumbMediaId = body.match(/<ThumbMediaId><\!\[CDATA\[(.*)\]\]><\/ThumbMediaId>/)[1];
var r_MediaId = body.match(/<MediaId><\!\[CDATA\[(.*)\]\]><\/MediaId>/)[1];
console.log(`[recive ${r_MsgType}] ${r_ThumbMediaId} ${r_MediaId} (from ${r_FromUserName} at ${r_CreateTime})`);
break;
case 'voice':
var r_MsgId = body.match(/<MsgId>(.*)<\/MsgId>/)[1];
var r_Format = body.match(/<Format><\!\[CDATA\[(.*)\]\]><\/Format>/)[1];
var r_MediaId = body.match(/<MediaId><\!\[CDATA\[(.*)\]\]><\/MediaId>/)[1];
var r_Recognition = body.match(/<Recognition><\!\[CDATA\[(.*)\]\]><\/Recognition>/)[1];
cmd = spawn('trans', ['-b', ':en', r_Recognition]);
console.log(`[recive ${r_MsgType}] ${r_Recognition} ${r_Format} ${r_MediaId} (from ${r_FromUserName} at ${r_CreateTime})`);
break;
case 'link':
var r_Title = body.match(/<Title><\!\[CDATA\[(.*)\]\]><\/Title>/)[1];
var r_Description = body.match(/<Description><\!\[CDATA\[(.*)\]\]><\/Description>/)[1];
var r_Url =body.match(/<Url><\!\[CDATA\[(.*)\]\]><\/Url>/)[1];
console.log(`[recive ${r_MsgType}] ${r_Title} ${r_Url} (from ${r_FromUserName} at ${r_CreateTime})`);
case 'event':
var r_Event = body.match(/<Event><\!\[CDATA\[(.*)\]\]><\/Event>/)[1];
// var r_EventKey = body.match(/<EventKey><\!\[CDATA\[(.*)\]\]><\/EventKey>/)[1];
console.log(`[recive ${r_MsgType}] ${r_Event} (from ${r_FromUserName} at ${r_CreateTime})`);
break;
default:
console.log(`[recive ${r_MsgType}] (from ${r_FromUserName} at ${r_CreateTime})`);
console.log(`[origin msg body] ${body}`);
}
cmd.stdout.on('data', (data) => {
s_Content += data;
});
cmd.on('close', (code) => {
s_Content = s_Content.replace(/\n$/, '');
if(!s_Content) {
console.log("[ERROR] Sorry I can not translate it.");
} else {
google(s_Content, function (googleErr, googleRes){
if (googleErr) console.error(googleErr)
var link = googleRes.links[0];
if (link.href) s_Content += '\n' + link.title + '\n' + link.href + '\n' + link.description;
var msg = `
<xml>
<ToUserName><![CDATA[${s_ToUserName}]]></ToUserName>
<FromUserName><![CDATA[${s_FromUserName}]]></FromUserName>
<CreateTime>${s_CreateTime}</CreateTime>
<MsgType><![CDATA[${s_MsgType}]]></MsgType>
<Content><![CDATA[${s_Content}]]></Content>
</xml>`;
res.end(msg);
})
}
/*
if (r_Content) {
rokidTTS(`<speak>${r_Content}<break time="1s"/>${s_Content}</speak>`);
} else if (r_Recognition) {
rokidTTS(`<speak>${r_Recognition}<break time="1s"/>${s_Content}</speak>`);
} else if (r_Title) {
rokidTTS(`<speak>${r_Title}<break time="1s"/> ${r_Description}</speak>`);
} else {
rokidTTS(s_Content);
}
*/
});
});
}
const server = http.createServer(checkSignature);
server.listen(PORT, () => {
console.log(`Server is runnig ar port ${PORT}`);
});
getAccessToken().then(token => console.log(`access_token: ${token}`));