work/appchat.pm untested

filter: Statementstmt 0.0% 0 / 61 Branchbran 0.0% 0 / 16 Conditioncond 0.0% 0 / 24 MC/DCmcdc 0.0% 0 / 12 Subroutinesub 0.0% 0 / 10 Podpod 0.0% 0 / 4 totaltotal 0.0% 0 / 127 CC 9 SCAR 21.6
CC
9
Cov
0%
create
12.0
update
12.0
get
12.0
CRAP 8.7
1 package QQ::weixin::work::appchat;
2
3 =encoding utf8
4
5 =head1 Name
6
7 QQ::weixin::work::appchat
8
9 =head1 DESCRIPTION
10
11 发送消息到群聊会话
12
13 =cut
14
15 use strict;
16 use base qw(QQ::weixin::work);
17 use Encode;
18 use LWP::UserAgent;
19 use JSON;
20 use utf8;
21
22 our $VERSION = '0.10';
23 our @EXPORT = qw/ create update get send /;
24
25 =head1 FUNCTION
26
27 =head2 create(access_token, hash);
28
29 创建群聊会话
30 最后更新:2022/08/18
31
32 =head2 SYNOPSIS
33
34 L<https://developer.work.weixin.qq.com/document/path/90245>
35
36 =head3 请求说明:
37
38 =head4 请求包结构体为:
39
40 {
41 "name" : "NAME",
42 "owner" : "userid1",
43 "userlist" : ["userid1", "userid2", "userid3"],
44 "chatid" : "CHATID"
45 }
46
47 =head4 参数说明:
48
49 参数 必须 说明
50 access_token 是 调用接口凭证
51 name 否 群聊名,最多50个utf8字符,超过将截断
52 owner 否 指定群主的id。如果不指定,系统会随机从userlist中选一人作为群主
53 userlist 是 群成员id列表。至少2人,至多2000人
54 chatid 否 群聊的唯一标志,不能与已有的群重复;字符串类型,最长32个字符。只允许字符0-9及字母a-zA-Z。如果不填,系统会随机生成群id
55
56 =head3 权限说明
57
58 只允许企业自建应用调用,且应用的可见范围必须是根部门;
59
60 =head4 限制说明:
61
62 群成员人数不可超过管理端配置的“群成员人数上限”,且最大不可超过2000人;
63 每企业创建群数不可超过1000/天;
64
65 =head3 RETURN 返回结果
66
67 {
68 "errcode" : 0,
69 "errmsg" : "ok",
70 "chatid" : "CHATID"
71 }
72
73 =head4 RETURN 参数说明
74
75 参数 说明
76 errcode 返回码
77 errmsg 对返回码的文本描述内容
78 chatid 群聊的唯一标志
79
80 注意:刚创建的群,如果没有下发消息,在企业微信不会出现该群。
81
82 =cut
83
84 sub create {
85 if ( @_ && $_[0] && ref $_[1] eq 'HASH' ) {
86 my $access_token = $_[0];
87 my $json = $_[1];
88 my $ua = LWP::UserAgent->new;
89 $ua->timeout(30);
90 $ua->env_proxy;
91
92 my $response = $ua->post("https://qyapi.weixin.qq.com/cgi-bin/appchat/create?access_token=$access_token",Content => to_json($json,{allow_nonref=>1}),Content_type =>'application/json');
93 if ($response->is_success) {
94 return from_json($response->decoded_content,{utf8 => 1, allow_nonref => 1});
95 }
96
97 }
98 return 0;
99 }
100
101 =head2 update(access_token, hash);
102
103 修改群聊会话
104 最后更新:2023/04/13
105
106 =head2 SYNOPSIS
107
108 L<https://developer.work.weixin.qq.com/document/path/98913>
109
110 =head3 请求说明:
111
112 =head4 请求包结构体为:
113
114 {
115 "chatid" : "CHATID",
116 "name" : "NAME",
117 "owner" : "userid2",
118 "add_user_list" : ["userid1", "userid2", "userid3"],
119 "del_user_list" : ["userid3", "userid4"]
120 }
121
122 =head4 参数说明:
123
124 参数 必须 说明
125 access_token 是 调用接口凭证
126 chatid 是 群聊id
127 name 否 新的群聊名。若不需更新,请忽略此参数。最多50个utf8字符,超过将截断
128 owner 否 新群主的id。若不需更新,请忽略此参数
129 add_user_list 否 添加成员的id列表
130 del_user_list 否 踢出成员的id列表
131
132 =head3 权限说明
133
134 只允许企业自建应用调用,且应用的可见范围必须是根部门。
135
136 =head4 限制说明:
137
138 chatid所代表的群必须是该应用所创建。
139 群成员人数不可超过2000人。
140 每企业变更群的次数不可超过1000次/小时。
141
142 =head3 RETURN 返回结果
143
144 {
145 "errcode": 0,
146 "errmsg": "ok"
147 }
148
149 =head4 RETURN 参数说明
150
151 参数 说明
152 errcode 返回码
153 errmsg 对返回码的文本描述内容
154
155 =cut
156
157 sub update {
158 if ( @_ && $_[0] && ref $_[1] eq 'HASH' ) {
159 my $access_token = $_[0];
160 my $json = $_[1];
161 my $ua = LWP::UserAgent->new;
162 $ua->timeout(30);
163 $ua->env_proxy;
164
165 my $response = $ua->post("https://qyapi.weixin.qq.com/cgi-bin/appchat/update?access_token=$access_token",Content => to_json($json,{allow_nonref=>1}),Content_type =>'application/json');
166 if ($response->is_success) {
167 return from_json($response->decoded_content,{utf8 => 1, allow_nonref => 1});
168 }
169
170 }
171 return 0;
172 }
173
174 =head2 get(access_token, chatid);
175
176 获取群聊会话
177 最后更新:2023/04/13
178
179 =head2 SYNOPSIS
180
181 L<https://developer.work.weixin.qq.com/document/path/90247>
182
183 =head3 请求说明:
184
185 =head4 参数说明:
186
187 参数 必须 说明
188 access_token 是 调用接口凭证
189 chatid 是 群聊id
190
191 =head4 权限说明:
192
193 只允许企业自建应用调用,且应用的可见范围必须是根部门;
194 chatid所代表的群必须是该应用所创建;
195 第三方不可调用。
196
197 =head3 RETURN 返回结果:
198
199 {
200 "errcode" : 0,
201 "errmsg" : "ok",
202 "chat_info" : {
203 "chatid" : "CHATID",
204 "name" : "NAME",
205 "owner" : "userid2",
206 "userlist" : ["userid1", "userid2", "userid3"]
207 }
208 }
209
210 =head4 RETURN 参数说明
211
212 参数 说明
213 errcode 返回码
214 errmsg 对返回码的文本描述内容
215 chat_info 群聊信息
216 chatid 群聊唯一标志
217 name 群聊名
218 owner 群主id
219 userlist 群成员id列表
220
221 =cut
222
223 sub get {
224 if ( @_ && $_[0] && $_[1] ) {
225 my $access_token = $_[0];
226 my $chatid = $_[1];
227 my $ua = LWP::UserAgent->new;
228 $ua->timeout(30);
229 $ua->env_proxy;
230
231 my $response = $ua->get("https://qyapi.weixin.qq.com/cgi-bin/appchat/get?access_token=$access_token&chatid=$chatid");
232 if ($response->is_success) {
233 return from_json($response->decoded_content,{utf8 => 1, allow_nonref => 1});
234 }
235
236 }
237 return 0;
238 }
239
240 =head2 send(access_token, hash);
241
242 应用推送消息
243 最后更新:2024/01/10
244
245 =head2 SYNOPSIS
246
247 L<https://developer.work.weixin.qq.com/document/path/90248>
248
249 =head3 请求说明:
250
251 应用支持推送文本、图片、视频、文件、图文等类型。
252
253 =head4 请求包结构体为:
254
255 各个消息类型的具体POST格式参考后面消息类型说明
256
257 =head4 参数说明:
258
259 参数 必须 说明
260 access_token 是 调用接口凭证
261
262 =head3 权限说明
263
264 只允许企业自建应用调用,且应用的可见范围必须是根部门。
265
266 =head4 限制说明:
267
268 chatid所代表的群必须是该应用所创建。
269 每企业消息发送量不可超过2万人次/分,不可超过30万人次/小时(若群有100人,每发一次消息算100人次)。
270 出于对成员的保护,每个成员在群中收到的同一个应用的消息不可超过200条/分,1万条/天,超过会被丢弃,而接口不会报错。(若应用创建了两个群,成员张三同时在这两个群中,应用往第一个群发送1条消息,再往第二个群发送2条消息,则张三累计收到该应用3条消息)。
271
272 =head3 RETURN 返回结果
273
274 {
275 "errcode": 0,
276 "errmsg": "ok"
277 }
278
279 =head4 RETURN 参数说明
280
281 参数 说明
282 errcode 返回码
283 errmsg 对返回码的文本描述内容
284
285 =cut
286
287 sub send {
288 if ( @_ && $_[0] && ref $_[1] eq 'HASH' ) {
289 my $access_token = $_[0];
290 my $json = $_[1];
291 my $ua = LWP::UserAgent->new;
292 $ua->timeout(30);
293 $ua->env_proxy;
294
295 my $response = $ua->post("https://qyapi.weixin.qq.com/cgi-bin/appchat/send?access_token=$access_token",Content => to_json($json,{allow_nonref=>1}),Content_type =>'application/json');
296 if ($response->is_success) {
297 return from_json($response->decoded_content,{utf8 => 1, allow_nonref => 1});
298 }
299
300 }
301 return 0;
302 }
303
304 1;
305 __END__