Robot.php
19.4 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
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
<?php
/**
* Created by PhpStorm.
* User: HanSon
* Date: 2016/12/7
* Time: 16:12
*/
namespace Hanson\Vbot;
use Endroid\QrCode\QrCode;
use GuzzleHttp\Client;
use Masterminds\HTML5\Exception;
use Symfony\Component\DomCrawler\Crawler;
class Robot
{
private $client;
public $tmpPath;
public $debug;
private $uuid;
private $baseUri = 'https://wx2.qq.com/cgi-bin/mmwebwx-bin';
private $baseHost = 'wx2.qq.com';
private $redirectUri;
private $skey;
private $sid;
private $uin;
private $passTicket;
private $deviceId;
private $baseRequest;
private $syncKey;
private $myAccount;
private $syncKeyStr;
private $memberList;
private $contactList;
private $publicList;
private $specialList;
private $groupList;
private $accountInfo = [
'groupMember' => [],
'normalMember' => []
];
private $groupMembers;
private $encryChatRoomId;
private $syncHost;
public function __construct(Array $option = [])
{
$this->client = new Client([
'cookies' => true
]);
$this->handleOption($option);
}
/**
* 处理option
*
* @param $option
*/
private function handleOption($option)
{
$this->tmpPath = $option['tmp'] ?? sys_get_temp_dir();
$this->debug = $option['debug'] ?? true;
}
public function run()
{
$this->getUuid();
$this->generateQrCode();
$this->log('[INFO] 请用微信扫码');
if($this->waitForLogin() !== 200){
die('[ERROR] 微信登录失败');
}
if(!$this->login()){
die('[ERROR] 登录失败');
}
$this->log('[INFO] 登录成功');
if(!$this->init()){
die('[INFO] 微信初始化失败');
}
$this->log('[INFO] 微信初始化成功');
$this->statusNotify();
$this->getContact();
$this->log('[INFO] 获取 ' . count($this->contactList) . ' 个联系人');
$this->log('[INFO] 开始获取信息');
$this->processMsg();
}
/**
* 获取UUID
* @throws \Exception
*/
public function getUuid()
{
$content = $this->client->get('https://login.weixin.qq.com/jslogin', [
'query' => [
'appid' => 'wx782c26e4c19acffb',
'fun' => 'new',
'lang' => 'zh_CN',
'_' => time() * 1000 . random_int(1, 999)
]
])->getBody()->getContents();
preg_match('/window.QRLogin.code = (\d+); window.QRLogin.uuid = \"(\S+?)\"/', $content, $matches);
if(!$matches){
throw new \Exception('获取UUID失败');
}
$this->uuid = $matches[2];
}
/**
* 生成登录二维码
*/
public function generateQrCode()
{
$url = 'https://login.weixin.qq.com/l/' . $this->uuid;
$qrCode = new QrCode($url);
$file = $this->tmpPath . 'login_qr_code.png';
$qrCode->save($file);
if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
system($file);
}
}
/**
* 等待登录
*
* @return int
*/
public function waitForLogin(): int
{
$urlTemplate = 'https://login.weixin.qq.com/cgi-bin/mmwebwx-bin/login?tip=%s&uuid=%s&_=%s';
$retryTime = 10;
$tip = 1;
$code = 0;
while($retryTime > 0){
$url = sprintf($urlTemplate, $tip, $this->uuid, time());
$content = $this->client->get($url, ['query' => []])->getBody()->getContents();
preg_match('/window.code=(\d+);/', $content, $matches);
$code = $matches[1];
switch($code){
case '201':
$this->log('[INFO] 请点击确认进行登录');
$tip = 0;
break;
case '200':
preg_match('/window.redirect_uri="(\S+?)";/', $content, $matches);
$this->redirectUri = $matches[1] . '&fun=new';
return $code;
case '408':
$this->log('[ERROR] 微信登录超时。将在 1 秒后重试');
$tip = 1;
$retryTime -= 1;
sleep(1);
break;
default:
$this->log("[ERROR] 微信登录异常。异常码:$code 。1 秒后重试");
$tip = 1;
$retryTime -= 1;
sleep(1);
break;
}
}
return $code;
}
/**
* 登录
*
* @return bool
*/
public function login()
{
$content = $this->client->get($this->redirectUri)->getBody()->getContents();
$crawler = new Crawler($content);
$this->skey = $crawler->filter('error skey')->text();
$this->sid = $crawler->filter('error wxsid')->text();
$this->uin = $crawler->filter('error wxuin')->text();
$this->passTicket = $crawler->filter('error pass_ticket')->text();
if(in_array('', [$this->skey, $this->sid, $this->uin, $this->passTicket])){
return false;
}
$this->deviceId = 'e' . strval(random_int(100000000000000, 999999999999999));
$this->baseRequest = [
'Uin' => $this->uin,
'Sid' => $this->sid,
'Skey' => $this->skey,
'DeviceID' => $this->deviceId
];
return true;
}
/**
* 初始化
*
* @return bool
*/
public function init()
{
$url = sprintf($this->baseUri . '/webwxinit?r=%i&lang=en_US&pass_ticket=%s', time(), $this->passTicket);
$content = $this->client->post($url, [
'json' => [
'BaseRequest' => $this->baseRequest
]
])->getBody()->getContents();
$result = json_decode($content, true);
$this->generateSyncKey($result);
$this->myAccount = $result['User'];
return $result['BaseResponse']['Ret'] == 0;
}
public function generateSyncKey($result)
{
$this->syncKey = $result['SyncKey'];
$syncKey = [];
foreach ($this->syncKey['List'] as $item) {
$syncKey[] = $item['Key'] . '_' . $item['Val'];
}
$this->syncKeyStr = implode('|', $syncKey);
}
public function statusNotify()
{
$url = sprintf($this->baseUri . '/webwxstatusnotify?lang=zh_CN&pass_ticket=%s', $this->passTicket);
$content = $this->client->post($url, [
'json' => [
'BaseRequest' => $this->baseRequest,
'Code' => 3,
'FromUserName' => $this->myAccount['UserName'],
'ToUserName' => $this->myAccount['UserName'],
'ClientMsgId' => time()
]
])->getBody()->getContents();
$this->debug($content);
return json_decode($content)->BaseResponse->Ret == 0;
}
public function getContact()
{
$url = sprintf($this->baseUri . '/webwxgetcontact?pass_ticket=%s&skey=%s&r=%s', $this->passTicket, $this->skey, time());
$content = $this->client->post($url, ['json' => []])->getBody()->getContents();
if($this->debug){
file_put_contents($this->tmpPath . 'contacts.json', $content);
}
$result = json_decode($content, true);
$this->memberList = $result['MemberList'];
$specialUsers = ['newsapp', 'fmessage', 'filehelper', 'weibo', 'qqmail',
'fmessage', 'tmessage', 'qmessage', 'qqsync', 'floatbottle',
'lbsapp', 'shakeapp', 'medianote', 'qqfriend', 'readerapp',
'blogapp', 'facebookapp', 'masssendapp', 'meishiapp',
'feedsapp', 'voip', 'blogappweixin', 'weixin', 'brandsessionholder',
'weixinreminder', 'wxid_novlwrv3lqwv11', 'gh_22b87fa7cb3c',
'officialaccounts', 'notification_messages', 'wxid_novlwrv3lqwv11',
'gh_22b87fa7cb3c', 'wxitil', 'userexperience_alarm', 'notification_messages'];
foreach ($this->memberList as $contact) {
if($contact['VerifyFlag'] & 8 != 0){ #公众号
$this->publicList[] = $contact;
$this->accountInfo['normalMember'][$contact['UserName']] = ['type' => 'public', 'info' => $contact];
}elseif (in_array($contact['UserName'], $specialUsers)){ # 特殊账户
$this->specialList[] = $contact;
$this->accountInfo['normalMember'][$contact['UserName']] = ['type' => 'special', 'info' => $contact];
}elseif (strstr($contact['UserName'], '@@') !== false){ # 群聊
$this->groupList[] = $contact;
$this->accountInfo['normalMember'][$contact['UserName']] = ['type' => 'group', 'info' => $contact];
}elseif ($contact['UserName'] === $this->myAccount['UserName']){ # 自己
$this->accountInfo['normalMember'][$contact['UserName']] = ['type' => 'self', 'info' => $contact];
}else{
$this->contactList[] = $contact;
$this->accountInfo['normalMember'][$contact['UserName']] = ['type' => 'contact', 'info' => $contact];
}
}
$this->getBatchGroupMembers();
foreach ($this->groupMembers as $key => $group) {
foreach ($group as $member) {
if(!in_array($member['UserName'], $this->accountInfo)){
$this->accountInfo['groupMember'][$member['UserName']] = ['type' => 'group_member', 'info' => $member, 'group' => $group];
}
}
}
if($this->debug){
file_put_contents($this->tmpPath . 'contact_list.json', json_encode($this->contactList));
file_put_contents($this->tmpPath . 'special_list.json', json_encode($this->specialList));
file_put_contents($this->tmpPath . 'group_list.json', json_encode($this->groupList));
file_put_contents($this->tmpPath . 'public_list.json', json_encode($this->publicList));
file_put_contents($this->tmpPath . 'member_list.json', json_encode($this->memberList));
file_put_contents($this->tmpPath . 'group_users.json', json_encode($this->groupMembers));
file_put_contents($this->tmpPath . 'account_info.json', json_encode($this->accountInfo));
}
return true;
}
public function getBatchGroupMembers()
{
$url = sprintf($this->baseUri . '/webwxbatchgetcontact?type=ex&r=%s&pass_ticket=%s', time(), $this->passTicket);
$list = [];
foreach ($this->groupList as $group) {
$list[] = ['UserName' => $group['UserName'], 'EncryChatRoomId' => ''];
}
$content = $this->client->post($url, [
'json' => [
'BaseRequest' => $this->baseRequest,
'Count' => count($this->groupList),
'List' => $list
]
])->getBody()->getContents();
$result = json_decode($content, true);
$groupMembers = [];
$encry = [];
foreach ($result['ContactList'] as $group) {
$gid = $group['UserName'];
$members = $group['MemberList'];
$groupMembers[$gid] = $members;
$encry[$gid] = $group['EncryChatRoomId'];
}
$this->groupMembers = $groupMembers;
$this->encryChatRoomId = $encry;
}
# 消息处理(第二期再分开到另一个类)
public function processMsg()
{
$this->testSyncCheck();
while (true){
list($retCode, $selector) = $this->syncCheck();
if(in_array($retCode, ['1100', '1101'])){ # 微信客户端上登出或者其他设备登录
break;
}elseif ($retCode == '0'){
// if($selector == 2){
// $msg = $this->sync();
// if($msg !== null){
// $this->handleMsg($msg);
// }
// }
$msg = $this->sync();
$this->log('[MSG]' . json_encode($msg));
}
}
}
/**
* 测试域名同步
*
* @return bool
*/
public function testSyncCheck()
{
foreach (['webpush.', 'webpush2.'] as $host) {
$this->syncHost = $host . $this->baseHost;
$retCode = $this->syncCheck()[0];
if($retCode == 0){
return true;
}
return false;
}
}
public function syncCheck()
{
$url = 'https://' . $this->syncHost . '/cgi-bin/mmwebwx-bin/synccheck?' . http_build_query([
'r' => time(),
'sid' => $this->sid,
'uin' => $this->uin,
'skey' => $this->skey,
'deviceid' => $this->deviceId,
'synckey' => $this->syncKeyStr,
'_' => time()
]);
try{
$content = $this->client->get($url, [
'connect_timeout' => 60
])->getBody()->getContents();
preg_match('/window.synccheck=\{retcode:"(\d+)",selector:"(\d+)"\}/', $content, $matches);
return [$matches[1], $matches[2]];
}catch (Exception $e){
return [-1, -1];
}
}
public function sync()
{
$url = sprintf($this->baseUri . '/webwxsync?sid=%s&skey=%s&lang=en_US&pass_ticket=%s', $this->sid, $this->skey, $this->passTicket);
try{
$content = $this->client->post($url, [
'json' => [
'BaseRequest' => $this->baseRequest,
'SyncKey' => $this->syncKey,
'rr' => ~time()
],
'connect_timeout' => 60
])->getBody()->getContents();
$result = json_decode($content, true);
if($result['BaseResponse']['Ret'] == 0){
$this->generateSyncKey($result);
}
return $result;
}catch (\Exception $e){
return null;
}
}
public function handleMsg($message)
{
foreach ($message['AddMsgList'] as $msg) {
$user = ['id' => $msg['FromUserName'], 'name' => 'unknown'];
if($msg['MsgType'] == 51){
$msgTypeId = 0;
$user['name'] = 'system';
}elseif ($msg['MsgType'] == 37){
$msgTypeId = 37;
continue;
}elseif ($msg['FromUserName'] === $this->myAccount['UserName']){
$msgTypeId = 1;
$user['name'] = 'self';
}elseif ($msg['ToUserName'] === 'filehelper'){
$msgTypeId = 2;
$user['name'] = 'file_helper';
}elseif (substr($msg['FromUserName'], 0, 2) === '@@'){
$msgTypeId = 3;
$user['name'] = $this->getContactPreferName($this->getContactName($user['id']));
}elseif ($this->isBelong($msg['FromUserName'], $this->contactList)){
$msgTypeId = 4;
$user['name'] = $this->getContactPreferName($this->getContactName($user['id']));
}elseif($this->isBelong($msg['FromUserName'], $this->publicList)){
$msgTypeId = 5;
$user['name'] = $this->getContactPreferName($this->getContactName($user['id']));
}elseif($this->isBelong($msg['FromUserName'], $this->specialList)){
$msgTypeId = 6;
$user['name'] = $this->getContactPreferName($this->getContactName($user['id']));
}else{
$msgTypeId = 99;
}
$user['name'] = html_entity_decode($user['name']);
if($this->debug && $msgTypeId !== 0){
$this->log("[MSG] {$user['name']} :");
}
}
}
/**
* 获取联系人名称数组
*
* @param $uid
* @return array|null
*/
public function getContactName($uid)
{
$info = $this->getContactInfo($uid);
if($info === null){
return null;
}
$name = [];
$info = $info['info'];
if($info['RemarkName']){
$name['remarkName'] = $info['RemarkName'];
}
if($info['NickName']){
$name['nickname'] = $info['NickName'];
}
if($info['DisplayName']){
$name['displayName'] = $info['DisplayName'];
}
return count($name) === 0 ? null : $name;
}
/**
* 根据ID获取用户信息
*
* @param $uid
* @return null
*/
public function getContactInfo($uid)
{
return $this->accountInfo['normalMember'][$uid] ?? null;
}
/**
* 根据优先级获取用户名称
*
* @param $name
* @return null
*/
public function getContactPreferName($name)
{
if(!$name){
return null;
}
return $name['remarkName'] ?? $name['nickname'] ?? $name['displayName'] ?? null;
}
/**
* 是否联系人
*
* @param $uid
* @param $list
* @return bool
*/
public function isBelong($uid, $list)
{
foreach ($list as $contact) {
if($uid === $contact['UserName']){
return true;
}
}
return false;
}
public function extractMsgContent($msgTypeId, $msg)
{
$content = html_entity_decode($msg['Content']);
$msgContent = [];
if($msgTypeId === 0){
return ['type' => 11, 'data' => ''];
}elseif($msgTypeId === 2){
return ['type' => 0, 'data' => str_replace('<br/>', '\n', $content)];
}elseif ($msgTypeId === 3){
$spilt = explode('<br/>', $content, 2);
$uid = substr($spilt[0], 0, -1);
$content = str_replace('<br/>', '', $content);
$name = $this->getContactPreferName($this->getContactName($uid));
$name = $name ?? $this->getGroupMemberPreferName($this->getGroupMemberName($msg['FromUserName'], $uid)) ?? 'unknown';
$msgContent['user'] = ['id' => $uid, 'name' => $name];
}
$msgPrefix = isset($msgContent['user']) ? $msgContent['user']['name'] . ':' : '';
if($msg['MsgType'] == 1){
if(strstr($content, 'http://weixin.qq.com/cgi-bin/redirectforward?args=') !== false){
}
}
}
public function getGroupMemberPreferName($name)
{
if(!$name){
return null;
}
return $name['remarkName'] ?? $name['displayName'] ?? $name['nickname'] ?? null;
}
/**
* 获取群聊用户名称
*
* @param $gid
* @param $uid
* @return array|null
*/
public function getGroupMemberName($gid, $uid)
{
if(!isset($this->groupMembers)){
return null;
}
$group = $this->groupMembers[$gid];
foreach ($group as $member) {
if($member['UserName'] === $uid){
$name = [];
if($member['RemarkName']){
$name['remarkName'] = $member['RemarkName'];
}
if($member['NickName']){
$name['nickname'] = $member['NickName'];
}
if($member['DisplayName']){
$name['displayName'] = $member['DisplayName'];
}
return $name;
}
}
return null;
}
private function debug($content)
{
file_put_contents($this->tmpPath . 'debug.json', $content);
}
private function log($msg)
{
echo $msg . PHP_EOL;
}
public function __get($value)
{
return $this->$value;
}
}