-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathwechat.js
More file actions
282 lines (249 loc) · 9.94 KB
/
wechat.js
File metadata and controls
282 lines (249 loc) · 9.94 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
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
'use strict';
const async = require('async');
const fs = require('fs');
const os = require('os');
const puppeteer = require('puppeteer');
const util = require('util');
const { logger, MessageError, getConfig, startApp } = require('./common.js');
let config = getConfig('./wechat_conf.json', './config.json');
// 用await延时
const sleep = (timeout) => new Promise((resolve, reject) => { setTimeout(() => resolve(), timeout); });
const readFile = util.promisify(fs.readFile);
let banwords = [];
const loadBanwords = async () => {
banwords = [];
logger.info(`Read banwords.lst`);
try {
const lines = (await readFile('banwords.lst')).toString();
for (let line of lines.split(os.EOL)) {
if (!line.match(/^\s*#/) && line.trim()) {
banwords.push(line.trim());
logger.info(line);
}
}
} catch (e) {
logger.error('Error reading banword: ', e);
}
};
loadBanwords();
(async () => {
// 开启无头浏览器
const browser = await puppeteer.launch({
headless: true,
});
const page = (await browser.pages())[0]; // 取浏览器第一个Tab页
await page.setViewport({ width: 1366, height: 768 }); // 浏览器窗口大小
// 使用简体中文界面
await page.goto('https://wx.qq.com/?lang=zh_CN');
const sendMessage = async (target, message) => {
// 判断是否登录
const unloginTest = await page.$('body.unlogin');
if (unloginTest) {
throw new MessageError('Not login', 'NOLOGIN');
}
if (!target) {
throw new MessageError('Target not found', 'NOTARGET');
}
// 如果当前聊天就是目标,那么不用搜了,直接蹦到聊天框
const testEle1 = await page.$('#chatArea a.title_name');
const test1 = await (await testEle1.getProperty('textContent')).jsonValue();
if (test1 !== target) {
const searchEle = await page.$('#search_bar > input');
// 清空搜索框和搜索结果
await page.$eval('#search_bar input', node => node.value = '');
await searchEle.focus();
await searchEle.type(' ');
await searchEle.press('Backspace');
// 延时,使页面上原有的搜索结果消失
for (let timeout = 40; timeout >= 0; timeout--) {
const testEle2 = await page.$('#search_bar div.mmpop');
if (!testEle2) {
break;
}
await sleep(50);
}
// 输入目标群组名称
await searchEle.type(target);
// 等待出现搜索结果,最长等待5秒
let ok = false;
for (let timeout = 100; timeout >= 0; timeout--) {
const testEle3 = await page.$('#search_bar div.mmpop h4.contact_title');
if (testEle3) {
const test3 = await (await testEle3.getProperty('textContent')).jsonValue();
if (test3 === '找不到匹配的结果') {
throw new MessageError('Target not found', 'NOTARGET');
} else {
ok = true;
break;
}
}
await sleep(50);
}
if (!ok) {
throw new MessageError('WeChat not responding', 'NORESPONSE');
}
// 遍历搜索结果
// 由于overflow数字不大,且翻页需要消耗操作和等待网络请求的时间,建议目标名称独一无二,免得不好找。
const pop = await page.$('#search_bar div.mmpop');
let lastname = '';
ok = false;
for (let overflow = 100; overflow >= 0; overflow--) {
const nowEle = await pop.$('div.contact_item.on');
// 说明正在loading
if (!nowEle) {
await sleep(50);
continue;
}
let currname = await (await (await nowEle.$('h4')).getProperty('textContent')).jsonValue();
if (lastname === currname) {
// 未找到目标,结束
ok = false;
break;
}
lastname = currname;
// 如果没找到而且能往下翻那么就继续往下翻
// 找到的话按一下回车键,进入聊天界面
if (currname === target) {
ok = true;
await searchEle.press('Enter');
break;
} else {
await searchEle.press('ArrowDown');
// 等待微信响应
for (let timeout = 10; timeout >= 0; timeout--) {
const nowEle2 = await pop.$('div.contact_item.on');
if (nowEle2) {
let currname2 = await (await (await nowEle2.$('h4')).getProperty('textContent')).jsonValue();
if (currname !== currname2) {
break;
}
await sleep(20);
} else {
// 暂时到底了,需要loading
await sleep(200);
}
}
}
}
if (!ok) {
throw new MessageError('Target not found', 'NOTARGET');
}
// 等待进入聊天界面
for (let timeout = 50; timeout >= 0; timeout--) {
const titleEle = await page.$('#chatArea a.title_name');
const title = await (await titleEle.getProperty('textContent')).jsonValue();
if (title === target) {
break;
}
await sleep(20);
}
}
const testEle4 = await page.$('#chatArea a.title_name');
const test4 = await (await testEle4.getProperty('textContent')).jsonValue();
if (test4 === target) {
// 输入消息
await page.$eval('#editArea', node => node.textContent = '');
const editEle = await page.$('#editArea');
await editEle.focus();
for (const [i, line] of message.split('\n').entries()) {
if (i > 0) {
// 发送多行消息时需要用 Ctrl+Enter 换行
await page.keyboard.down('Control');
await page.keyboard.press('Enter');
await page.keyboard.up('Control');
}
await editEle.type(line);
}
// 按下发送按钮
await page.keyboard.press('Enter');
} else {
throw new MessageError('Target not confirmed', 'NORESPONSE');
}
};
// 模拟浏览器操作,每次操作需要花点时间,而且不支持并行,所以需要使用队列处理
const queue = async.queue((param, callback) => {
sendMessage(param.target, param.message).then(() => {
callback(null);
}).catch((err) => {
callback(err);
});
}, 1);
// 启动API
const app = startApp({
config,
sendMessage: (target, message) => new Promise((resolve, reject) => {
// 屏蔽敏感词
let censoredMessage = message;
for (let banword of banwords) {
censoredMessage = censoredMessage.replace(new RegExp(banword), (str) => '?'.repeat(str.length));
}
queue.push({ target, censoredMessage }, (err) => {
if (err) {
reject(err);
} else {
resolve();
}
});
}),
});
// 以下是其他API
// 获取网页截图
app.get(config.wechat.screenshotUrl, async (req, res, next) => {
if (req.query.token === config.server.apiToken) {
const buffer = await page.screenshot({
fullPage: true,
});
res.status(200).header({'Content-Type': 'image/png'}).send(buffer);
} else {
res.status(404);
next();
}
});
// 检测是否登录
if (config.wechat.isLoginUrl) {
app.get(config.wechat.isLoginUrl, async (req, res, next) => {
if (req.query.token === config.server.apiToken) {
// 判断是否登录
const unloginTest = await page.$('body.unlogin');
if (unloginTest) {
res.status(200).json({
isLogin: false,
});
} else {
res.status(200).json({
isLogin: true,
});
}
} else {
res.status(404);
next();
}
});
}
// 重新加载设置(实际上仅限token和mapping)
if (config.wechat.reloadUrl) {
app.get(config.wechat.reloadUrl, (req, res, next) => {
if (req.query.token === config.server.apiToken) {
try {
const newconfig = getConfig('./wechat_conf.json', './config.json');
config.server = newconfig.server;
config.mapping = newconfig.mapping;
loadBanwords().catch(() => {});
res.status(200).json({
status: 'SUCCESS',
message: '',
});
} catch (e) {
logger.error(`Error reading config: `, e);
res.status(200).json({
status: 'FAILED',
message: e.message,
});
}
} else {
res.status(404);
next();
}
});
}
})();