|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the overtrue/wechat. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) overtrue <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* This source file is subject to the MIT license that is bundled |
|
9
|
|
|
* with this source code in the file LICENSE. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace EasyWeChat\OfficialAccount\CustomerService; |
|
13
|
|
|
|
|
14
|
|
|
use EasyWeChat\Kernel\Exceptions\RuntimeException; |
|
15
|
|
|
use EasyWeChat\Kernel\Messages\Message; |
|
16
|
|
|
use EasyWeChat\Kernel\Messages\Raw as RawMessage; |
|
17
|
|
|
use EasyWeChat\Kernel\Messages\Text; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Class MessageBuilder. |
|
21
|
|
|
* |
|
22
|
|
|
* @author overtrue <[email protected]> |
|
23
|
|
|
*/ |
|
24
|
|
|
class Messenger |
|
25
|
|
|
{ |
|
26
|
|
|
/** |
|
27
|
|
|
* Messages to send. |
|
28
|
|
|
* |
|
29
|
|
|
* @var \EasyWeChat\Kernel\Messages\Message; |
|
30
|
|
|
*/ |
|
31
|
|
|
protected $message; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Messages target user open id. |
|
35
|
|
|
* |
|
36
|
|
|
* @var string |
|
37
|
|
|
*/ |
|
38
|
|
|
protected $to; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Messages sender staff id. |
|
42
|
|
|
* |
|
43
|
|
|
* @var string |
|
44
|
|
|
*/ |
|
45
|
|
|
protected $account; |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Customer service instance. |
|
49
|
|
|
* |
|
50
|
|
|
* @var \EasyWeChat\OfficialAccount\CustomerService\Client |
|
51
|
|
|
*/ |
|
52
|
|
|
protected $client; |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* MessageBuilder constructor. |
|
56
|
|
|
* |
|
57
|
|
|
* @param \EasyWeChat\OfficialAccount\CustomerService\Client $client |
|
58
|
|
|
*/ |
|
59
|
4 |
|
public function __construct(Client $client) |
|
60
|
|
|
{ |
|
61
|
4 |
|
$this->client = $client; |
|
62
|
4 |
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Set message to send. |
|
66
|
|
|
* |
|
67
|
|
|
* @param string|Message $message |
|
68
|
|
|
* |
|
69
|
|
|
* @return Messenger |
|
70
|
|
|
*/ |
|
71
|
3 |
|
public function message($message) |
|
72
|
|
|
{ |
|
73
|
3 |
|
if (is_string($message)) { |
|
74
|
2 |
|
$message = new Text($message); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
3 |
|
$this->message = $message; |
|
78
|
|
|
|
|
79
|
3 |
|
return $this; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Set staff account to send message. |
|
84
|
|
|
* |
|
85
|
|
|
* @param string $account |
|
86
|
|
|
* |
|
87
|
|
|
* @return Messenger |
|
88
|
|
|
*/ |
|
89
|
1 |
|
public function by(string $account) |
|
90
|
|
|
{ |
|
91
|
1 |
|
$this->account = $account; |
|
92
|
|
|
|
|
93
|
1 |
|
return $this; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* @param string $account |
|
98
|
|
|
* |
|
99
|
|
|
* @return Messenger |
|
100
|
|
|
*/ |
|
101
|
1 |
|
public function from(string $account) |
|
102
|
|
|
{ |
|
103
|
1 |
|
return $this->by($account); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* Set target user open id. |
|
108
|
|
|
* |
|
109
|
|
|
* @param string $openid |
|
110
|
|
|
* |
|
111
|
|
|
* @return Messenger |
|
112
|
|
|
*/ |
|
113
|
2 |
|
public function to($openid) |
|
114
|
|
|
{ |
|
115
|
2 |
|
$this->to = $openid; |
|
116
|
|
|
|
|
117
|
2 |
|
return $this; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* Send the message. |
|
122
|
|
|
* |
|
123
|
|
|
* @return \Psr\Http\Message\ResponseInterface|\EasyWeChat\Kernel\Support\Collection|array|object|string |
|
124
|
|
|
* |
|
125
|
|
|
* @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException |
|
126
|
|
|
* @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException |
|
127
|
|
|
* @throws \EasyWeChat\Kernel\Exceptions\RuntimeException |
|
128
|
|
|
*/ |
|
129
|
3 |
|
public function send() |
|
130
|
|
|
{ |
|
131
|
3 |
|
if (empty($this->message)) { |
|
132
|
1 |
|
throw new RuntimeException('No message to send.'); |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
2 |
|
if ($this->message instanceof RawMessage) { |
|
136
|
1 |
|
$message = json_decode($this->message->get('content'), true); |
|
137
|
|
|
} else { |
|
138
|
|
|
$prepends = [ |
|
139
|
1 |
|
'touser' => $this->to, |
|
140
|
|
|
]; |
|
141
|
1 |
|
if ($this->account) { |
|
142
|
1 |
|
$prepends['customservice'] = ['kf_account' => $this->account]; |
|
143
|
|
|
} |
|
144
|
1 |
|
$message = $this->message->transformForJsonRequest($prepends); |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
2 |
|
return $this->client->send($message); |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
/** |
|
151
|
|
|
* Return property. |
|
152
|
|
|
* |
|
153
|
|
|
* @param string $property |
|
154
|
|
|
* |
|
155
|
|
|
* @return mixed |
|
156
|
|
|
*/ |
|
157
|
1 |
|
public function __get(string $property) |
|
158
|
|
|
{ |
|
159
|
1 |
|
if (property_exists($this, $property)) { |
|
160
|
1 |
|
return $this->$property; |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
1 |
|
return null; |
|
164
|
|
|
} |
|
165
|
|
|
} |
|
166
|
|
|
|