work/kf/knowledge.pm untested

filter: Statementstmt 0.0% 0 / 101 Branchbran 0.0% 0 / 32 Conditioncond 0.0% 0 / 48 MC/DCmcdc 0.0% 0 / 24 Subroutinesub 0.0% 0 / 14 Podpod 0.0% 0 / 8 totaltotal 0.0% 0 / 227 CC 17 SCAR 23.0
CC
17
Cov
0%
add_group
12.0
del_group
12.0
mod_group
12.0
CRAP 10.0
1 package QQ::weixin::work::kf::knowledge;
2
3 =encoding utf8
4
5 =head1 Name
6
7 QQ::weixin::work::kf::knowledge
8
9 =head1 DESCRIPTION
10
11 =cut
12
13 use strict;
14 use base qw(QQ::weixin::work::kf);
15 use Encode;
16 use LWP::UserAgent;
17 use JSON;
18 use utf8;
19
20 our $VERSION = '0.10';
21 our @EXPORT = qw/ add_group del_group mod_group list_group
22 add_intent del_intent mod_intent list_intent /;
23
24 =head1 FUNCTION
25
26 =head2 知识库分组管理
27 最后更新:2023/11/30
28
29 =head3 请求说明:
30
31 通过分组管理接口,可操作企业管理后台-微信客服-机器人-知识库的分组数据。
32
33 相关的约束条件跟管理后台一致:
34
35 分组名不能重复
36 全部分组数上限为100
37
38 =head4 权限说明:
39
40 调用的应用需要满足如下的权限
41
42 应用类型 权限要求
43 自建应用 配置到「 微信客服- 可调用接口的应用」中
44 第三方应用 暂不支持
45 代开发自建应用 暂不支持
46 注: 从2023年12月1日0点起,不再支持通过系统应用secret调用接口,存量企业暂不受影响 查看详情
47
48 =head2 add_group(access_token, hash);
49
50 添加分组
51
52 =head2 SYNOPSIS
53
54 L<https://developer.work.weixin.qq.com/document/path/95971#添加分组>
55
56 =head3 请求说明:
57
58 可通过此接口创建新的知识库分组。
59
60 =head4 请求包结构体为:
61
62 {
63 "name": "分组名"
64 }
65
66 =head4 参数说明:
67
68 参数 必须 类型 说明
69 access_token 是 调用接口凭证
70 name stirng 是 分组名。不超过12个字
71
72 =head3 权限说明
73
74 =head3 RETURN 返回结果
75
76 {
77 "errcode": 0,
78 "errmsg": "ok",
79 "group_id" "GROUP_ID"
80 }
81
82 =head4 RETURN 参数说明
83
84 参数 类型 说明
85 errcode int 返回码
86 errmsg string 错误码描述
87 group_id string 分组ID
88
89 =cut
90
91 sub add_group {
92 if ( @_ && $_[0] && ref $_[1] eq 'HASH' ) {
93 my $access_token = $_[0];
94 my $json = $_[1];
95 my $ua = LWP::UserAgent->new;
96 $ua->timeout(30);
97 $ua->env_proxy;
98
99 my $response = $ua->post("https://qyapi.weixin.qq.com/cgi-bin/kf/knowledge/add_group?access_token=$access_token",Content => to_json($json,{allow_nonref=>1}),Content_type =>'application/json');
100 if ($response->is_success) {
101 return from_json($response->decoded_content,{utf8 => 1, allow_nonref => 1});
102 }
103
104 }
105 return 0;
106 }
107
108 =head2 del_group(access_token, hash);
109
110 删除分组
111
112 =head2 SYNOPSIS
113
114 L<https://developer.work.weixin.qq.com/document/path/95971#删除分组>
115
116 =head3 请求说明:
117
118 可通过此接口删除已有的知识库分组,但不能删除系统创建的默认分组。
119
120 =head4 请求包结构体为:
121
122 {
123 "group_id": "GROUP_ID"
124 }
125
126 =head4 参数说明:
127
128 参数 必须 类型 说明
129 access_token 是 调用接口凭证
130 group_id string 是 分组ID
131
132 =head3 权限说明
133
134 =head3 RETURN 返回结果
135
136 {
137 "errcode": 0,
138 "errmsg": "ok"
139 }
140
141 =head4 RETURN 参数说明
142
143 参数 类型 说明
144 errcode int 返回码
145 errmsg string 错误码描述
146
147 =cut
148
149 sub del_group {
150 if ( @_ && $_[0] && ref $_[1] eq 'HASH' ) {
151 my $access_token = $_[0];
152 my $json = $_[1];
153 my $ua = LWP::UserAgent->new;
154 $ua->timeout(30);
155 $ua->env_proxy;
156
157 my $response = $ua->post("https://qyapi.weixin.qq.com/cgi-bin/kf/knowledge/del_group?access_token=$access_token",Content => to_json($json,{allow_nonref=>1}),Content_type =>'application/json');
158 if ($response->is_success) {
159 return from_json($response->decoded_content,{utf8 => 1, allow_nonref => 1});
160 }
161
162 }
163 return 0;
164 }
165
166 =head2 mod_group(access_token, hash);
167
168 修改分组
169
170 =head2 SYNOPSIS
171
172 L<https://developer.work.weixin.qq.com/document/path/95971#修改分组>
173
174 =head3 请求说明:
175
176 可通过此接口修改已有的知识库分组,但不能修改系统创建的默认分组。
177
178 =head4 请求包结构体为:
179
180 {
181 "group_id": "GROUP_ID",
182 "name": "分组名"
183 }
184
185 =head4 参数说明:
186
187 参数 必须 类型 说明
188 access_token 是 调用接口凭证
189 group_id string 是 分组ID
190 name stirng 是 分组名。不超过12个字
191
192 =head3 权限说明
193
194 =head3 RETURN 返回结果
195
196 {
197 "errcode": 0,
198 "errmsg": "ok"
199 }
200
201 =head4 RETURN 参数说明
202
203 参数 类型 说明
204 errcode int 返回码
205 errmsg string 错误码描述
206
207 =cut
208
209 sub mod_group {
210 if ( @_ && $_[0] && ref $_[1] eq 'HASH' ) {
211 my $access_token = $_[0];
212 my $json = $_[1];
213 my $ua = LWP::UserAgent->new;
214 $ua->timeout(30);
215 $ua->env_proxy;
216
217 my $response = $ua->post("https://qyapi.weixin.qq.com/cgi-bin/kf/knowledge/mod_group?access_token=$access_token",Content => to_json($json,{allow_nonref=>1}),Content_type =>'application/json');
218 if ($response->is_success) {
219 return from_json($response->decoded_content,{utf8 => 1, allow_nonref => 1});
220 }
221
222 }
223 return 0;
224 }
225
226 =head2 list_group(access_token, hash);
227
228 获取分组列表
229
230 =head2 SYNOPSIS
231
232 L<https://developer.work.weixin.qq.com/document/path/95971#获取分组列表>
233
234 =head3 请求说明:
235
236 可通过此接口分页获取所有的知识库分组。
237
238 =head4 请求包结构体为:
239
240 {
241 "cursor": "CURSOR",
242 "limit": 100,
243 "group_id": "GROUP_ID"
244 }
245
246 =head4 参数说明:
247
248 参数 必须 类型 说明
249 access_token 是 调用接口凭证
250 cursor string 否 上一次调用时返回的next_cursor,第一次拉取可以不填
251 limit uint32 否 每次拉取的数据量,默认值500,最大值为1000
252 group_id string 否 分组ID。可指定拉取特定的分组
253
254 =head3 权限说明
255
256 =head3 RETURN 返回结果
257
258 {
259 "errcode": 0,
260 "errmsg": "ok",
261 "next_cursor": "NEXT_CURSOR",
262 "has_more":1,
263 "group_list": [
264 {
265 "group_id": "GROUP_ID",
266 "name": "NAME",
267 "is_default": 1
268 }, {
269 "group_id": "GROUP_ID",
270 "name": "NAME",
271 "is_default": 0
272 }
273 ]
274 }
275
276 =head4 RETURN 参数说明
277
278 参数 类型 说明
279 errcode int 返回码
280 errmsg string 错误码描述
281 next_cursor string 分页游标,再下次请求时填写以获取之后分页的记录
282 has_more uint32 是否还有更多数据。0-没有 1-有
283 group_list obj[] 分组列表
284 group_list[].group_id string 分组ID
285 group_list[].name string 分组名
286 group_list[].is_default uint32 是否为默认分组。0-否 1-是。默认分组为系统自动创建,不可修改/删除
287
288 =cut
289
290 sub list_group {
291 if ( @_ && $_[0] && ref $_[1] eq 'HASH' ) {
292 my $access_token = $_[0];
293 my $json = $_[1];
294 my $ua = LWP::UserAgent->new;
295 $ua->timeout(30);
296 $ua->env_proxy;
297
298 my $response = $ua->post("https://qyapi.weixin.qq.com/cgi-bin/kf/knowledge/list_group?access_token=$access_token",Content => to_json($json,{allow_nonref=>1}),Content_type =>'application/json');
299 if ($response->is_success) {
300 return from_json($response->decoded_content,{utf8 => 1, allow_nonref => 1});
301 }
302
303 }
304 return 0;
305 }
306
307 =head2 知识库问答管理
308 最后更新:2023/11/30
309
310 =head3 请求说明:
311
312 通过问答管理接口,可操作企业管理后台-微信客服-机器人-知识库的问答数据。
313
314 相关的约束条件跟管理后台一致:
315
316 不同分组的问题不能重复
317 单个分组的问答数上限为200
318
319 =head4 权限说明:
320
321 调用的应用需要满足如下的权限
322
323 应用类型 权限要求
324 自建应用 配置到「 微信客服- 可调用接口的应用」中
325 第三方应用 暂不支持
326 代开发自建应用 暂不支持
327 注: 从2023年12月1日0点起,不再支持通过系统应用secret调用接口,存量企业暂不受影响 查看详情
328
329 =head2 add_intent(access_token, hash);
330
331 添加问答
332
333 =head2 SYNOPSIS
334
335 L<https://developer.work.weixin.qq.com/document/path/95972#添加问答>
336
337 =head3 请求说明:
338
339 可通过此接口创建新的知识库问答。
340
341 =head4 请求包结构体为:
342
343 {
344 "group_id": "GROUP_ID",
345 "question": {
346 "text": {
347 "content": "主问题"
348 }
349 },
350 "similar_questions": {
351 "items": [
352 {
353 "text": {
354 "content": "相似问题1"
355 }
356 },
357 {
358 "text": {
359 "content": "相似问题2"
360 }
361 }
362 ]
363 },
364 "answers": [
365 {
366 "text": {
367 "content": "问题的回复"
368 },
369 "attachments": [
370 {
371 "msgtype": "image",
372 "image": {
373 "media_id": "MEDIA_ID"
374 }
375 }
376 ]
377 }
378 ]
379 }
380
381 =head4 参数说明:
382
383 参数 必须 类型 说明
384 access_token 是 调用接口凭证
385 group_id string 是 分组ID
386 question obj 是 主问题
387 question.text obj 是 主问题文本
388 question.text.content string 是 主问题文本内容。不超过200个字
389 similar_questions obj 否 相似问题
390 similar_questions.items obj[] 否 相似问题列表。最多支持100个
391 similar_questions.items[].text obj 是 相似问题文本
392 similar_questions.items[].text.content string 是 相似问题文本内容。不超过200个字
393 answers obj[] 是 回答列表。目前仅支持1个
394 answers[].text obj 是 回答文本
395 answers[].text.content string 是 回答文本内容。不超过500个字
396 answers[].attachments obj[] 否 回答附件列表。最多支持4个
397 answers[].attachments[].* obj 是 回答附件。具体见附录-问答附件类型
398
399 =head3 权限说明
400
401 =head3 RETURN 返回结果
402
403 {
404 "errcode": 0,
405 "errmsg": "ok",
406 "intent_id": "INTENT_ID"
407 }
408
409 =head4 RETURN 参数说明
410
411 参数 类型 说明
412 errcode int 返回码
413 errmsg string 错误码描述
414 intent_id string 问答ID
415
416 =cut
417
418 sub add_intent {
419 if ( @_ && $_[0] && ref $_[1] eq 'HASH' ) {
420 my $access_token = $_[0];
421 my $json = $_[1];
422 my $ua = LWP::UserAgent->new;
423 $ua->timeout(30);
424 $ua->env_proxy;
425
426 my $response = $ua->post("https://qyapi.weixin.qq.com/cgi-bin/kf/knowledge/add_intent?access_token=$access_token",Content => to_json($json,{allow_nonref=>1}),Content_type =>'application/json');
427 if ($response->is_success) {
428 return from_json($response->decoded_content,{utf8 => 1, allow_nonref => 1});
429 }
430
431 }
432 return 0;
433 }
434
435 =head2 del_intent(access_token, hash);
436
437 删除问答
438
439 =head2 SYNOPSIS
440
441 L<https://developer.work.weixin.qq.com/document/path/95972#删除问答>
442
443 =head3 请求说明:
444
445 可通过此接口删除已有的知识库问答。
446
447 =head4 请求包结构体为:
448
449 {
450 "intent_id": "INTENT_ID"
451 }
452
453 =head4 参数说明:
454
455 参数 必须 类型 说明
456 access_token 是 调用接口凭证
457 intent_id string 是 问答ID
458
459 =head3 权限说明
460
461 =head3 RETURN 返回结果
462
463 {
464 "errcode": 0,
465 "errmsg": "ok"
466 }
467
468 =head4 RETURN 参数说明
469
470 参数 类型 说明
471 errcode int 返回码
472 errmsg string 错误码描述
473
474 =cut
475
476 sub del_intent {
477 if ( @_ && $_[0] && ref $_[1] eq 'HASH' ) {
478 my $access_token = $_[0];
479 my $json = $_[1];
480 my $ua = LWP::UserAgent->new;
481 $ua->timeout(30);
482 $ua->env_proxy;
483
484 my $response = $ua->post("https://qyapi.weixin.qq.com/cgi-bin/kf/knowledge/del_intent?access_token=$access_token",Content => to_json($json,{allow_nonref=>1}),Content_type =>'application/json');
485 if ($response->is_success) {
486 return from_json($response->decoded_content,{utf8 => 1, allow_nonref => 1});
487 }
488
489 }
490 return 0;
491 }
492
493 =head2 mod_intent(access_token, hash);
494
495 修改问答
496
497 =head2 SYNOPSIS
498
499 L<https://developer.work.weixin.qq.com/document/path/95972#修改问答>
500
501 =head3 请求说明:
502
503 可通过此接口修改已有的知识库问答。
504 question/similar_questions/answers这三部分可以按需更新,但更新的每一部分是覆盖写,需要传完整的字段。
505
506 =head4 请求包结构体为:
507
508 {
509 "intent_id": "INTENT_ID",
510 "question": {
511 "text": {
512 "content": "主问题"
513 }
514 },
515 "similar_questions": {
516 "items": [
517 {
518 "text": {
519 "content": "相似问题1"
520 }
521 },
522 {
523 "text": {
524 "content": "相似问题2"
525 }
526 }
527 ]
528 },
529 "answers": [
530 {
531 "text": {
532 "content": "问题的回复"
533 },
534 "attachments": [
535 {
536 "msgtype": "image",
537 "image": {
538 "media_id": "MEDIA_ID"
539 }
540 }
541 ]
542 }
543 ]
544 }
545
546 =head4 参数说明:
547
548 参数 必须 类型 说明
549 access_token 是 调用接口凭证
550 intent_id string 是 问答ID
551 question obj 否 主问题
552 question.text obj 否 主问题文本
553 question.text.content string 是 主问题文本内容
554 similar_questions obj 否 相似问题
555 similar_questions.items obj[] 否 相似问题列表。最多支持100个
556 similar_questions.items[].text obj 是 相似问题文本
557 similar_questions.items[].text.content string 是 相似问题文本内容
558 answers obj[] 否 回答列表。目前仅支持1个
559 answers[].text obj 是 回答文本
560 answers[].text.content string 是 回答文本内容
561 answers[].attachments obj[] 否 回答附件列表。最多支持4个
562 answers[].attachments[].* obj 是 回答附件。具体见附录-问答附件类型
563
564 =head3 权限说明
565
566 =head3 RETURN 返回结果
567
568 {
569 "errcode": 0,
570 "errmsg": "ok"
571 }
572
573 =head4 RETURN 参数说明
574
575 参数 类型 说明
576 errcode int 返回码
577 errmsg string 错误码描述
578
579 =cut
580
581 sub mod_intent {
582 if ( @_ && $_[0] && ref $_[1] eq 'HASH' ) {
583 my $access_token = $_[0];
584 my $json = $_[1];
585 my $ua = LWP::UserAgent->new;
586 $ua->timeout(30);
587 $ua->env_proxy;
588
589 my $response = $ua->post("https://qyapi.weixin.qq.com/cgi-bin/kf/knowledge/mod_intent?access_token=$access_token",Content => to_json($json,{allow_nonref=>1}),Content_type =>'application/json');
590 if ($response->is_success) {
591 return from_json($response->decoded_content,{utf8 => 1, allow_nonref => 1});
592 }
593
594 }
595 return 0;
596 }
597
598 =head2 list_intent(access_token, hash);
599
600 获取问答列表
601
602 =head2 SYNOPSIS
603
604 L<https://developer.work.weixin.qq.com/document/path/95972#获取问答列表>
605
606 =head3 请求说明:
607
608 可通过此接口分页获取的知识库问答详情列表。
609
610 =head4 请求包结构体为:
611
612 {
613 "cursor": "CURSOR",
614 "limit": 100,
615 "group_id": "GROUP_ID",
616 "intent_id": "INTENT_ID"
617 }
618
619 =head4 参数说明:
620
621 参数 必须 类型 说明
622 access_token 是 调用接口凭证
623 cursor string 否 上一次调用时返回的next_cursor,第一次拉取可以不填
624 limit uint32 否 每次拉取的数据量,默认值500,最大值为1000
625 group_id string 否 分组ID。可指定拉取特定分组下的问答
626 intent_id string 否 问答ID。可指定拉取特定的问答
627
628 =head3 权限说明
629
630 =head3 RETURN 返回结果
631
632 {
633 "errcode": 0,
634 "errmsg": "ok",
635 "next_cursor": "NEXT_CURSOR",
636 "has_more": 1,
637 "intent_list": [
638 {
639 "group_id": "GROUP_ID",
640 "intent_id": "INTENT_ID",
641 "question": {
642 "text": {
643 "content": "主问题"
644 },
645 "similar_questions": {
646 "items": [
647 {
648 "text": {
649 "content": "相似问题1"
650 }
651 },
652 {
653 "text": {
654 "content": "相似问题2"
655 }
656 }
657 ]
658 },
659 "answers": [
660 {
661 "text": {
662 "content": "问题的回复"
663 },
664 "attachments": [
665 {
666 "msgtype": "image",
667 "image": {
668 "name": "图片(仅返回名字).jpg"
669 }
670 }
671 ]
672 }
673 ]
674 }
675 }
676 ]
677 }
678
679 =head4 RETURN 参数说明
680
681 参数 类型 说明
682 errcode int 返回码
683 errmsg string 错误码描述
684 next_cursor string 分页游标,再下次请求时填写以获取之后分页的记录
685 has_more uint32 是否还有更多数据。0-没有 1-有
686 intent_list obj[] 问答摘要列表
687 intent_list[].group_id string 分组ID
688 intent_list[].intent_id string 问答ID
689 intent_list[].question obj 主问题
690 intent_list[].question.text obj 主问题文本
691 intent_list[].question.text.content string 主问题文本内容
692 intent_list[].similar_questions obj 相似问题
693 intent_list[].similar_questions.items obj[] 相似问题列表。最多支持100个
694 intent_list[].similar_questions.items[].text obj 相似问题文本
695 intent_list[].similar_questions.items[].text.content string 相似问题文本内容
696 intent_list[].answers obj[] 回答列表。目前仅支持1个
697 intent_list[].answers[].text obj 回答文本
698 intent_list[].answers[].text.content string 回答文本内容
699 intent_list[].answers[].attachments obj[] 回答附件列表。最多支持4个
700 intent_list[].answers[].attachments[].* obj 回答附件。具体见附录-问答附件类型
701
702 =cut
703
704 sub list_intent {
705 if ( @_ && $_[0] && ref $_[1] eq 'HASH' ) {
706 my $access_token = $_[0];
707 my $json = $_[1];
708 my $ua = LWP::UserAgent->new;
709 $ua->timeout(30);
710 $ua->env_proxy;
711
712 my $response = $ua->post("https://qyapi.weixin.qq.com/cgi-bin/kf/knowledge/list_intent?access_token=$access_token",Content => to_json($json,{allow_nonref=>1}),Content_type =>'application/json');
713 if ($response->is_success) {
714 return from_json($response->decoded_content,{utf8 => 1, allow_nonref => 1});
715 }
716
717 }
718 return 0;
719 }
720
721 1;
722 __END__