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
|
|
|
/** |
13
|
|
|
* MessageBuilder.php. |
14
|
|
|
* |
15
|
|
|
* @author overtrue <[email protected]> |
16
|
|
|
* @copyright 2015 overtrue <[email protected]> |
17
|
|
|
* |
18
|
|
|
* @link https://github.com/overtrue |
19
|
|
|
* @link http://overtrue.me |
20
|
|
|
*/ |
21
|
|
|
namespace EasyWeChat\Broadcast; |
22
|
|
|
|
23
|
|
|
use EasyWeChat\Core\Exceptions\InvalidArgumentException; |
24
|
|
|
use EasyWeChat\Core\Exceptions\RuntimeException; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Class MessageBuilder. |
28
|
|
|
*/ |
29
|
|
|
class MessageBuilder |
30
|
|
|
{ |
31
|
|
|
/** |
32
|
|
|
* Message target user or group. |
33
|
|
|
* |
34
|
|
|
* @var mixed |
35
|
|
|
*/ |
36
|
|
|
protected $to; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Message type. |
40
|
|
|
* |
41
|
|
|
* @var string |
42
|
|
|
*/ |
43
|
|
|
protected $msgType; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Message. |
47
|
|
|
* |
48
|
|
|
* @var mixed |
49
|
|
|
*/ |
50
|
|
|
protected $message; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Message types. |
54
|
|
|
* |
55
|
|
|
* @var array |
56
|
|
|
*/ |
57
|
|
|
private $msgTypes = [ |
58
|
|
|
Broadcast::MSG_TYPE_TEXT, |
59
|
|
|
Broadcast::MSG_TYPE_NEWS, |
60
|
|
|
Broadcast::MSG_TYPE_IMAGE, |
61
|
|
|
Broadcast::MSG_TYPE_VIDEO, |
62
|
|
|
Broadcast::MSG_TYPE_VOICE, |
63
|
|
|
Broadcast::MSG_TYPE_CARD, |
64
|
|
|
]; |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Preview bys. |
68
|
|
|
* |
69
|
|
|
* @var array |
70
|
|
|
*/ |
71
|
|
|
private $previewBys = [ |
72
|
|
|
Broadcast::PREVIEW_BY_OPENID, |
73
|
|
|
Broadcast::PREVIEW_BY_NAME, |
74
|
|
|
]; |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Set message type. |
78
|
|
|
* |
79
|
|
|
* @param string $msgType |
80
|
|
|
* |
81
|
|
|
* @return MessageBuilder |
82
|
|
|
* |
83
|
|
|
* @throws InvalidArgumentException |
84
|
|
|
*/ |
85
|
5 |
|
public function msgType($msgType) |
86
|
|
|
{ |
87
|
5 |
|
if (!in_array($msgType, $this->msgTypes, true)) { |
88
|
1 |
|
throw new InvalidArgumentException('This message type not exist.'); |
89
|
|
|
} |
90
|
|
|
|
91
|
5 |
|
$this->msgType = $msgType; |
92
|
|
|
|
93
|
5 |
|
return $this; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Set message. |
98
|
|
|
* |
99
|
|
|
* @param string|array $message |
100
|
|
|
* |
101
|
|
|
* @return MessageBuilder |
102
|
|
|
*/ |
103
|
5 |
|
public function message($message) |
104
|
|
|
{ |
105
|
5 |
|
$this->message = $message; |
106
|
|
|
|
107
|
5 |
|
return $this; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Set target user or group. |
112
|
|
|
* |
113
|
|
|
* @param mixed $to |
114
|
|
|
* |
115
|
|
|
* @return MessageBuilder |
116
|
|
|
*/ |
117
|
5 |
|
public function to($to) |
118
|
|
|
{ |
119
|
5 |
|
$this->to = $to; |
120
|
|
|
|
121
|
5 |
|
return $this; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Build message. |
126
|
|
|
* |
127
|
|
|
* @return bool |
128
|
|
|
* |
129
|
|
|
* @throws RuntimeException |
130
|
|
|
*/ |
131
|
3 |
|
public function build() |
132
|
|
|
{ |
133
|
3 |
|
if (empty($this->msgType)) { |
134
|
2 |
|
throw new RuntimeException('message type not exist.'); |
135
|
|
|
} |
136
|
|
|
|
137
|
2 |
|
if (empty($this->message)) { |
138
|
|
|
throw new RuntimeException('No message content to send.'); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
// 群发视频消息给用户列表时,视频消息格式需要另外处理,具体见文档 |
142
|
2 |
|
if (isset($this->to) && is_array($this->to) && $this->msgType === Broadcast::MSG_TYPE_VIDEO) { |
143
|
1 |
|
$this->msgType = 'video'; |
144
|
2 |
|
} elseif ($this->msgType === Broadcast::MSG_TYPE_VIDEO) { |
145
|
1 |
|
$this->msgType = 'mpvideo'; |
146
|
1 |
|
} |
147
|
|
|
|
148
|
2 |
|
$content = (new Transformer($this->msgType, $this->message))->transform(); |
149
|
|
|
|
150
|
2 |
|
$group = isset($this->to) ? $this->to : null; |
151
|
|
|
|
152
|
2 |
|
$message = array_merge($this->buildGroup($group), $content); |
153
|
|
|
|
154
|
2 |
|
return $message; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* Build preview message. |
159
|
|
|
* |
160
|
|
|
* @param string $by |
161
|
|
|
* |
162
|
|
|
* @return array |
163
|
|
|
* |
164
|
|
|
* @throws RuntimeException |
165
|
|
|
* @throws InvalidArgumentException |
166
|
|
|
*/ |
167
|
2 |
|
public function buildPreview($by) |
168
|
|
|
{ |
169
|
2 |
|
if (!in_array($by, $this->previewBys, true)) { |
170
|
|
|
throw new InvalidArgumentException('This preview by not exist.'); |
171
|
|
|
} |
172
|
|
|
|
173
|
2 |
|
if (empty($this->msgType)) { |
174
|
|
|
throw new RuntimeException('Message type not exist.'); |
175
|
|
|
} |
176
|
|
|
|
177
|
2 |
|
if (empty($this->message)) { |
178
|
|
|
throw new RuntimeException('No message content to send.'); |
179
|
|
|
} |
180
|
|
|
|
181
|
2 |
|
if (empty($this->to)) { |
182
|
|
|
throw new RuntimeException('No to.'); |
183
|
|
|
} |
184
|
|
|
|
185
|
2 |
|
$content = (new Transformer($this->msgType, $this->message))->transform(); |
186
|
|
|
|
187
|
2 |
|
$message = array_merge($this->buildTo($this->to, $by), $content); |
188
|
|
|
|
189
|
2 |
|
return $message; |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* Build group. |
194
|
|
|
* |
195
|
|
|
* @param mixed $group |
196
|
|
|
* |
197
|
|
|
* @return array |
198
|
|
|
*/ |
199
|
2 |
|
private function buildGroup($group) |
200
|
|
|
{ |
201
|
2 |
|
if (is_null($group)) { |
202
|
|
|
$group = [ |
203
|
|
|
'filter' => [ |
204
|
2 |
|
'is_to_all' => true, |
205
|
2 |
|
], |
206
|
2 |
|
]; |
207
|
2 |
|
} elseif (is_array($group)) { |
208
|
|
|
$group = [ |
209
|
2 |
|
'touser' => $group, |
210
|
2 |
|
]; |
211
|
2 |
|
} else { |
212
|
|
|
$group = [ |
213
|
|
|
'filter' => [ |
214
|
|
|
'is_to_all' => false, |
215
|
|
|
'group_id' => $group, |
216
|
|
|
], |
217
|
|
|
]; |
218
|
|
|
} |
219
|
|
|
|
220
|
2 |
|
return $group; |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
/** |
224
|
|
|
* Build to. |
225
|
|
|
* |
226
|
|
|
* @param string $to |
227
|
|
|
* @param string $by |
228
|
|
|
* |
229
|
|
|
* @return array |
230
|
|
|
*/ |
231
|
2 |
|
private function buildTo($to, $by) |
232
|
|
|
{ |
233
|
|
|
return [ |
234
|
2 |
|
$by => $to, |
235
|
2 |
|
]; |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
/** |
239
|
|
|
* Return property. |
240
|
|
|
* |
241
|
|
|
* @param string $property |
242
|
|
|
* |
243
|
|
|
* @return mixed |
244
|
|
|
*/ |
245
|
3 |
|
public function __get($property) |
246
|
|
|
{ |
247
|
3 |
|
if (property_exists($this, $property)) { |
248
|
3 |
|
return $this->$property; |
249
|
|
|
} |
250
|
|
|
} |
251
|
|
|
} |
252
|
|
|
|