1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace EntWeChat\Broadcast; |
4
|
|
|
|
5
|
|
|
use EntWeChat\Core\Exceptions\RuntimeException; |
6
|
|
|
use EntWeChat\Message\Raw as RawMessage; |
7
|
|
|
use EntWeChat\Message\Text; |
8
|
|
|
use EntWeChat\Support\Arr; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Class MessageBuilder. |
12
|
|
|
*/ |
13
|
|
|
class MessageBuilder |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* Message target user or group. |
17
|
|
|
* |
18
|
|
|
* @var array |
19
|
|
|
*/ |
20
|
|
|
protected $to = []; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Message type. |
24
|
|
|
* |
25
|
|
|
* @var string |
26
|
|
|
*/ |
27
|
|
|
protected $msgType; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Agent id. |
31
|
|
|
* |
32
|
|
|
* @var mixed |
33
|
|
|
*/ |
34
|
|
|
protected $agentId; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Message. |
38
|
|
|
* |
39
|
|
|
* @var mixed |
40
|
|
|
*/ |
41
|
|
|
protected $message; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Safe message. |
45
|
|
|
* |
46
|
|
|
* @var mixed |
47
|
|
|
*/ |
48
|
|
|
protected $safe = 0; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Broadcast instance. |
52
|
|
|
* |
53
|
|
|
* @var \EntWeChat\Broadcast\Broadcast |
54
|
|
|
*/ |
55
|
|
|
protected $broadcast; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Message types. |
59
|
|
|
* |
60
|
|
|
* @var array |
61
|
|
|
*/ |
62
|
|
|
private $msgTypes = [ |
|
|
|
|
63
|
|
|
Broadcast::MSG_TYPE_TEXT, |
64
|
|
|
Broadcast::MSG_TYPE_NEWS, |
65
|
|
|
Broadcast::MSG_TYPE_IMAGE, |
66
|
|
|
Broadcast::MSG_TYPE_VIDEO, |
67
|
|
|
Broadcast::MSG_TYPE_VOICE, |
68
|
|
|
Broadcast::MSG_TYPE_CARD, |
69
|
|
|
Broadcast::MSG_TYPE_FILE, |
70
|
|
|
Broadcast::MSG_TYPE_MPNEWS, |
71
|
|
|
Broadcast::MSG_TYPE_TEXTCARD, |
72
|
|
|
]; |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* MessageBuilder constructor. |
76
|
|
|
* |
77
|
|
|
* @param \EntWeChat\Broadcast\Broadcast $broadcast |
78
|
|
|
*/ |
79
|
|
|
public function __construct(Broadcast $broadcast) |
80
|
|
|
{ |
81
|
|
|
$this->broadcast = $broadcast; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Set message. |
86
|
|
|
* |
87
|
|
|
* @param string|array $message |
88
|
|
|
* |
89
|
|
|
* @return $this |
90
|
|
|
*/ |
91
|
|
View Code Duplication |
public function message($message) |
|
|
|
|
92
|
|
|
{ |
93
|
|
|
if (is_string($message)) { |
94
|
|
|
$message = new Text(['content' => $message]); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
$this->message = $message; |
98
|
|
|
|
99
|
|
|
return $this; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Set agent to send message. |
104
|
|
|
* |
105
|
|
|
* @param $agentId |
106
|
|
|
* |
107
|
|
|
* @return $this |
108
|
|
|
*/ |
109
|
|
|
public function by($agentId) |
110
|
|
|
{ |
111
|
|
|
$this->agentId = $agentId; |
112
|
|
|
|
113
|
|
|
return $this; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Set target user or group. |
118
|
|
|
* |
119
|
|
|
* @param array $to |
120
|
|
|
* |
121
|
|
|
* @return $this |
122
|
|
|
*/ |
123
|
|
|
public function to(array $to) |
124
|
|
|
{ |
125
|
|
|
$this->to = Arr::only($to, ['touser', 'toparty', 'totag']); |
126
|
|
|
|
127
|
|
|
return $this; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Message target all. |
132
|
|
|
* |
133
|
|
|
* @return $this |
134
|
|
|
*/ |
135
|
|
|
public function toAll() |
136
|
|
|
{ |
137
|
|
|
$this->to['touser'] = '@all'; |
138
|
|
|
|
139
|
|
|
return $this; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* Message target user. |
144
|
|
|
* |
145
|
|
|
* @return $this |
146
|
|
|
*/ |
147
|
|
View Code Duplication |
public function toUser() |
|
|
|
|
148
|
|
|
{ |
149
|
|
|
if (func_num_args() > 0) { |
150
|
|
|
$userIds = is_array(func_get_arg(0)) ? func_get_arg(0) : func_get_args(); |
151
|
|
|
|
152
|
|
|
$this->to['touser'] = implode('|', $userIds); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
return $this; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* Message target party. |
160
|
|
|
* |
161
|
|
|
* @return $this |
162
|
|
|
*/ |
163
|
|
View Code Duplication |
public function toParty() |
|
|
|
|
164
|
|
|
{ |
165
|
|
|
if (func_num_args() > 0) { |
166
|
|
|
$partyIds = is_array(func_get_arg(0)) ? func_get_arg(0) : func_get_args(); |
167
|
|
|
|
168
|
|
|
$this->to['toparty'] = implode('|', $partyIds); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
return $this; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* Message target tag. |
176
|
|
|
* |
177
|
|
|
* @return $this |
178
|
|
|
*/ |
179
|
|
View Code Duplication |
public function toTag() |
|
|
|
|
180
|
|
|
{ |
181
|
|
|
if (func_num_args() > 0) { |
182
|
|
|
$tagIds = is_array(func_get_arg(0)) ? func_get_arg(0) : func_get_args(); |
183
|
|
|
|
184
|
|
|
$this->to['totag'] = implode('|', $tagIds); |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
return $this; |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* Use safe message. |
192
|
|
|
* |
193
|
|
|
* @return $this |
194
|
|
|
*/ |
195
|
|
|
public function safe() |
196
|
|
|
{ |
197
|
|
|
$this->safe = 1; |
198
|
|
|
|
199
|
|
|
return $this; |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* Send the message. |
204
|
|
|
* |
205
|
|
|
* @throws RuntimeException |
206
|
|
|
* |
207
|
|
|
* @return bool |
208
|
|
|
*/ |
209
|
|
View Code Duplication |
public function send() |
|
|
|
|
210
|
|
|
{ |
211
|
|
|
if (empty($this->message)) { |
212
|
|
|
throw new RuntimeException('No message to send.'); |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
$transformer = new Transformer(); |
216
|
|
|
|
217
|
|
|
if ($this->message instanceof RawMessage) { |
218
|
|
|
$message = $this->message->get('content'); |
219
|
|
|
} else { |
220
|
|
|
$content = $transformer->transform($this->message); |
221
|
|
|
|
222
|
|
|
$message = array_merge($this->to, ['agentid' => $this->agentId], ['safe' => $this->safe], $content); |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
return $this->broadcast->send($message); |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
/** |
229
|
|
|
* Return property. |
230
|
|
|
* |
231
|
|
|
* @param string $property |
232
|
|
|
* |
233
|
|
|
* @return mixed |
234
|
|
|
*/ |
235
|
|
|
public function __get($property) |
236
|
|
|
{ |
237
|
|
|
if (property_exists($this, $property)) { |
238
|
|
|
return $this->$property; |
239
|
|
|
} |
240
|
|
|
} |
241
|
|
|
} |
242
|
|
|
|
This check marks private properties in classes that are never used. Those properties can be removed.