-
Notifications
You must be signed in to change notification settings - Fork 279
Expand file tree
/
Copy pathMessage.class.php
More file actions
393 lines (324 loc) · 10.8 KB
/
Message.class.php
File metadata and controls
393 lines (324 loc) · 10.8 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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
<?php
include_once(__DIR__."/../../utils/error.inc.php");
include_once(__DIR__."/../../utils/Utils.class.php");
class Message
{
public $sendToAll = false; // bool, 是否全员发送, 即文档所谓 @all
public $touser = array(); // string array
public $toparty = array(); // uint array
public $totag = array(); // uint array
public $agentid = null; // uint
public $safe = null; // uint, 表示是否是保密消息,0表示否,1表示是,默认0
public $messageContent = null; // xxxMessageContent
public $enable_id_trans = 0; // 表示是否开启id转译,0表示否,1表示是,默认0
public $enable_duplicate_check = 0; // 表示是否开启重复消息检查,0表示否,1表示是,默认0
public $duplicate_check_interval = 1800; //表示是否重复消息检查的时间间隔,默认1800s,最大不超过4小时
public function CheckMessageSendArgs()
{
if (count($this->touser) > 1000) throw new QyApiError("touser should be no more than 1000");
if (count($this->toparty) > 100) throw new QyApiError("toparty should be no more than 100");
if (count($this->totag) > 100) throw new QyApiError("toparty should be no more than 100");
if (is_null($this->messageContent)) throw new QyApiError("messageContent is empty");
$this->messageContent->CheckMessageSendArgs();
}
public function Message2Array()
{
$args = array();
if (true == $this->sendToAll) {
Utils::setIfNotNull("@all", "touser", $args);
} else {
//
$touser_string = null;
foreach($this->touser as $item) {
$touser_string = $touser_string . $item . "|";
}
Utils::setIfNotNull($touser_string, "touser", $args);
//
$toparty_string = null;
foreach($this->toparty as $item) {
$toparty_string = $toparty_string . $item . "|";
}
Utils::setIfNotNull($toparty_string, "toparty", $args);
//
$totag_string = null;
foreach($this->totag as $item) {
$totag_string = $totag_string . $item . "|";
}
Utils::setIfNotNull($totag_string, "totag", $args);
}
Utils::setIfNotNull($this->agentid, "agentid", $args);
Utils::setIfNotNull($this->safe, "safe", $args);
Utils::setIfNotNull($this->enable_id_trans, "enable_id_trans", $args);
Utils::setIfNotNull($this->enable_duplicate_check, "enable_duplicate_check", $args);
Utils::setIfNotNull($this->duplicate_check_interval, "duplicate_check_interval", $args);
$this->messageContent->MessageContent2Array($args);
return $args;
}
}
// --------------------- 各种类型的 MessageContent -----------------------------
//
class TextMessageContent
{
public $msgtype = "text";
private $content = null; // string
public function __construct($content=null)
{
$this->content = $content;
}
public function CheckMessageSendArgs()
{
$len = strlen($this->content);
if ($len == 0 || $len > 2048) {
throw new QyApiError("invalid content length " . $len);
}
}
public function MessageContent2Array(&$arr)
{
Utils::setIfNotNull($this->msgtype, "msgtype", $arr);
$contentArr = array("content" => $this->content);
Utils::setIfNotNull($contentArr, $this->msgtype, $arr);
}
}
class ImageMessageContent
{
public $msgtype = "image";
private $media_id = null; // string
public function __construct($media_id=null)
{
$this->media_id = $media_id;
}
public function CheckMessageSendArgs()
{
Utils::checkNotEmptyStr($this->media_id, "media_id");
}
public function MessageContent2Array(&$arr)
{
Utils::setIfNotNull($this->msgtype, "msgtype", $arr);
$contentArr = array("media_id" => $this->media_id);
Utils::setIfNotNull($contentArr, $this->msgtype, $arr);
}
}
class VoiceMessageContent
{
public $msgtype = "voice";
private $media_id = null; // string
public function __construct($media_id=null)
{
$this->media_id = $media_id;
}
public function CheckMessageSendArgs()
{
Utils::checkNotEmptyStr($this->media_id, "media_id");
}
public function MessageContent2Array(&$arr)
{
Utils::setIfNotNull($this->msgtype, "msgtype", $arr);
$contentArr = array("media_id" => $this->media_id);
Utils::setIfNotNull($contentArr, $this->msgtype, $arr);
}
}
class VideoMessageContent
{
public $msgtype = "video";
public $media_id = null; // string
public $title = null; // string
public $description = null; // string
public function __construct($media_id=null, $title=null, $description=null)
{
$this->media_id = $media_id;
$this->title = $title;
$this->description = $description;
}
public function CheckMessageSendArgs()
{
Utils::checkNotEmptyStr($this->media_id, "media_id");
}
public function MessageContent2Array(&$arr)
{
Utils::setIfNotNull($this->msgtype, "msgtype", $arr);
$contentArr = array();
{
Utils::setIfNotNull($this->media_id, "media_id", $contentArr);
Utils::setIfNotNull($this->title, "title", $contentArr);
Utils::setIfNotNull($this->description, "description", $contentArr);
}
Utils::setIfNotNull($contentArr, $this->msgtype, $arr);
}
}
class FileMessageContent
{
public $msgtype = "file";
public $media_id = null; // string
public function __construct($media_id=null)
{
$this->media_id = $media_id;
}
public function CheckMessageSendArgs()
{
Utils::checkNotEmptyStr($this->media_id, "media_id");
}
public function MessageContent2Array(&$arr)
{
Utils::setIfNotNull($this->msgtype, "msgtype", $arr);
$contentArr = array();
{
Utils::setIfNotNull($this->media_id, "media_id", $contentArr);
}
Utils::setIfNotNull($contentArr, $this->msgtype, $arr);
}
}
class TextCardMessageContent
{
public $msgtype = "textcard";
public $title = null; // string
public $description = null; // string
public $url = null; // string
public $btntxt = null; // string
public function __construct($title=null, $description=null, $url=null, $btntxt=null)
{
$this->title = $title;
$this->description = $description;
$this->url = $url;
$this->btntxt = $btntxt;
}
public function CheckMessageSendArgs()
{
Utils::checkNotEmptyStr($this->title, "title");
Utils::checkNotEmptyStr($this->description, "description");
Utils::checkNotEmptyStr($this->url, "url");
}
public function MessageContent2Array(&$arr)
{
Utils::setIfNotNull($this->msgtype, "msgtype", $arr);
$contentArr = array();
{
Utils::setIfNotNull($this->title, "title", $contentArr);
Utils::setIfNotNull($this->description, "description", $contentArr);
Utils::setIfNotNull($this->url, "url", $contentArr);
Utils::setIfNotNull($this->btntxt, "btntxt", $contentArr);
}
Utils::setIfNotNull($contentArr, $this->msgtype, $arr);
}
}
class NewsArticle {
public $title = null; // string
public $description = null; // string
public $url = null; // string
public $picurl = null; // string
public $btntxt = null; // string
public function __construct($title=null, $description=null, $url=null, $picurl=null, $btntxt=null)
{
$this->title = $title;
$this->description = $description;
$this->url = $url;
$this->picurl = $picurl;
$this->btntxt = $btntxt;
}
public function CheckMessageSendArgs()
{
Utils::checkNotEmptyStr($this->title, "title");
Utils::checkNotEmptyStr($this->url, "url");
}
public function Article2Array()
{
$args = array();
Utils::setIfNotNull($this->title, "title", $args);
Utils::setIfNotNull($this->description, "description", $args);
Utils::setIfNotNull($this->url, "url", $args);
Utils::setIfNotNull($this->picurl, "picurl", $args);
Utils::setIfNotNull($this->btntxt, "btntxt", $args);
return $args;
}
}
class NewsMessageContent
{
public $msgtype = "news";
public $articles = array(); // NewsArticle array
public function __construct($articles)
{
$this->articles = $articles;
}
public function CheckMessageSendArgs()
{
$size = count($this->articles);
if ($size < 1 || $size > 8) throw QyApiError("1~8 articles should be given");
foreach($this->articles as $item) {
$item->CheckMessageSendArgs();
}
}
public function MessageContent2Array(&$arr)
{
Utils::setIfNotNull($this->msgtype, "msgtype", $arr);
$articleList = array();
foreach($this->articles as $item) {
$articleList[] = $item->Article2Array();
}
$arr[$this->msgtype]["articles"] = $articleList;
}
}
class MpNewsArticle {
public $title = null; // string
public $thumb_media_id = null; // string
public $author = null; // string
public $content_source_url = null; // string
public $content = null; // string
public $digest = null; // string
public function __construct(
$title=null,
$thumb_media_id=null,
$author=null,
$content_source_url=null,
$content=null,
$digest=null)
{
$this->title = $title;
$this->thumb_media_id = $thumb_media_id;
$this->author = $author;
$this->content_source_url = $content_source_url;
$this->content = $content;
$this->digest = $digest;
}
public function CheckMessageSendArgs()
{
Utils::checkNotEmptyStr($this->title, "title");
Utils::checkNotEmptyStr($this->thumb_media_id, "thumb_media_id");
Utils::checkNotEmptyStr($this->content, "content");
}
public function Article2Array()
{
$args = array();
Utils::setIfNotNull($this->title, "title", $args);
Utils::setIfNotNull($this->thumb_media_id , "thumb_media_id", $args);
Utils::setIfNotNull($this->author, "author", $args);
Utils::setIfNotNull($this->content_source_url, "content_source_url", $args);
Utils::setIfNotNull($this->content, "content", $args);
Utils::setIfNotNull($this->digest, "digest", $args);
return $args;
}
}
class MpNewsMessageContent
{
public $msgtype = "mpnews";
public $articles = array(); // MpNewsArticle array
public function __construct($articles)
{
$this->articles = $articles;
}
public function CheckMessageSendArgs()
{
$size = count($this->articles);
if ($size < 1 || $size > 8) throw QyApiError("1~8 articles should be given");
foreach($this->articles as $item) {
$item->CheckMessageSendArgs();
}
}
public function MessageContent2Array(&$arr)
{
Utils::setIfNotNull($this->msgtype, "msgtype", $arr);
$articleList = array();
foreach($this->articles as $item) {
$articleList[] = $item->Article2Array();
}
$arr[$this->msgtype]["articles"] = $articleList;
}
}