-
Notifications
You must be signed in to change notification settings - Fork 85
/
weixinbot.js
806 lines (690 loc) · 20.3 KB
/
weixinbot.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
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
/* eslint-disable quote-props,no-constant-condition,
prefer-template,consistent-return,new-cap,no-param-reassign */
const fs = require('fs');
const url = require('url');
const path = require('path');
const http = require('http');
const https = require('https');
const axios = require('axios');
const Debug = require('debug');
const touch = require('touch');
const tough = require('tough-cookie');
const Datastore = require('nedb');
const Promise = require('bluebird');
const EventEmitter = require('events');
const nodemailer = require('nodemailer');
const qrcode = require('qrcode-terminal');
const FileCookieStore = require('tough-cookie-filestore');
const axiosCookieJarSupport = require('node-axios-cookiejar');
const {
getUrls, CODES, SP_ACCOUNTS, PUSH_HOST_LIST,
} = require('./conf');
Promise.promisifyAll(Datastore.prototype);
const debug = Debug('weixinbot');
let URLS = getUrls({});
const logo = fs.readFileSync(path.join(__dirname, 'logo.txt'), 'utf8');
// try persistent cookie
const cookiePath = path.join(process.cwd(), '.cookie.json');
touch.sync(cookiePath);
const jar = new tough.CookieJar(new FileCookieStore(cookiePath));
const req = axios.create({
timeout: 35e3,
headers: {
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_2) ' +
'AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2652.0 Safari/537.36',
'Referer': 'https://wx2.qq.com/',
},
jar,
withCredentials: true,
xsrfCookieName: null,
xsrfHeaderName: null,
httpAgent: new http.Agent({ keepAlive: true }),
httpsAgent: new https.Agent({ keepAlive: true }),
});
axiosCookieJarSupport(req);
const secretPath = path.join(process.cwd(), '.secret.json');
const makeDeviceID = () => 'e' + Math.random().toFixed(15).toString().substring(2, 17);
class WeixinBot extends EventEmitter {
constructor(options = {}) {
super();
// transporter for send qrcode image url
// 请不要依赖这个默认提供的邮件账户!。
this.transporter = nodemailer.createTransport(options.mailOpts || {
service: 'QQex',
auth: {
user: '[email protected]',
pass: 'V0an1KqPdz4ZKNuP',
},
});
// email address for get qrcode image url
this.receiver = options.receiver || '';
Object.assign(this, CODES);
debug(logo);
}
async run() {
if (fs.existsSync(secretPath)) {
this.initConfig();
const secret = JSON.parse(fs.readFileSync(secretPath, 'utf8'));
Object.assign(this, secret);
this.runLoop();
} else {
this.init();
}
}
initConfig() {
this.baseHost = '';
this.pushHost = '';
this.uuid = '';
this.redirectUri = '';
this.skey = '';
this.sid = '';
this.uin = '';
this.passTicket = '';
this.baseRequest = null;
this.my = null;
this.syncKey = null;
this.formateSyncKey = '';
this.deviceid = makeDeviceID();
// member store
this.Members = new Datastore();
this.Contacts = new Datastore();
this.Groups = new Datastore();
this.GroupMembers = new Datastore();
this.Brands = new Datastore(); // 公众帐号
this.SPs = new Datastore(); // 特殊帐号
// indexing
this.Members.ensureIndex({ fieldName: 'UserName', unique: true });
this.Contacts.ensureIndex({ fieldName: 'UserName', unique: true });
this.Groups.ensureIndex({ fieldName: 'UserName', unique: true });
this.Brands.ensureIndex({ fieldName: 'UserName', unique: true });
this.SPs.ensureIndex({ fieldName: 'UserName', unique: true });
clearTimeout(this.checkSyncTimer);
clearInterval(this.updataContactTimer);
}
async init() {
debug('开始登录...');
this.initConfig();
try {
this.uuid = await this.fetchUUID();
} catch (e) {
debug('fetch uuid error', e);
this.init();
return;
}
if (!this.uuid) {
debug('获取 uuid 失败,正在重试...');
this.init();
return;
}
debug(`获得 uuid -> ${this.uuid}`);
const qrcodeUrl = URLS.QRCODE_PATH + this.uuid;
this.emit('qrcode', qrcodeUrl);
if (this.receiver) {
debug(`发送二维码图片到邮箱 ${this.receiver}`);
this.transporter.sendMail({
from: `WeixinBot <${this.transporter.transporter.options.auth.user}>`,
to: this.receiver,
subject: 'WeixinBot 请求登录',
html: `<img src="${qrcodeUrl}" height="256" width="256" />`,
}, (e) => {
if (e) debug(`发送二维码图片到邮箱 ${this.receiver} 失败`, e);
});
} else {
qrcode.generate(qrcodeUrl.replace('/qrcode/', '/l/'));
}
// limit check times
this.checkTimes = 0;
while (true) {
const loginCode = await this.checkLoginStep();
if (loginCode === 200) break;
if (loginCode !== 201) this.checkTimes += 1;
if (this.checkTimes > 6) {
debug('检查登录状态次数超出限制,重新获取二维码');
this.init();
return;
}
}
try {
debug('正在获取凭据...');
await this.fetchTickets();
debug('获取凭据成功!');
} catch (e) {
debug('鉴权失败,正在重新登录...', e);
this.init();
return;
}
debug('开始循环拉取新消息');
this.runLoop();
}
async runLoop() {
debug('正在初始化参数...');
try {
await this.webwxinit();
} catch (e) {
debug('登录信息已失效,正在重新获取二维码...');
this.init();
return;
}
debug('初始化成功!');
try {
debug('正在通知客户端网页端已登录...');
await this.notifyMobile();
debug('正在获取通讯录列表...');
await this.fetchContact();
} catch (e) {
debug('初始化信息失败,正在重试');
this.runLoop();
}
debug('通知成功!');
debug('获取通讯录列表成功!');
// await this.fetchBatchgetContact();
this.pushHost = await this.lookupSyncCheckHost();
URLS = getUrls({ baseHost: this.baseHost, pushHost: this.pushHost });
this.syncCheck();
// auto update Contacts every ten minute
this.updataContactTimer = setInterval(() => {
this.updateContact();
}, 1000 * 60 * 10);
}
async fetchUUID() {
let result;
try {
result = await req.get(URLS.API_jsLogin, {
params: {
appid: 'wx782c26e4c19acffb',
fun: 'new',
lang: 'zh_CN',
_: +new Date,
},
});
} catch (e) {
debug('fetch uuid network error', e);
// network error retry
return await this.fetchUUID();
}
const { data } = result;
if (!/uuid = "(.+)";$/.test(data)) {
throw new Error('get uuid failed');
}
const uuid = data.match(/uuid = "(.+)";$/)[1];
return uuid;
}
async checkLoginStep() {
let result;
try {
result = await req.get(URLS.API_login, {
params: {
tip: 1,
uuid: this.uuid,
_: +new Date,
},
});
} catch (e) {
debug('checkLoginStep network error', e);
await this.checkLoginStep();
return;
}
const { data } = result;
if (!/code=(\d{3});/.test(data)) {
// retry
return await this.checkLoginStep();
}
const loginCode = parseInt(data.match(/code=(\d{3});/)[1], 10);
switch (loginCode) {
case 200:
debug('已点击确认登录!');
this.redirectUri = data.match(/redirect_uri="(.+)";$/)[1] + '&fun=new';
this.baseHost = url.parse(this.redirectUri).host;
URLS = getUrls({ baseHost: this.baseHost });
break;
case 201:
debug('二维码已被扫描,请确认登录!');
break;
case 408:
debug('检查登录超时,正在重试...');
break;
default:
debug('未知的状态,重试...');
}
return loginCode;
}
async fetchTickets() {
let result;
try {
result = await req.get(this.redirectUri);
} catch (e) {
debug('fetch tickets network error', e);
// network error, retry
await this.fetchTickets();
return;
}
const { data } = result;
if (!/<ret>0<\/ret>/.test(data)) {
throw new Error('Get skey failed, restart login');
}
// const retM = data.match(/<ret>(.*)<\/ret>/);
// const scriptM = data.match(/<script>(.*)<\/script>/);
const skeyM = data.match(/<skey>(.*)<\/skey>/);
const wxsidM = data.match(/<wxsid>(.*)<\/wxsid>/);
const wxuinM = data.match(/<wxuin>(.*)<\/wxuin>/);
const passTicketM = data.match(/<pass_ticket>(.*)<\/pass_ticket>/);
// const redirectUrl = data.match(/<redirect_url>(.*)<\/redirect_url>/);
this.skey = skeyM && skeyM[1];
this.sid = wxsidM && wxsidM[1];
this.uin = wxuinM && wxuinM[1];
this.passTicket = passTicketM && passTicketM[1];
debug(`
获得 skey -> ${this.skey}
获得 sid -> ${this.sid}
获得 uid -> ${this.uin}
获得 pass_ticket -> ${this.passTicket}
`);
this.baseRequest = {
Uin: parseInt(this.uin, 10),
Sid: this.sid,
Skey: this.skey,
DeviceID: this.deviceid,
};
fs.writeFileSync(secretPath, JSON.stringify({
skey: this.skey,
sid: this.sid,
uin: this.uin,
passTicket: this.passTicket,
baseHost: this.baseHost,
baseRequest: this.baseRequest,
}), 'utf8');
}
async webwxinit() {
let result;
try {
result = await req.post(
URLS.API_webwxinit,
{ BaseRequest: this.baseRequest },
{
params: {
pass_ticket: this.passTicket,
skey: this.skey,
},
}
);
} catch (e) {
debug('webwxinit network error', e);
// network error retry
await this.webwxinit();
return;
}
const { data } = result;
if (!data || !data.BaseResponse || data.BaseResponse.Ret !== 0) {
throw new Error('Init Webwx failed');
}
this.my = data.User;
this.syncKey = data.SyncKey;
this.formateSyncKey = this.syncKey.List.map((item) => item.Key + '_' + item.Val).join('|');
}
async webwxsync() {
let result;
try {
result = await req.post(
URLS.API_webwxsync,
{
BaseRequest: this.baseRequest,
SyncKey: this.syncKey,
rr: ~new Date,
},
{
params: {
sid: this.sid,
skey: this.skey,
pass_ticket: this.passTicket,
},
}
);
} catch (e) {
debug('webwxsync network error', e);
// network error retry
await this.webwxsync();
return;
}
const { data } = result;
this.syncKey = data.SyncKey;
this.formateSyncKey = this.syncKey.List.map((item) => item.Key + '_' + item.Val).join('|');
data.AddMsgList.forEach((msg) => this.handleMsg(msg));
}
async lookupSyncCheckHost() {
for (let host of PUSH_HOST_LIST) {
let result;
try {
result = await req.get('https://' + host + '/cgi-bin/mmwebwx-bin/synccheck', {
params: {
r: +new Date,
skey: this.skey,
sid: this.sid,
uin: this.uin,
deviceid: this.deviceid,
synckey: this.formateSyncKey,
_: +new Date,
},
});
} catch (e) {
debug('lookupSyncCheckHost network error', host);
// network error retry
break;
}
const { data } = result;
const retcode = data.match(/retcode:"(\d+)"/)[1];
if (retcode === '0') return host;
}
}
async syncCheck() {
let result;
try {
result = await req.get(
URLS.API_synccheck,
{
params: {
r: +new Date(),
skey: this.skey,
sid: this.sid,
uin: this.uin,
deviceid: this.deviceid,
synckey: this.syncKey,
_: +new Date(),
},
}
);
} catch (e) {
debug('synccheck network error', e);
// network error retry
return await this.syncCheck();
}
const { data } = result;
const retcode = data.match(/retcode:"(\d+)"/)[1];
const selector = data.match(/selector:"(\d+)"/)[1];
if (retcode !== '0') {
debug('你在其他地方登录或登出了微信,正在尝试重新登录...');
this.runLoop();
return;
}
if (selector !== '0') {
this.webwxsync();
}
clearTimeout(this.checkSyncTimer);
this.checkSyncTimer = setTimeout(() => {
this.syncCheck();
}, 3e3);
}
async notifyMobile() {
let result;
try {
result = await req.post(
URLS.API_webwxstatusnotify,
{
BaseRequest: this.baseRequest,
Code: CODES.StatusNotifyCode_INITED,
FromUserName: this.my.UserName,
ToUserName: this.my.UserName,
ClientMsgId: +new Date,
},
{
params: {
lang: 'zh_CN',
pass_ticket: this.passTicket,
},
}
);
} catch (e) {
debug('notify mobile network error', e);
// network error retry
await this.notifyMobile();
return;
}
const { data } = result;
if (!data || !data.BaseResponse || data.BaseResponse.Ret !== 0) {
throw new Error('通知客户端失败');
}
}
async fetchContact() {
let result;
try {
result = await req.post(
URLS.API_webwxgetcontact,
{},
{
params: {
pass_ticket: this.passTicket,
skey: this.skey,
r: +new Date,
},
}
);
} catch (e) {
debug('fetch contact network error', e);
// network error retry
await this.fetchContact();
return;
}
const { data } = result;
if (!data || !data.BaseResponse || data.BaseResponse.Ret !== 0) {
throw new Error('获取通讯录失败');
}
this.Members.insert(data.MemberList);
this.totalMemberCount = data.MemberList.length;
this.brandCount = 0;
this.spCount = 0;
this.groupCount = 0;
this.friendCount = 0;
data.MemberList.forEach((member) => {
const userName = member.UserName;
if (member.VerifyFlag & CODES.MM_USERATTRVERIFYFALG_BIZ_BRAND) {
this.brandCount += 1;
this.Brands.insert(member);
return;
}
if (SP_ACCOUNTS.includes(userName) || /@qqim$/.test(userName)) {
this.spCount += 1;
this.SPs.insert(member);
return;
}
if (userName.includes('@@')) {
this.groupCount += 1;
this.Groups.insert(member);
return;
}
if (userName !== this.my.UserName) {
this.friendCount += 1;
this.Contacts.insert(member);
}
});
debug(`
获取通讯录成功
全部成员数: ${this.totalMemberCount}
公众帐号数: ${this.brandCount}
特殊帐号数: ${this.spCount}
通讯录好友数: ${this.friendCount}
加入的群聊数(不准确,只有把群聊加入通讯录才会在这里显示): ${this.groupCount}
`);
}
async fetchBatchgetContact(groupIds) {
const list = groupIds.map((id) => ({ UserName: id, EncryChatRoomId: '' }));
let result;
try {
result = await req.post(
URLS.API_webwxbatchgetcontact,
{
BaseRequest: this.baseRequest,
Count: list.length,
List: list,
},
{
params: {
type: 'ex',
r: +new Date,
},
}
);
} catch (e) {
debug('fetch batchgetcontact network error', e);
// network error retry
await this.fetchBatchgetContact(groupIds);
return;
}
const { data } = result;
if (!data || !data.BaseResponse || data.BaseResponse.Ret !== 0) {
throw new Error('Fetch batchgetcontact fail');
}
data.ContactList.forEach((Group) => {
this.Groups.insert(Group);
debug(`获取到群: ${Group.NickName}`);
debug(`群 ${Group.NickName} 成员数量: ${Group.MemberList.length}`);
const { MemberList } = Group;
MemberList.forEach((member) => {
member.GroupUserName = Group.UserName;
this.GroupMembers.update({
UserName: member.UserName,
GroupUserName: member.GroupUserName,
}, member, { upsert: true });
});
});
}
async updateContact() {
debug('正在更新通讯录');
try {
await this.fetchContact();
const groups = await this.Groups.findAsync({});
const groupIds = groups.map((group) => group.UserName);
await this.fetchBatchgetContact(groupIds);
} catch (e) {
debug('更新通讯录失败', e);
}
debug('更新通讯录成功!');
}
async getMember(id) {
const member = await this.Members.findOneAsync({ UserName: id });
return member;
}
async getGroup(groupId) {
let group = await this.Groups.findOneAsync({ UserName: groupId });
if (group) return group;
try {
await this.fetchBatchgetContact([groupId]);
} catch (e) {
debug('fetchBatchgetContact error', e);
return null;
}
group = await this.Groups.findOneAsync({ UserName: groupId });
return group;
}
async getGroupMember(id, groupId) {
let member = await this.GroupMembers.findOneAsync({
UserName: id,
GroupUserName: groupId,
});
if (member) return member;
try {
await this.fetchBatchgetContact([groupId]);
} catch (e) {
debug('fetchBatchgetContact error', e);
return null;
}
member = await this.GroupMembers.findOneAsync({ UserName: id });
return member;
}
async handleMsg(msg) {
if (msg.FromUserName.includes('@@')) {
const userId = msg.Content.match(/^(@[a-zA-Z0-9]+|[a-zA-Z0-9_-]+):<br\/>/)[1];
msg.GroupMember = await this.getGroupMember(userId, msg.FromUserName);
msg.Group = await this.getGroup(msg.FromUserName);
msg.Content = msg.Content.replace(/^(@[a-zA-Z0-9]+|[a-zA-Z0-9_-]+):<br\/>/, '');
debug(`
来自群 ${msg.Group.NickName} 的消息
${msg.GroupMember.DisplayName || msg.GroupMember.NickName}: ${msg.Content}
`);
this.emit('group', msg);
return;
}
msg.Member = await this.getMember(msg.FromUserName);
if (!msg.Member) return;
debug(`
新消息
${msg.Member.RemarkName || msg.Member.NickName}: ${msg.Content}
`);
this.emit('friend', msg);
// if (msg.MsgType === CODES.MSGTYPE_SYSNOTICE) {
// return;
// }
// switch (msg.MsgType) {
// case CODES.MSGTYPE_APP:
// break;
// case CODES.MSGTYPE_EMOTICON:
// break;
// case CODES.MSGTYPE_IMAGE:
// break;
// case CODES.MSGTYPE_VOICE:
// break;
// case CODES.MSGTYPE_VIDEO:
// break;
// case CODES.MSGTYPE_MICROVIDEO:
// break;
// case CODES.MSGTYPE_TEXT:
// try {
// await this.sendText(msg.FromUserName, msg.Content);
// } catch (e) {
// console.error(e);
// }
// break;
// case CODES.MSGTYPE_RECALLED:
// break;
// case CODES.MSGTYPE_LOCATION:
// break;
// case CODES.MSGTYPE_VOIPMSG:
// case CODES.MSGTYPE_VOIPNOTIFY:
// case CODES.MSGTYPE_VOIPINVITE:
// break;
// case CODES.MSGTYPE_POSSIBLEFRIEND_MSG:
// break;
// case CODES.MSGTYPE_VERIFYMSG:
// break;
// case CODES.MSGTYPE_SHARECARD:
// break;
// case CODES.MSGTYPE_SYS:
// break;
// default:
// }
}
sendText(to, content, callback) {
const clientMsgId = (+new Date + Math.random().toFixed(3)).replace('.', '');
req.post(URLS.API_webwxsendmsg,
{
BaseRequest: this.baseRequest,
Msg: {
Type: CODES.MSGTYPE_TEXT,
Content: content,
FromUserName: this.my.UserName,
ToUserName: to,
LocalID: clientMsgId,
ClientMsgId: clientMsgId,
},
},
{
params: {
pass_ticket: this.passTicket,
},
}
).then((result) => {
const { data } = result;
callback = callback || (() => (null));
if (!data || !data.BaseResponse || data.BaseResponse.Ret !== 0) {
return callback(new Error('Send text fail'));
}
callback();
}).catch((e) => {
debug('send text network error', e);
// network error, retry
this.sendText(to, content, callback);
return;
});
}
}
// compatible nodejs require
module.exports = WeixinBot;