Message.php
10 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
<?php
/**
* Created by PhpStorm.
* User: Hanson
* Date: 2016/12/15
* Time: 0:12
*/
namespace Hanson\Robot\Message;
use Carbon\Carbon;
use Hanson\Robot\Core\Server;
use Hanson\Robot\Collections\Contact;
use Hanson\Robot\Collections\OfficialAccount;
use Hanson\Robot\Collections\SpecialAccount;
use Hanson\Robot\Support\FileManager;
use Hanson\Robot\Support\Console;
use Hanson\Robot\Support\ObjectAble;
class Message
{
use ObjectAble;
/**
* @var array 消息来源
*/
public $from;
/**
* @var array 当from为群组时,sender为用户发送者
*/
public $sender;
/**
* @var string 来源的username,用于回复
*/
public $username;
/**
* @var array 消息接收者
*/
public $to;
/**
* @var string 经过处理的内容
*/
public $content;
/**
* @var Carbon 时间
*/
public $time;
/**
* @var string 消息发送者类型
*/
public $fromType;
/**
* @var string 消息类型
*/
public $type;
public $isAt = false;
const USER_TYPE = [
0 => 'Init',
1 => 'Self',
2 => 'FileHelper',
3 => 'Group',
4 => 'Contact',
5 => 'Public',
6 => 'Special',
99 => 'UnKnown',
];
public $rawMsg;
public $msg;
static $mediaCount = -1;
public function __construct($msg)
{
$this->msg = $msg;
$this->setFrom();
$this->setTo();
}
// public function make($selector, $msg)
// {
// $this->rawMsg = $msg;
// $this->time = Carbon::now();
//
// $this->setFrom();
// $this->setTo();
// $this->setFromType();
// $this->setType();
// $this->rawMsg['selector'] = $selector;
// $this->addMessageCollection();
// return $this;
// }
/**
* 设置消息发送者
*/
private function setFrom()
{
$from = account()->getAccount($this->msg['FromUserName']);
$this->from = $this->toObject($from);
}
private function setTo()
{
$to = account()->getAccount($this->msg['ToUserName']);
print_r($to);
$this->to = $this->toObject($to);
}
private function setFromType()
{
if ($this->rawMsg['MsgType'] == 51) {
$this->fromType = 'System';
} elseif ($this->rawMsg['MsgType'] == 37) {
$this->fromType = 'FriendRequest';
} elseif ($this->rawMsg['FromUserName'] === myself()->username) {
$this->fromType = 'Self';
} elseif ($this->rawMsg['ToUserName'] === 'filehelper') {
$this->fromType = 'FileHelper';
} elseif (substr($this->rawMsg['FromUserName'], 0, 2) === '@@') { # group
$this->fromType = 'Group';
} elseif (contact()->getContactByUsername($this->rawMsg['FromUserName'])) {
$this->fromType = 'Contact';
} elseif (OfficialAccount::getInstance()->isPublic($this->rawMsg['FromUserName'])) {
$this->fromType = 'Official';
} elseif (SpecialAccount::getInstance()->get($this->rawMsg['FromUserName'], false)) {
$this->fromType = 'Special';
} else {
$this->fromType = 'Unknown';
}
}
private function setType()
{
$this->rawMsg['Content'] = html_entity_decode($this->rawMsg['Content']);
$this->setTypeByFrom();
$this->handleMessageByType();
}
/**
* 根据消息来源处理消息
*/
private function setTypeByFrom()
{
if($this->fromType === 'System'){
$this->type = 'Empty';
}elseif ($this->fromType === 'FileHelper'){ # File Helper
$this->type = 'Text';
$this->content = $this->formatContent($this->rawMsg['Content']);
}elseif ($this->fromType === 'Group'){
$this->handleGroupContent($this->rawMsg['Content']);
}
}
/**
* 处理消息类型
*/
private function handleMessageByType()
{
switch($this->rawMsg['MsgType']){
case 1: //文本消息
if(Location::isLocation($this->rawMsg)){
$this->type = 'Location';
$this->content = Location::getLocationText($this->rawMsg['Content']);
}else{
$this->type = 'Text';
$this->content = $this->rawMsg['Content'];
}
break;
case 3: // 图片消息
$this->type = 'Image';
$this->content = Server::BASE_URI . sprintf('/webwxgetmsgimg?MsgID=%s&skey=%s', $this->rawMsg['MsgId'], server()->skey);
$content = http()->get($this->content);
FileManager::download($this->rawMsg['MsgId'].'.jpg', $content, 'jpg');
break;
case 34: // 语音消息
$this->type = 'Voice';
$this->content = Server::BASE_URI . sprintf('/webwxgetvoice?msgid=%s&skey=%s', $this->rawMsg['MsgId'], server()->skey);
$content = http()->get($this->content);
FileManager::download($this->rawMsg['MsgId'].'.mp3', $content, 'mp3');
break;
case 37: // 好友验证
$this->type = 'AddUser';
break;
case 42: //共享名片
$this->type = 'Recommend';
$this->content = (object)$this->rawMsg['RecommendInfo'];
break;
case 43:
$this->type = 'VideoCall';
Console::log('video');
$url = Server::BASE_URI . sprintf('/webwxgetvideo?msgid=%s&skey=%s', $this->rawMsg['MsgId'], server()->skey);
$content = http()->request($url, 'get', [
'headers' => [
'Range' => 'bytes=0-'
]
]);
FileManager::download($this->rawMsg['MsgId'].'.mp4', $content, 'mp4');
break;
case 47: // 动画表情
$this->type = 'Emoticon';
$this->content = Server::BASE_URI . sprintf('/webwxgetmsgimg?MsgID=%s&skey=%s', $this->rawMsg['MsgId'], server()->skey);
$content = http()->get($this->content);
FileManager::download($this->rawMsg['MsgId'].'.gif', $content, 'gif');
break;
case 49:
$this->type = 'Share';
break;
case 62:
$this->type = 'Video';
Console::log('video');
$url = Server::BASE_URI . sprintf('/webwxgetvideo?msgid=%s&skey=%s', $this->rawMsg['MsgId'], server()->skey);
$content = http()->request($url, 'get', [
'headers' => [
'Range' => 'bytes=0-'
]
]);
FileManager::download($this->rawMsg['MsgId'].'.mp4', $content, 'mp4');
break;
case 51:
$this->type = 'Init';
break;
case 53:
$this->type = 'VideoCall';
break;
case 10000:
if($this->rawMsg['Status'] == 4){
$this->type = 'RedPacket'; // 红包
}else{
$this->type = 'Unknown';
}
break;
case 10002:
$this->type = 'Recall'; // 撤回
$this->msgId = $msgId = $this->parseMsgId($this->rawMsg['Content']);
break;
default:
$this->type = 'Unknown';
break;
}
}
/**
* 处理群发消息的内容
*
* @param $content string 内容
*/
private function handleGroupContent($content)
{
if(!$content || !str_contains($content, '<br/>')){
return;
}
list($uid, $content) = explode('<br/>', $content, 2);
$this->sender = member()->getMemberByUsername(substr($uid, 0, -1));
$this->rawMsg['Content'] = $this->formatContent($content);
$this->isAt = str_contains($this->rawMsg['Content'], '@'.myself()->nickname);
}
private function formatContent($content)
{
return str_replace('<br/>', "\n", $content);
}
/**
* 解析message获取msgId
*
* @param $xml
* @return string msgId
*/
private function parseMsgId($xml)
{
preg_match('/<msgid>(\d+)<\/msgid>/', $xml, $matches);
return $matches[1];
}
/**
* 存储消息到 Message 集合
*/
public function addMessageCollection()
{
message()->put($this->rawMsg['MsgId'], [
'content' => $this->content,
'username' => $this->username,
'sender' => $this->sender,
'msg_type' => $this->rawMsg['MsgType'],
'type' => $this->type,
'created_at' => $this->rawMsg['CreateTime'],
'from_type' => $this->fromType
]);
}
/**
* 发送消息
*
* @param $word string 消息内容
* @param $username string 目标username
* @return bool
*/
public static function send(string $word, $username)
{
if(!$word && !is_string($word)){
return false;
}
Console::log($word);
$random = strval(time() * 1000) . '0' . strval(rand(100, 999));
$data = [
'BaseRequest' => server()->baseRequest,
'Msg' => [
'Type' => 1,
'Content' => $word,
'FromUserName' => myself()->username,
'ToUserName' => $username,
'LocalID' => $random,
'ClientMsgId' => $random,
],
'Scene' => 0
];
$result = http()->post(Server::BASE_URI . '/webwxsendmsg?pass_ticket=' . server()->passTicket,
json_encode($data, JSON_UNESCAPED_UNICODE), true
);
if($result['BaseResponse']['Ret'] != 0){
Console::log('发送消息失败');
return false;
}
return true;
}
public function __toString()
{
return $this->content;
}
}