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\Work\ExternalContact; |
13
|
|
|
|
14
|
|
|
use EasyWeChat\Kernel\BaseClient; |
15
|
|
|
use EasyWeChat\Kernel\Exceptions\InvalidArgumentException; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Class MessageClient. |
19
|
|
|
* |
20
|
|
|
* @author milkmeowo <[email protected]> |
21
|
|
|
*/ |
22
|
|
|
class MessageClient extends BaseClient |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* Required attributes. |
26
|
|
|
* |
27
|
|
|
* @var array |
28
|
|
|
*/ |
29
|
|
|
protected $required = ['content', 'media_id', 'title', 'url', 'pic_media_id', 'appid', 'page']; |
30
|
|
|
|
31
|
|
|
protected $textMessage = [ |
32
|
|
|
'content' => '', |
33
|
|
|
]; |
34
|
|
|
|
35
|
|
|
protected $imageMessage = [ |
36
|
|
|
'media_id' => '', |
37
|
|
|
]; |
38
|
|
|
|
39
|
|
|
protected $linkMessage = [ |
40
|
|
|
'title' => '', |
41
|
|
|
'picurl' => '', |
42
|
|
|
'desc' => '', |
43
|
|
|
'url' => '', |
44
|
|
|
]; |
45
|
|
|
|
46
|
|
|
protected $miniprogramMessage = [ |
47
|
|
|
'title' => '', |
48
|
|
|
'pic_media_id' => '', |
49
|
|
|
'appid' => '', |
50
|
|
|
'page' => '', |
51
|
|
|
]; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* 添加企业群发消息模板 |
55
|
|
|
* |
56
|
|
|
* @see https://work.weixin.qq.com/api/doc#90000/90135/91560 |
57
|
|
|
* |
58
|
|
|
* @param array $msg |
59
|
|
|
* |
60
|
|
|
* @return array|\EasyWeChat\Kernel\Support\Collection|object|\Psr\Http\Message\ResponseInterface|string |
61
|
|
|
* |
62
|
|
|
* @throws InvalidArgumentException |
63
|
|
|
* @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException |
64
|
|
|
*/ |
65
|
5 |
|
public function addTemplateMessage(array $msg) |
66
|
|
|
{ |
67
|
5 |
|
$params = $this->formatMessage($msg); |
68
|
|
|
|
69
|
2 |
|
return $this->httpPostJson('cgi-bin/externalcontact/add_msg_template', $params); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* 获取企业群发消息发送结果. |
74
|
|
|
* |
75
|
|
|
* @see https://work.weixin.qq.com/api/doc#90000/90135/91561 |
76
|
|
|
* |
77
|
|
|
* @param $msgId |
78
|
|
|
* |
79
|
|
|
* @return array|\EasyWeChat\Kernel\Support\Collection|object|\Psr\Http\Message\ResponseInterface|string |
80
|
|
|
* |
81
|
|
|
* @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException |
82
|
|
|
*/ |
83
|
1 |
|
public function getGroupMsgResult($msgId) |
84
|
|
|
{ |
85
|
1 |
|
return $this->httpPostJson('cgi-bin/externalcontact/get_group_msg_result', [ |
86
|
1 |
|
'msgid' => $msgId, |
87
|
|
|
]); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* 发送新客户欢迎语. |
92
|
|
|
* |
93
|
|
|
* @see https://work.weixin.qq.com/api/doc#90000/90135/91688 |
94
|
|
|
* |
95
|
|
|
* @param string $welcomeCode |
96
|
|
|
* @param array $msg |
97
|
|
|
* |
98
|
|
|
* @return array|\EasyWeChat\Kernel\Support\Collection|object|\Psr\Http\Message\ResponseInterface|string |
99
|
|
|
* |
100
|
|
|
* @throws InvalidArgumentException |
101
|
|
|
* @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException |
102
|
|
|
*/ |
103
|
1 |
|
public function sendWelcomeMsg(string $welcomeCode, array $msg) |
104
|
|
|
{ |
105
|
1 |
|
$formattedMsg = $this->formatMessage($msg); |
106
|
|
|
|
107
|
1 |
|
$params = array_merge($formattedMsg, [ |
108
|
1 |
|
'welcome_code' => $welcomeCode, |
109
|
|
|
]); |
110
|
|
|
|
111
|
1 |
|
return $this->httpPostJson('cgi-bin/externalcontact/send_welcome_msg', $params); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @param array $data |
116
|
|
|
* |
117
|
|
|
* @return array |
118
|
|
|
* |
119
|
|
|
* @throws InvalidArgumentException |
120
|
|
|
*/ |
121
|
6 |
|
protected function formatMessage(array $data = []) |
122
|
|
|
{ |
123
|
6 |
|
$params = $data; |
124
|
|
|
|
125
|
6 |
|
if (!empty($params['text'])) { |
126
|
3 |
|
$params['text'] = $this->baseFormat($params['text'], $this->textMessage); |
127
|
|
|
} |
128
|
|
|
|
129
|
5 |
|
if (!empty($params['image'])) { |
130
|
2 |
|
$params['image'] = $this->baseFormat($params['image'], $this->imageMessage); |
131
|
|
|
} |
132
|
|
|
|
133
|
4 |
|
if (!empty($params['link'])) { |
134
|
2 |
|
$params['link'] = $this->baseFormat($params['link'], $this->linkMessage); |
135
|
|
|
} |
136
|
|
|
|
137
|
3 |
|
if (!empty($params['miniprogram'])) { |
138
|
2 |
|
$params['miniprogram'] = $this->baseFormat($params['miniprogram'], $this->miniprogramMessage); |
139
|
|
|
} |
140
|
|
|
|
141
|
3 |
|
return $params; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* @param array $data |
146
|
|
|
* @param array $default |
147
|
|
|
* |
148
|
|
|
* @return array |
149
|
|
|
* |
150
|
|
|
* @throws InvalidArgumentException |
151
|
|
|
*/ |
152
|
6 |
|
protected function baseFormat($data = [], $default = []) |
153
|
|
|
{ |
154
|
6 |
|
$params = array_merge($default, $data); |
155
|
6 |
|
foreach ($params as $key => $value) { |
156
|
6 |
|
if (in_array($key, $this->required, true) && empty($value) && empty($default[$key])) { |
157
|
4 |
|
throw new InvalidArgumentException(sprintf('Attribute "%s" can not be empty!', $key)); |
158
|
|
|
} |
159
|
|
|
|
160
|
4 |
|
$params[$key] = empty($value) ? $default[$key] : $value; |
161
|
|
|
} |
162
|
|
|
|
163
|
3 |
|
return $params; |
164
|
|
|
} |
165
|
|
|
} |
166
|
|
|
|