-
Notifications
You must be signed in to change notification settings - Fork 23
/
SendMessage.php
412 lines (383 loc) · 15.8 KB
/
SendMessage.php
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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
<?php
require_once (dirname(__FILE__) . '/Tools.php');
require_once (dirname(__FILE__) . '/Material.php');
/**
* 微信企业号
* 发送消息
* @author faith [email protected]
* @createtime 2015-08-02 00:57:49
*/
class SendMessage {
private $token;
private $tools;
private $url;
public function __construct($token) {
$this -> token = $token;
$this -> tools = new Tools();
$this -> url = "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token={$this->token}";
}
/**
* 设置token
*/
public function setToken($token = NULL) {
$this -> token = $token;
$this -> url = "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token={$this->token}";
}
/**
* 获取token
*/
public function getToken() {
return $this -> token;
}
/**
* 发送文字消息
* @param $agent_id 企业应用的id,整型。可在应用的设置页面查看
* @param $content 消息内容
* @param $to_user 成员ID列表,一维数组传递['1', '2', ...],最多1000个,默认发送给全部成员
* @param $to_party 部门ID列表,一维数组传递['1', '2', ...],最多1000个
* @param $to_tag 标签ID列表,一维数组传递['1', '2', ...],最多1000个
* @param $safe 表示是否是保密消息,0表示否,1表示是,默认0
*/
public function sendText($agent_id, $content, $to_user = "@all", $to_party = array(), $to_tag = array(), $safe = 0) {
if (intval($agent_id) < 0 || empty($content)) {
return json_encode(array('success' => FALSE, 'errmsg' => 'Agent_id or content is empty!', 'errcode' => -2));
}
$data = array('agentid' => $agent_id, 'msgtype' => "text", 'text' => array('content' => $content));
$data['touser'] = $this->getToUserList($to_user);
if (($tmp = $this->getToList($to_party)) != FALSE) {
$data['toparty'] = $tmp;
}
if (($tmp = $this->getToList($to_tag)) != FALSE) {
$data['totag'] = $tmp;
}
if (intval($safe) == 1) {
$data['safe'] = 1;
}
$result = $this -> tools -> httpRequest($this -> url, $data, 'post');
if ($result) {
if ($result['errcode'] == 0) {
$result['success'] = TRUE;
return json_encode($result);
} else {
$result['success'] = FALSE;
return json_encode($result);
}
} else {
return json_encode(array('success' => FALSE, 'errmsg' => 'Send text fails!', 'errcode' => -2));
}
}
/**
* 发送图片
* @param $agent_id 企业应用的id,整型。可在应用的设置页面查看
* @param $media_id 媒体文件id,可以调用上传临时素材或者永久素材接口获取
* @param $to_user 成员ID列表,一维数组传递['1', '2', ...],最多1000个,默认发送给全部成员
* @param $to_party 部门ID列表,一维数组传递['1', '2', ...],最多1000个
* @param $to_tag 标签ID列表,一维数组传递['1', '2', ...],最多1000个
* @param $safe 表示是否是保密消息,0表示否,1表示是,默认0
*/
public function sendImage($agent_id, $media_id, $to_user = "@all", $to_party = array(), $to_tag = array(), $safe = 0) {
if (intval($agent_id) < 0 || empty($media_id)) {
return json_encode(array('success' => FALSE, 'errmsg' => 'Agent_id or media_id is empty!', 'errcode' => -2));
}
$result = $this -> sendImageVoiceFileMpnews($agent_id, $media_id, Material::MEDIA_TYPE_IMAGE, $to_user, $to_party, $to_tag, $safe);
if ($result) {
if ($result['errcode'] == 0) {
$result['success'] = TRUE;
return json_encode($result);
} else {
$result['success'] = FALSE;
return json_encode($result);
}
} else {
return json_encode(array('success' => FALSE, 'errmsg' => 'Send image fails!', 'errcode' => -2));
}
}
/**
* 发送语音
* @param $agent_id 企业应用的id,整型。可在应用的设置页面查看
* @param $media_id 媒体文件id,可以调用上传临时素材或者永久素材接口获取
* @param $to_user 成员ID列表,一维数组传递['1', '2', ...],最多1000个,默认发送给全部成员
* @param $to_party 部门ID列表,一维数组传递['1', '2', ...],最多1000个
* @param $to_tag 标签ID列表,一维数组传递['1', '2', ...],最多1000个
* @param $safe 表示是否是保密消息,0表示否,1表示是,默认0
*/
public function sendVoice($agent_id, $media_id, $to_user = "@all", $to_party = array(), $to_tag = array(), $safe = 0) {
if (intval($agent_id) < 0 || empty($media_id)) {
return json_encode(array('success' => FALSE, 'errmsg' => 'Agent_id or media_id is empty!', 'errcode' => -2));
}
$result = $this -> sendImageVoiceFileMpnews($agent_id, $media_id, Material::MEDIA_TYPE_VOICE, $to_user, $to_party, $to_tag, $safe);
if ($result) {
if ($result['errcode'] == 0) {
$result['success'] = TRUE;
return json_encode($result);
} else {
$result['success'] = FALSE;
return json_encode($result);
}
} else {
return json_encode(array('success' => FALSE, 'errmsg' => 'Send voice fails!', 'errcode' => -2));
}
}
/**
* 发送视频
* @param $agent_id 企业应用的id,整型。可在应用的设置页面查看
* @param $media_id 媒体文件id,可以调用上传临时素材或者永久素材接口获取
* @param $title 视频消息的标题
* @param $description 视频消息的描述
* @param $to_user 成员ID列表,一维数组传递['1', '2', ...],最多1000个,默认发送给全部成员
* @param $to_party 部门ID列表,一维数组传递['1', '2', ...],最多1000个
* @param $to_tag 标签ID列表,一维数组传递['1', '2', ...],最多1000个
* @param $safe 表示是否是保密消息,0表示否,1表示是,默认0
*/
public function sendVideo($agent_id, $media_id, $title = FALSE, $description = FALSE, $to_user = "@all", $to_party = array(), $to_tag = array(), $safe = 0) {
if (intval($agent_id) < 0 || empty($media_id)) {
return json_encode(array('success' => FALSE, 'errmsg' => 'Agent_id or media_id is empty!', 'errcode' => -2));
}
$data = array('agentid' => $agent_id, 'msgtype' => "video", 'video' => array('media_id' => $media_id));
if (!empty($title)) {
$data['video']['title'] = $title;
}
if (!empty($description)) {
$data['video']['description'] = $description;
}
$data['touser'] = $this->getToUserList($to_user);
if (($tmp = $this->getToList($to_party)) != FALSE) {
$data['toparty'] = $tmp;
}
if (($tmp = $this->getToList($to_tag)) != FALSE) {
$data['totag'] = $tmp;
}
if (intval($safe) == 1) {
$data['safe'] = 1;
}
$result = $this -> tools -> httpRequest($this -> url, $data, 'post');
if ($result) {
if ($result['errcode'] == 0) {
$result['success'] = TRUE;
return json_encode($result);
} else {
$result['success'] = FALSE;
return json_encode($result);
}
} else {
return json_encode(array('success' => FALSE, 'errmsg' => 'Send text fails!', 'errcode' => -2));
}
}
/**
* 发送文件
* @param $agent_id 企业应用的id,整型。可在应用的设置页面查看
* @param $media_id 媒体文件id,可以调用上传临时素材或者永久素材接口获取
* @param $to_user 成员ID列表,一维数组传递['1', '2', ...],最多1000个,默认发送给全部成员
* @param $to_party 部门ID列表,一维数组传递['1', '2', ...],最多1000个
* @param $to_tag 标签ID列表,一维数组传递['1', '2', ...],最多1000个
* @param $safe 表示是否是保密消息,0表示否,1表示是,默认0
*/
public function sendFile($agent_id, $media_id, $to_user = "@all", $to_party = array(), $to_tag = array(), $safe = 0) {
if (intval($agent_id) < 0 || empty($media_id)) {
return json_encode(array('success' => FALSE, 'errmsg' => 'Agent_id or media_id is empty!', 'errcode' => -2));
}
$result = $this -> sendImageVoiceFileMpnews($agent_id, $media_id, Material::MEDIA_TYPE_FILE, $to_user, $to_party, $to_tag, $safe);
if ($result) {
if ($result['errcode'] == 0) {
$result['success'] = TRUE;
return json_encode($result);
} else {
$result['success'] = FALSE;
return json_encode($result);
}
} else {
return json_encode(array('success' => FALSE, 'errmsg' => 'Send file fails!', 'errcode' => -2));
}
}
/**
* 发送news信息
* @param $agent_id 企业应用的id,整型。可在应用的设置页面查看
* @param $articles 图文消息,一个图文消息支持1到10条图文,格式参考微信官方文档:http://qydev.weixin.qq.com/wiki/index.php?title=%E6%B6%88%E6%81%AF%E7%B1%BB%E5%9E%8B%E5%8F%8A%E6%95%B0%E6%8D%AE%E6%A0%BC%E5%BC%8F
* @param $to_user 成员ID列表,一维数组传递['1', '2', ...],最多1000个,默认发送给全部成员
* @param $to_party 部门ID列表,一维数组传递['1', '2', ...],最多1000个
* @param $to_tag 标签ID列表,一维数组传递['1', '2', ...],最多1000个
*/
public function sendNews($agent_id, $articles, $to_user = "@all", $to_party = array(), $to_tag = array()) {
if (intval($agent_id) < 0) {
return json_encode(array('success' => FALSE, 'errmsg' => 'Agent_id must be greater than zero!', 'errcode' => -2));
}
if (empty($articles) || !is_array($articles)) {
return json_encode(array('success' => FALSE, 'errmsg' => 'Articles is invalid!', 'errcode' => -2));
}
$data = array('agentid' => $agent_id, 'msgtype' => "news", 'news' => array('articles' => $articles));
$data['touser'] = $this->getToUserList($to_user);
if (($tmp = $this->getToList($to_party)) != FALSE) {
$data['toparty'] = $tmp;
}
if (($tmp = $this->getToList($to_tag)) != FALSE) {
$data['totag'] = $tmp;
}
$result = $this -> tools -> httpRequest($this -> url, $data, 'post');
if ($result) {
if ($result['errcode'] == 0) {
$result['success'] = TRUE;
return json_encode($result);
} else {
$result['success'] = FALSE;
return json_encode($result);
}
} else {
return json_encode(array('success' => FALSE, 'errmsg' => 'Send text fails!', 'errcode' => -2));
}
}
/**
* 通过media_id发送图文消息
* @param $agent_id 企业应用的id,整型。可在应用的设置页面查看
* @param $media_id 媒体文件id,可以调用上传临时素材或者永久素材接口获取
* @param $to_user 成员ID列表,一维数组传递['1', '2', ...],最多1000个,默认发送给全部成员
* @param $to_party 部门ID列表,一维数组传递['1', '2', ...],最多1000个
* @param $to_tag 标签ID列表,一维数组传递['1', '2', ...],最多1000个
* @param $safe 表示是否是保密消息,0表示否,1表示是,默认0
*/
public function sendMpnewsByMediaID($agent_id, $media_id, $to_user = "@all", $to_party = array(), $to_tag = array(), $safe = 0) {
if (intval($agent_id) < 0 || empty($media_id)) {
return json_encode(array('success' => FALSE, 'errmsg' => 'Agent_id or media_id is empty!', 'errcode' => -2));
}
$result = $this -> sendImageVoiceFileMpnews($agent_id, $media_id, Material::MEDIA_TYPE_MPNEWS, $to_user, $to_party, $to_tag, $safe);
if ($result) {
if ($result['errcode'] == 0) {
$result['success'] = TRUE;
return json_encode($result);
} else {
$result['success'] = FALSE;
return json_encode($result);
}
} else {
return json_encode(array('success' => FALSE, 'errmsg' => 'Send mpnews fails!', 'errcode' => -2));
}
}
/**
* 通过图文消息的内容发送图文消息
* @param $agent_id 企业应用的id,整型。可在应用的设置页面查看
* @param $articles 图文消息,一个图文消息支持1到10个图文,格式参考微信官方文档:http://qydev.weixin.qq.com/wiki/index.php?title=%E6%B6%88%E6%81%AF%E7%B1%BB%E5%9E%8B%E5%8F%8A%E6%95%B0%E6%8D%AE%E6%A0%BC%E5%BC%8F
* @param $to_user 成员ID列表,一维数组传递['1', '2', ...],最多1000个,默认发送给全部成员
* @param $to_party 部门ID列表,一维数组传递['1', '2', ...],最多1000个
* @param $to_tag 标签ID列表,一维数组传递['1', '2', ...],最多1000个
* @param $safe 表示是否是保密消息,0表示否,1表示是,默认0
*/
public function sendMpnewsByContent($agent_id, $articles, $to_user = "@all", $to_party = array(), $to_tag = array(), $safe = 0) {
if (intval($agent_id) < 0) {
return json_encode(array('success' => FALSE, 'errmsg' => 'Agent_id must be greater than zero!', 'errcode' => -2));
}
if (empty($articles) || !is_array($articles)) {
return json_encode(array('success' => FALSE, 'errmsg' => 'Articles is invalid!', 'errcode' => -2));
}
$data = array('agentid' => $agent_id, 'msgtype' => "mpnews", 'mpnews' => array('articles' => $articles));
$data['touser'] = $this->getToUserList($to_user);
if (($tmp = $this->getToList($to_party)) != FALSE) {
$data['toparty'] = $tmp;
}
if (($tmp = $this->getToList($to_tag)) != FALSE) {
$data['totag'] = $tmp;
}
if (intval($safe) == 1) {
$data['safe'] = 1;
}
$result = $this -> tools -> httpRequest($this -> url, $data, 'post');
if ($result) {
if ($result['errcode'] == 0) {
$result['success'] = TRUE;
return json_encode($result);
} else {
$result['success'] = FALSE;
return json_encode($result);
}
} else {
return json_encode(array('success' => FALSE, 'errmsg' => 'Send text fails!', 'errcode' => -2));
}
}
/**
* 发送图片、文件、音频、图文消息
* @param $agent_id 企业应用的id,整型。可在应用的设置页面查看
* @param $media_id 媒体文件id,可以调用上传临时素材或者永久素材接口获取
* @param $msg_type 消息类型,取值范围为Material类中的类常量
* @param $to_user 成员ID列表,一维数组传递['1', '2', ...],最多1000个,默认发送给全部成员
* @param $to_party 部门ID列表,一维数组传递['1', '2', ...],最多1000个
* @param $to_tag 标签ID列表,一维数组传递['1', '2', ...],最多1000个
* @param $safe 表示是否是保密消息,0表示否,1表示是,默认0
*/
private function sendImageVoiceFileMpnews($agent_id, $media_id, $msg_type, $to_user = "@all", $to_party = array(), $to_tag = array(), $safe = 0) {
if (intval($agent_id) < 0 || empty($media_id) || empty($msg_type)) {
return FALSE;
}
$data = array('agentid' => $agent_id);
switch ($msg_type) {
case Material::MEDIA_TYPE_FILE :
$data['file'] = array('media_id' => $media_id);
break;
case Material::MEDIA_TYPE_IMAGE :
$data['image'] = array('media_id' => $media_id);
break;
case Material::MEDIA_TYPE_VOICE :
$data['voice'] = array('media_id' => $media_id);
break;
case Material::MEDIA_TYPE_MPNEWS:
$data['mpnews'] = array('media_id'=> $media_id);
break;
default :
return FALSE;
break;
}
$data['msgtype'] = $msg_type;
$data['touser'] = $this->getToUserList($to_user);
if (($tmp = $this->getToList($to_party)) != FALSE) {
$data['toparty'] = $tmp;
}
if (($tmp = $this->getToList($to_tag)) != FALSE) {
$data['totag'] = $tmp;
}
if (intval($safe) == 1) {
$data['safe'] = 1;
}
$result = $this -> tools -> httpRequest($this -> url, $data, 'post');
return $result;
}
/**
* 根据成员ID列表返回微信需要的成员列表格式字符串
* @param $to_user 成员ID列表,一维数组传递['1', '2', ...],最多1000个,默认发送给全部成员
*/
private function getToUserList($to_user = "@all") {
$data = "";
if (is_array($to_user)) {
$first = TRUE;
foreach ($to_user as $value) {
if ($first) {
$data = $value;
$first = FALSE;
} else {
$data .= "|" . $value;
}
}
} else {
$data = $to_user;
}
return $data;
}
/**
* 根据传入的数组,返回微信需要的列表格式字符串
* @param $to_list 一维数组传递['1', '2', ...],最多1000个
*/
private function getToList($to_list = array()) {
$data = "";
if (!empty($to_list) && is_array($to_list)) {
$first = TRUE;
foreach ($to_list as $value) {
if ($first) {
$data = $value;
$first = FALSE;
} else {
$data .= "|" . $value;
}
}
}else {
$data = FALSE;
}
return $data;
}
}
/* End of file */