Text.php
1.61 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
<?php
/**
* Created by PhpStorm.
* User: Hanson
* Date: 2016/12/16
* Time: 18:33
*/
namespace Hanson\Vbot\Message\Entity;
use Hanson\Vbot\Message\MessageInterface;
use Hanson\Vbot\Support\Console;
class Text extends Message implements MessageInterface
{
public $isAt;
public function __construct($msg)
{
parent::__construct($msg);
$this->make();
}
/**
* 发送消息
*
* @param $word string 消息内容
* @param $username string 目标username
* @return bool
*/
public static function send($username, string $word)
{
if(!$word){
return false;
}
$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()->baseUri . '/webwxsendmsg?pass_ticket=' . server()->passTicket,
json_encode($data, JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES), true
);
if($result['BaseResponse']['Ret'] != 0){
Console::log('发送消息失败');
return false;
}
return true;
}
public function make()
{
$this->content = $this->msg['Content'];
$this->isAt = str_contains($this->content, '@'.myself()->nickname);
}
}