MessageHandler.php
5.14 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
<?php
/**
* Created by PhpStorm.
* User: Hanson
* Date: 2016/12/14
* Time: 23:08
*/
namespace Hanson\Robot\Core;
use Closure;
use Hanson\Robot\Message\Message;
use Hanson\Robot\Support\Log;
class MessageHandler
{
protected $server;
private $syncHost;
private $handler;
static $instance = null;
const MESSAGE_MAP = [
2 => 'text', // 新消息
3 => 'unknown', // 未知
4 => 'contactUpdate', // 通讯录更新
6 => 'money', // 可能是红包
7 => 'mobile' // 手机上操作了微信
];
public function __construct(Server $server)
{
$this->server = $server;
}
/**
* get a message handler single instance
*
* @param Server $server
* @return MessageHandler
*/
public static function getInstance($server = null)
{
if(static::$instance === null){
static::$instance = new MessageHandler($server);
}
return static::$instance;
}
public function setMessageHandler(Closure $closure)
{
if(!$closure instanceof Closure){
throw new \Exception('message handler must be a closure!');
}
$this->handler = $closure;
}
/**
* listen the chat api
*/
public function listen()
{
$this->preCheckSync();
while (true){
$time = time();
list($retCode, $selector) = $this->checkSync();
if(in_array($retCode, ['1100', '1101'])){ # 微信客户端上登出或者其他设备登录
break;
}elseif ($retCode == 0){
$this->handlerMessage($selector);
}else{
$this->debugMessage($retCode, $selector, 10);
}
$this->checkTime($time);
}
// call_user_func_array($this->handler, []);
}
private function handlerMessage($selector)
{
if($selector === 0){
return;
}
$message = $this->sync();
foreach ($message['AddMsgList'] as $msg) {
Log::echo((new Message)->make($selector, $msg)->content->msg);
}
Log::echo(json_encode($message));
// $messages = (new Message)->make($selector, $message);
//
// foreach ($messages as $message) {
// call_user_func_array($this->handler, [$message]);
// }
}
/**
* get a message code
*
* @return array
*/
private function checkSync()
{
$url = 'https://' . $this->syncHost . '/cgi-bin/mmwebwx-bin/synccheck?' . http_build_query([
'r' => time(),
'sid' => $this->server->sid,
'uin' => $this->server->uin,
'skey' => $this->server->skey,
'deviceid' => $this->server->deviceId,
'synckey' => $this->server->syncKeyStr,
'_' => time()
]);
try{
$content = $this->server->http->get($url);
preg_match('/window.synccheck=\{retcode:"(\d+)",selector:"(\d+)"\}/', $content, $matches);
return [$matches[1], $matches[2]];
}catch (\Exception $e){
return [-1, -1];
}
}
/**
* test a domain before sync
*
* @return bool
*/
private function preCheckSync()
{
foreach (['webpush.', 'webpush2.'] as $host) {
$this->syncHost = $host . Server::BASE_HOST;
list($retCode,) = $this->checkSync();
if($retCode == 0){
return true;
}
}
return false;
}
private function sync()
{
$url = sprintf(Server::BASE_URI . '/webwxsync?sid=%s&skey=%s&lang=en_US&pass_ticket=%s', $this->server->sid, $this->server->skey, $this->server->passTicket);
try{
$result = $this->server->http->json($url, [
'BaseRequest' => $this->server->baseRequest,
'SyncKey' => $this->server->syncKey,
'rr' => ~time()
], true);
if($result['BaseResponse']['Ret'] == 0){
$this->generateSyncKey($result);
}
return $result;
}catch (\Exception $e){
return null;
}
}
/**
* generate a sync key
*
* @param $result
*/
private function generateSyncKey($result)
{
$this->server->syncKey = $result['SyncKey'];
$syncKey = [];
foreach ($this->server->syncKey['List'] as $item) {
$syncKey[] = $item['Key'] . '_' . $item['Val'];
}
$this->server->syncKeyStr = implode('|', $syncKey);
}
/**
* check message time
*
* @param $time
*/
private function checkTime($time)
{
$checkTime = time() - $time;
if($checkTime < 0.8){
sleep(1 - $checkTime);
}
}
/**
* debug while the sync
*
* @param $retCode
* @param $selector
* @param null $sleep
*/
private function debugMessage($retCode, $selector, $sleep = null)
{
Log::echo('[DEBUG] retcode:' . $retCode . ' selector:' . $selector);
if($sleep){
sleep($sleep);
}
}
}