Message.php
2.58 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
<?php
/**
* Created by PhpStorm.
* User: Hanson
* Date: 2016/12/15
* Time: 0:12
*/
namespace Hanson\Vbot\Message\Entity;
use Carbon\Carbon;
use Hanson\Vbot\Core\Server;
use Hanson\Vbot\Collections\Contact;
use Hanson\Vbot\Collections\Official;
use Hanson\Vbot\Collections\Special;
use Hanson\Vbot\Support\Content;
use Hanson\Vbot\Support\FileManager;
use Hanson\Vbot\Support\Console;
class Message
{
/**
* @var array 消息来源
*/
public $from;
/**
* @var array 当from为群组时,sender为用户发送者
*/
public $sender;
/**
* @var string 经过处理的内容
*/
public $content;
/**
* @var Carbon 时间
*/
public $time;
/**
* @var string 消息发送者类型
*/
public $fromType;
public $msg;
static $mediaCount = -1;
public function __construct($msg)
{
$this->msg = $msg;
$this->setFrom();
$this->setFromType();
$this->msg['Content'] = Content::formatContent($this->msg['Content']);
if($this->fromType === 'Group'){
$this->handleGroupContent($this->msg['Content']);
}
$this->time = $msg['CreateTime'];
}
/**
* 设置消息发送者
*/
private function setFrom()
{
$this->from = account()->getAccount($this->msg['FromUserName']);
}
private function setFromType()
{
if ($this->msg['MsgType'] == 51) {
$this->fromType = 'System';
} elseif ($this->msg['FromUserName'] === myself()->username) {
$this->fromType = 'Self';
} elseif (substr($this->msg['FromUserName'], 0, 2) === '@@') { # group
$this->fromType = 'Group';
} elseif (contact()->get($this->msg['FromUserName'])) {
$this->fromType = 'Contact';
} elseif (official()->get($this->msg['FromUserName'])) {
$this->fromType = 'Official';
} elseif (Special::getInstance()->get($this->msg['FromUserName'], false)) {
$this->fromType = 'Special';
} else {
$this->fromType = 'Unknown';
}
}
/**
* 处理群发消息的内容
*
* @param $content string 内容
*/
private function handleGroupContent($content)
{
if(!$content || !str_contains($content, ":\n")){
return;
}
list($uid, $content) = explode(":\n", $content, 2);
$this->sender = account()->getAccount($uid);
$this->msg['Content'] = Content::replaceBr($content);
}
public function __toString()
{
return $this->content;
}
}