work/media.pm untested

filter: Statementstmt 0.0% 0 / 91 Branchbran 0.0% 0 / 24 Conditioncond 0.0% 0 / 45 MC/DCmcdc 0.0% 0 / 21 Subroutinesub 0.0% 0 / 12 Podpod 0.0% 0 / 6 totaltotal 0.0% 0 / 199 CC 13 SCAR 22.5
CC
13
Cov
0%
upload
12.0
uploadimg
12.0
get
12.0
CRAP 9.5
1 package QQ::weixin::work::media;
2
3 =encoding utf8
4
5 =head1 Name
6
7 QQ::weixin::work::media
8
9 =head1 DESCRIPTION
10
11 =cut
12
13 use strict;
14 use base qw(QQ::weixin::work);
15 use Encode;
16 use LWP::UserAgent;
17 use JSON;
18 use utf8;
19
20 our $VERSION = '0.10';
21 our @EXPORT = qw/ upload uploadimg get
22 upload_by_url get_upload_by_url_result
23 upload_attachment /;
24
25 =head1 FUNCTION
26
27 =head2 upload(access_token, type, media);
28
29 上传临时素材
30 最后更新:2021/10/26
31
32 =head2 SYNOPSIS
33
34 L<https://developer.work.weixin.qq.com/document/path/90253>
35 L<https://developer.work.weixin.qq.com/document/path/90389>
36
37 =head3 请求说明:
38
39 使用multipart/form-data POST上传文件, 文件标识名为"media"
40
41 =head4 参数说明:
42
43 参数 必须 说明
44 access_token 是 调用接口凭证
45 type 是 媒体文件类型,分别有图片(image)、语音(voice)、视频(video),普通文件(file)
46 media 是 媒体路径和文件名
47
48 =head4 权限说明:
49
50 素材上传得到media_id,该media_id仅三天内有效
51 media_id在同一企业内所有应用之间可以共享。
52
53 POST的请求包中,form-data中媒体文件标识,应包含有 filename、filelength、content-type等信息
54 filename标识文件展示的名称。比如,使用该media_id发消息时,展示的文件名由该字段控制
55
56 =head3 RETURN 返回结果:
57
58 请求示例:
59
60 POST https://qyapi.weixin.qq.com/cgi-bin/media/upload?access_token=accesstoken001&type=file HTTP/1.1
61 Content-Type: multipart/form-data; boundary=-------------------------acebdf13572468
62 Content-Length: 220
63
64 ---------------------------acebdf13572468
65 Content-Disposition: form-data; name="media";filename="wework.txt"; filelength=6
66 Content-Type: application/octet-stream
67
68 mytext
69 ---------------------------acebdf13572468--
70
71
72 返回数据:
73
74 {
75 "errcode": 0,
76 "errmsg": "",
77 "type": "image",
78 "media_id": "1G6nrLmr5EC3MMb_-zK1dDdzmd0p7cNliYu9V5w7o8K0",
79 "created_at": "1380000000"
80 }
81
82
83 =head4 RETURN 参数说明:
84
85 参数 说明
86 type 媒体文件类型,分别有图片(image)、语音(voice)、视频(video),普通文件(file)
87 media_id 媒体文件上传后获取的唯一标识,3天内有效
88 created_at 媒体文件上传时间戳
89
90 上传的媒体文件限制
91 所有文件size必须大于5个字节
92
93 图片(image):10MB,支持JPG,PNG格式
94 语音(voice) :2MB,播放长度不超过60s,仅支持AMR格式
95 视频(video) :10MB,支持MP4格式
96 普通文件(file):20MB
97
98 =cut
99
100 sub upload {
101 if ( @_ && $_[0] && $_[1] && $_[2] ) {
102 my $access_token = $_[0];
103 my $type = $_[1];
104 my $file = $_[2];
105 my $ua = LWP::UserAgent->new;
106 $ua->timeout(30);
107 $ua->env_proxy;
108
109 my $response = $ua->post("https://qyapi.weixin.qq.com/cgi-bin/media/upload?access_token=$access_token&type=$type",[media => [$file]],Content_Type => 'multipart/form-data');
110 if ($response->is_success) {
111 return from_json($response->decoded_content,{utf8 => 1, allow_nonref => 1});
112 }
113
114 }
115 return 0;
116 }
117
118 =head2 uploadimg(access_token, media);
119
120 上传图片
121 最后更新:2022/01/20
122
123 =head2 SYNOPSIS
124
125 L<https://developer.work.weixin.qq.com/document/path/90256>
126
127 =head3 请求说明:
128
129 使用multipart/form-data POST上传文件
130
131 =head4 参数说明:
132
133 参数 必须 说明
134 access_token 是 调用接口凭证
135 media 是 路径和文件名
136
137 =head4 权限说明:
138
139 上传图片得到图片URL,该URL永久有效
140 返回的图片URL,仅能用于图文消息正文中的图片展示,或者给客户发送欢迎语等;若用于非企业微信环境下的页面,图片将被屏蔽。
141 每个企业每月最多可上传3000张图片,每天最多可上传1000张图片
142
143 POST的请求包中,form-data中媒体文件标识,应包含有 filename、content-type等信息
144
145 =head3 RETURN 返回结果:
146
147 =head4 请求示例:
148
149 ---------------------------acebdf13572468
150 Content-Disposition: form-data; name="fieldNameHere"; filename="20180103195745.png"
151 Content-Type: image/png
152 Content-Length: 220
153
154 <@INCLUDE *C:\Users\abelzhu\Pictures\企业微信截图_20180103195745.png*@>
155 ---------------------------acebdf13572468--
156
157
158 =head4 返回数据:
159
160 {
161 "errcode": 0,
162 "errmsg": "",
163 "url" : "http://p.qpic.cn/pic_wework/3474110808/7a7c8471673ff0f178f63447935d35a5c1247a7f31d9c060/0"
164 }
165
166
167 =head4 RETURN 参数说明:
168
169 参数 说明
170 errcode 返回码
171 errmsg 对返回码的文本描述内容
172 url 上传后得到的图片URL。永久有效
173
174 上传的图片大小限制
175 图片文件大小应在 5B ~ 2MB 之间
176
177 =cut
178
179 sub uploadimg {
180 if ( @_ && $_[0] && $_[1] ) {
181 my $access_token = $_[0];
182 my $file = $_[1];
183 my $ua = LWP::UserAgent->new;
184 $ua->timeout(30);
185 $ua->env_proxy;
186
187 my $response = $ua->post("https://qyapi.weixin.qq.com/cgi-bin/media/uploadimg?access_token=$access_token",[media => [$file]],Content_Type => 'multipart/form-data');
188 if ($response->is_success) {
189 return from_json($response->decoded_content,{utf8 => 1, allow_nonref => 1});
190 }
191
192 }
193 return 0;
194 }
195
196 =head2 get(access_token, media_id);
197
198 获取临时素材
199 最后更新:2022/12/26
200
201 =head2 SYNOPSIS
202
203 L<https://developer.work.weixin.qq.com/document/path/90254>
204
205 =head3 请求说明:
206
207 =head4 参数说明:
208
209 参数 必须 说明
210 access_token 是 调用接口凭证
211 media_id 是 媒体文件id,见上传临时素材,以及异步上传临时素材(超过20M需使用Range分块下载,且分块大小不超过20M,否则返回错误码830002)
212
213 =head4 权限说明:
214
215 完全公开,media_id在同一企业内所有应用之间可以共享。
216 media_id有效期只有3天,注意要及时获取,以免过期。
217
218 =head3 RETURN 返回结果:
219
220 正确时返回(和普通的http下载相同,请根据http头做相应的处理):
221
222 HTTP/1.1 200 OK
223 Connection: close
224 Content-Type: image/jpeg
225 Content-disposition: attachment; filename="MEDIA_ID.jpg"
226 Date: Sun, 06 Jan 2013 10:20:18 GMT
227 Cache-Control: no-cache, must-revalidate
228 Content-Length: 339721
229
230 Xxxx
231
232 错误时返回(这里省略了HTTP首部):
233
234 {
235 "errcode": 40007,
236 "errmsg": "invalid media_id"
237 }
238
239 =head4 RETURN 参数说明:
240
241 参数 说明
242 errcode 出错返回码
243 errmsg 对返回码的文本描述内容
244
245 =cut
246
247 sub get {
248 if ( @_ && $_[0] && $_[1] ) {
249 my $access_token = $_[0];
250 my $media_id = $_[1];
251 my $ua = LWP::UserAgent->new;
252 $ua->timeout(30);
253 $ua->env_proxy;
254
255 my $response = $ua->get("https://qyapi.weixin.qq.com/cgi-bin/media/get?access_token=$access_token&media_id=$media_id");
256 if ($response->is_success) {
257 my $reply;
258 $reply->{"content-type"} = $response->content_type;
259 $reply->{filename} = $response->filename;
260 $reply->{data} = $response->decoded_content;
261 return $reply;
262 # return $response->decoded_content;
263 }
264
265 }
266 return 0;
267 }
268
269 =head2 异步上传临时素材
270
271 最后更新:2022/12/14
272
273 =head2 SYNOPSIS
274
275 L<https://developer.work.weixin.qq.com/document/path/96219>
276
277 =head3 请求说明:
278
279 为了满足临时素材的大文件诉求(最高支持200M),支持指定文件的CDN链接(必须支持Range分块下载),由企微微信后台异步下载和处理,处理完成后回调通知任务完成,再通过接口主动查询任务结果。
280
281 跟普通临时素材一样,media_id仅三天内有效,media_id在同一企业内应用之间可以共享。
282
283 =head3 使用场景说明
284
285 跟上传临时素材拿到的media_id使用场景是不通用的,目前适配的接口如下:
286
287 接口 适用场景值(scene) 说明
288 获取临时素材 所有 若文件大小超过20M,必须使用Range分块下载且分块大小不超过20M,否则返回错误830002
289 入群欢迎语素材管理 1 添加素材、编辑素材兼容video和file两种素材类型使用;
290 获取素材返回的media_id类型则跟添加/编辑时的media_id类型对应
291
292 =head2 upload_by_url(access_token, type, media);
293
294 生成异步上传任务
295
296 =head2 SYNOPSIS
297
298 L<https://developer.work.weixin.qq.com/document/path/96219#生成异步上传任务>
299
300 =head3 请求说明:
301
302 =head4 请求包结构体为:
303
304 {
305 "scene": 1,
306 "type": "video",
307 "filename": "video.mp4",
308 "url": "https://xxxx",
309 "md5": "MD5"
310 }
311
312 =head4 参数说明:
313
314 参数 必须 说明
315 access_token 是 调用接口凭证
316 scene uint32 是 场景值。1-客户联系入群欢迎语素材(目前仅支持1)。
317 注意:每个场景值有对应的使用范围,详见上面的「使用场景说明」
318 type string 是 媒体文件类型。目前仅支持video-视频,file-普通文件
319 不超过32字节。
320 filename string 是 文件名,标识文件展示的名称。比如,使用该media_id发消息时,展示的文件名由该字段控制。
321 不超过128字节。
322 url string 是 文件cdn url。url要求支持Range分块下载
323 不超过1024字节。
324 如果为腾讯云cos链接,则需要设置为「公有读」权限。
325 md5 string 是 文件md5。对比从url下载下来的文件md5是否一致。
326 不超过32字节。
327
328 =head4 权限说明:
329
330 客户联系权限
331
332 =head4 上传的媒体文件限制
333
334 所有文件size必须大于5个字节
335
336 图片(image):暂不支持
337 语音(voice) :暂不支持
338 视频(video) :200MB,仅支持MP4格式
339 普通文件(file):200MB
340
341 =head3 RETURN 返回结果:
342
343 {
344 "errcode": 0,
345 "errmsg": "ok",
346 "jobid": "jobid"
347 }
348
349 =head4 RETURN 参数说明:
350
351 参数 说明
352 errcode 返回码
353 errmsg 对返回码的文本描述内容
354 jobid 任务id。可通过此jobid查询结果
355
356 =cut
357
358 sub upload_by_url {
359 if ( @_ && $_[0] && ref $_[1] eq 'HASH' ) {
360 my $access_token = $_[0];
361 my $json = $_[1];
362 my $ua = LWP::UserAgent->new;
363 $ua->timeout(30);
364 $ua->env_proxy;
365
366 my $response = $ua->post("https://qyapi.weixin.qq.com/cgi-bin/media/upload_by_url?access_token=$access_token",Content => to_json($json,{allow_nonref=>1}),Content_type =>'application/json');
367 if ($response->is_success) {
368 return from_json($response->decoded_content,{utf8 => 1, allow_nonref => 1});
369 }
370
371 }
372 return 0;
373 }
374
375 =head2 get_upload_by_url_result(access_token, type, media);
376
377 查询异步任务结果
378
379 =head2 SYNOPSIS
380
381 L<https://developer.work.weixin.qq.com/document/path/96219#查询异步任务结果>
382
383 =head3 请求说明:
384
385 =head4 请求包结构体为:
386
387 {
388 "jobid": "JOBID"
389 }
390
391
392 =head4 参数说明:
393
394 参数 必须 说明
395 access_token 是 调用接口凭证
396 jobid string 是 任务id。最长为128字节,60分钟内有效
397
398 =head4 权限说明:
399
400 客户联系权限
401
402 =head3 RETURN 返回结果:
403
404 {
405 "errcode": 0,
406 "errmsg": "ok",
407 "status": 2,
408 "detail": {
409 "errcode": 0,
410 "errmsg": "ok",
411 "media_id": "3*1*G6nrLmr5EC3MMb_-zK1dDdzmd0p7cNliYu9V5w7o8K0",
412 "created_at": "1380000000"
413 }
414 }
415
416 =head4 RETURN 参数说明:
417
418 参数 说明
419 errcode 返回码
420 errmsg 对返回码的文本描述内容
421 status string 任务状态。1-处理中,2-完成,3-异常失败
422 detail obj 结果明细
423 detail.errcode int32 任务失败返回码。当status为3时返回非0,其他返回0
424 detail.errmsg string 任务失败错误码描述
425 detail.media_id string 媒体文件上传后获取的唯一标识,3天内有效。当status为2时返回。
426 detail.created_at string 媒体文件创建的时间戳。当status为2时返回。
427
428 =head4 任务结果常见错误码列表(detail.errcode)
429
430 错误码 错误说明 排查方法
431 830001 url非法 确认url是否支持Range分块下载
432 830003 url下载数据失败 确认url本身是否能正常访问
433 45001 文件大小超过限制 确认文件在5字节~200M范围内
434 301019 文件MD5不匹配 确认url对应的文件内容md5,跟所填的md5参数是否一致
435
436 =cut
437
438 sub get_upload_by_url_result {
439 if ( @_ && $_[0] && ref $_[1] eq 'HASH' ) {
440 my $access_token = $_[0];
441 my $json = $_[1];
442 my $ua = LWP::UserAgent->new;
443 $ua->timeout(30);
444 $ua->env_proxy;
445
446 my $response = $ua->post("https://qyapi.weixin.qq.com/cgi-bin/media/get_upload_by_url_result?access_token=$access_token",Content => to_json($json,{allow_nonref=>1}),Content_type =>'application/json');
447 if ($response->is_success) {
448 return from_json($response->decoded_content,{utf8 => 1, allow_nonref => 1});
449 }
450
451 }
452 return 0;
453 }
454
455 =head2 upload_attachment(access_token, type, attachment_type, media);
456
457 上传附件资源
458 最后更新:2023/12/01
459
460 =head2 SYNOPSIS
461
462 L<https://developer.work.weixin.qq.com/document/path/95098>
463
464 =head3 请求说明:
465
466 使用multipart/form-data POST上传文件, 文件标识名为"media"
467
468 =head4 参数说明:
469
470 参数 必须 说明
471 access_token 是 调用接口凭证
472 media_type 是 媒体文件类型,分别有图片(image)、视频(video)、普通文件(file)
473 attachment_type 是 附件类型,不同的附件类型用于不同的场景。1:朋友圈;2:商品图册
474
475 =head4 权限说明:
476
477 素材上传得到media_id,该media_id仅三天内有效
478 media_id在同一企业内所有应用之间可以共享。
479
480 注:朋友圈附件类型:如果是客户端jsapi或者小程序接口使用,仅支持企业微信客户端版本在4.0.2及以上版本使用。不然可能显示异常。
481
482 POST的请求包中,form-data中媒体文件标识,应包含有 filename、filelength、content-type等信息
483 filename标识文件展示的名称。比如,使用该media_id发消息时,展示的文件名由该字段控制
484 朋友圈附件类型,仅支持图片与视频
485
486 权限说明:
487 调用接口的应用需要满足如下的权限:
488
489 应用类型 权限要求
490 自建应用 配置到「客户联系 可调用接口的应用」中
491 代开发应用 具有「企业客户」权限
492 第三方应用 具有「企业客户」权限
493
494 注: 从2023年12月1日0点起,不再支持通过系统应用secret调用接口,存量企业暂不受影响 查看详情
495
496 =head3 请求示例:
497
498 POST https://qyapi.weixin.qq.com/cgi-bin/media/upload_attachment?access_token=accesstoken001&media_type=TYPE&attachment_type=1 HTTP/1.1
499 Content-Type: multipart/form-data; boundary=-------------------------acebdf13572468
500 Content-Length: 220
501
502 ---------------------------acebdf13572468
503 Content-Disposition: form-data; name="media";filename="wework.txt"; filelength=6
504 Content-Type: application/octet-stream
505
506 mytext
507 ---------------------------acebdf13572468--
508
509
510 =head3 返回数据:
511
512 {
513 "errcode": 0,
514 "errmsg": "",
515 "type": "image",
516 "media_id": "1G6nrLmr5EC3MMb_-zK1dDdzmd0p7cNliYu9V5w7o8K0",
517 "created_at": "1380000000"
518 }
519
520
521 =head4 RETURN 参数说明:
522
523 参数 说明
524 type 媒体文件类型,分别有图片(image)、语音(voice)、视频(video),普通文件(file)
525 media_id 媒体文件上传后获取的唯一标识,3天内有效
526 created_at 媒体文件上传时间戳
527
528 =head4 上传的媒体文件限制
529
530 所有文件size必须大于5个字节
531
532 图片(image):10MB,支持JPG,PNG格式
533 语音(voice) :2MB,播放长度不超过60s,仅支持AMR格式
534 视频(video) :10MB,支持MP4格式
535 普通文件(file):20MB
536
537 =cut
538
539 sub upload_attachment {
540 if ( @_ && $_[0] && $_[1] && $_[2] && $_[3] ) {
541 my $access_token = $_[0];
542 my $media_type = $_[1];
543 my $attachment_type = $_[2];
544 my $file = $_[3];
545 my $ua = LWP::UserAgent->new;
546 $ua->timeout(30);
547 $ua->env_proxy;
548
549 my $response = $ua->post("https://qyapi.weixin.qq.com/cgi-bin/media/upload_attachment?access_token=$access_token&media_type=$media_type&attachment_type=$attachment_type",[media => [$file]],Content_Type => 'multipart/form-data');
550 if ($response->is_success) {
551 return from_json($response->decoded_content,{utf8 => 1, allow_nonref => 1});
552 }
553
554 }
555 return 0;
556 }
557
558 1;
559 __END__