| 1 | package QQ::weixin::work::kf; | ||
| 2 | |||
| 3 | =encoding utf8 | ||
| 4 | |||
| 5 | =head1 Name | ||
| 6 | |||
| 7 | QQ::weixin::work::kf | ||
| 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/ add_contact_way | ||
| 24 | sync_msg send_msg send_msg_on_event | ||
| 25 | get_corp_statistic get_servicer_statistic /; | ||
| 26 | |||
| 27 | =head1 FUNCTION | ||
| 28 | |||
| 29 | =head2 add_contact_way(access_token, hash); | ||
| 30 | |||
| 31 | 获取客服帐号链接 | ||
| 32 | |||
| 33 | =head2 SYNOPSIS | ||
| 34 | |||
| 35 | L<https://developer.work.weixin.qq.com/document/path/94665> | ||
| 36 | |||
| 37 | =head3 请求说明: | ||
| 38 | |||
| 39 | 企业可通过此接口获取带有不同参数的客服链接,不同客服帐号对应不同的客服链接。获取后,企业可将链接嵌入到网页等场景中,微信用户点击链接即可向对应的客服帐号发起咨询。企业可依据参数来识别用户的咨询来源等。 | ||
| 40 | |||
| 41 | =head4 请求包结构体为: | ||
| 42 | |||
| 43 | { | ||
| 44 | "open_kfid": "OPEN_KFID", | ||
| 45 | "scene": "12345" | ||
| 46 | } | ||
| 47 | |||
| 48 | =head4 参数说明: | ||
| 49 | |||
| 50 | 参数 必须 类型 说明 | ||
| 51 | access_token 是 string 调用接口凭证 | ||
| 52 | open_kfid 是 string 客服帐号ID | ||
| 53 | scene 否 string 场景值,字符串类型,由开发者自定义。 | ||
| 54 | 不多于32字节 | ||
| 55 | 字符串取值范围(正则表达式):[0-9a-zA-Z_-]* | ||
| 56 | |||
| 57 | 1. 若scene非空,返回的客服链接开发者可拼接scene_param=SCENE_PARAM参数使用,用户进入会话事件会将SCENE_PARAM原样返回。其中SCENE_PARAM需要urlencode,且长度不能超过128字节。 | ||
| 58 | 如 https://work.weixin.qq.com/kf/kfcbf8f8d07ac7215f?enc_scene=ENCGFSDF567DF&scene_param=a%3D1%26b%3D2 | ||
| 59 | 2. 历史调用接口返回的客服链接(包含encScene=XXX参数),不支持scene_param参数。 | ||
| 60 | 3. 返回的客服链接,不能修改或复制参数到其他链接使用。否则进入会话事件参数校验不通过,导致无法回调。 | ||
| 61 | |||
| 62 | =head3 权限说明 | ||
| 63 | |||
| 64 | 调用的应用需要满足如下的权限 | ||
| 65 | |||
| 66 | 应用类型 权限要求 | ||
| 67 | 自建应用 配置到「 微信客服- 可调用接口的应用」中 | ||
| 68 | 第三方应用 具有“微信客服->获取基础信息”权限 | ||
| 69 | 代开发自建应用 具有“微信客服->获取基础信息”权限 | ||
| 70 | 微信客服组件应用 具有“管理接入的微信客服->获取企业授权接入的客服账号->客服账号信息与链接”权限,仅可获取企业已授权的客服账号链接 | ||
| 71 | |||
| 72 | 注: 从2023年12月1日0点起,不再支持通过系统应用secret调用接口,存量企业暂不受影响 查看详情 | ||
| 73 | |||
| 74 | =head3 RETURN 返回结果 | ||
| 75 | |||
| 76 | { | ||
| 77 | "errcode": 0, | ||
| 78 | "errmsg": "ok", | ||
| 79 | "url":"https://work.weixin.qq.com/kf/kfcbf8f8d07ac7215f?enc_scene=ENCGFSDF567DF" | ||
| 80 | } | ||
| 81 | |||
| 82 | =head4 RETURN 参数说明 | ||
| 83 | |||
| 84 | 参数 类型 说明 | ||
| 85 | errcode int32 返回码 | ||
| 86 | errmsg string 对返回码的文本描述内容 | ||
| 87 | url string 客服链接,开发者可将该链接嵌入到H5页面中,用户点击链接即可向对应的微信客服帐号发起咨询。开发者也可根据该url自行生成需要的二维码图片 | ||
| 88 | |||
| 89 | =cut | ||
| 90 | |||
| 91 | sub add_contact_way { | ||
| 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/add_contact_way?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 sync_msg(access_token, hash); | ||
| 109 | |||
| 110 | 接收消息和事件 | ||
| 111 | 最后更新:2024/01/31 | ||
| 112 | 读取消息 | ||
| 113 | |||
| 114 | =head2 SYNOPSIS | ||
| 115 | |||
| 116 | L<https://developer.work.weixin.qq.com/document/path/94670#读取消息> | ||
| 117 | |||
| 118 | =head3 请求说明: | ||
| 119 | |||
| 120 | 微信客户发送的消息、接待人员在企业微信回复的消息、发送消息接口发送失败事件(如被用户拒收)、客户点击菜单消息的回复消息,可以通过该接口获取最近3天内具体的消息内容和事件。不支持读取通过发送消息接口发送的消息。 | ||
| 121 | 支持的消息类型:文本、图片、语音、视频、文件、位置、链接、名片、小程序、菜单、事件。 | ||
| 122 | |||
| 123 | 图片、语音、视频、文件消息的媒体文件有如下大小限制,超出会获取到文本提示消息: | ||
| 124 | |||
| 125 | 图片:2MB | ||
| 126 | 语音:2MB | ||
| 127 | 视频:10MB | ||
| 128 | 文件:20MB | ||
| 129 | |||
| 130 | 接口定义 | ||
| 131 | |||
| 132 | =head4 请求包结构体为: | ||
| 133 | |||
| 134 | { | ||
| 135 | "cursor": "4gw7MepFLfgF2VC5npN", | ||
| 136 | "token": "ENCApHxnGDNAVNY4AaSJKj4Tb5mwsEMzxhFmHVGcra996NR", | ||
| 137 | "limit": 1000, | ||
| 138 | "voice_format": 0, | ||
| 139 | "open_kfid": "wkxxxxxx" | ||
| 140 | } | ||
| 141 | |||
| 142 | =head4 参数说明: | ||
| 143 | |||
| 144 | 参数 必须 类型 说明 | ||
| 145 | access_token 是 string 调用接口凭证 | ||
| 146 | cursor 否 string 上一次调用时返回的next_cursor,第一次拉取可以不填。 | ||
| 147 | 不多于64字节 | ||
| 148 | token 否 string 回调事件返回的token字段,10分钟内有效;可不填,如果不填接口有严格的频率限制。 | ||
| 149 | 不多于128字节 | ||
| 150 | limit 否 uint32 期望请求的数据量,默认值和最大值都为1000。 | ||
| 151 | 注意:可能会出现返回条数少于limit的情况,需结合返回的has_more字段判断是否继续请求。 | ||
| 152 | voice_format 否 uint32 语音消息类型,0-Amr 1-Silk,默认0。可通过该参数控制返回的语音格式 | ||
| 153 | open_kfid 是 string 指定拉取某个客服账号的消息 | ||
| 154 | |||
| 155 | =head3 权限说明 | ||
| 156 | |||
| 157 | 调用的应用需要满足如下的权限 | ||
| 158 | |||
| 159 | 应用类型 权限要求 | ||
| 160 | 自建应用 配置到「 微信客服- 可调用接口的应用」中 | ||
| 161 | 第三方应用 具有“微信客服->管理账号、分配会话和收发消息”权限 | ||
| 162 | 代开发自建应用 具有“微信客服->管理账号、分配会话和收发消息”权限 | ||
| 163 | |||
| 164 | 注: 从2023年12月1日0点起,不再支持通过系统应用secret调用接口,存量企业暂不受影响 查看详情 | ||
| 165 | |||
| 166 | 只能通过API管理企业指定的客服账号。企业可在管理后台“微信客服-通过API管理微信客服账号”处设置对应的客服账号通过API来管理。 | ||
| 167 | 操作的客服账号对应的接待人员应在应用的可见范围内 | ||
| 168 | |||
| 169 | =head3 RETURN 返回结果 | ||
| 170 | |||
| 171 | { | ||
| 172 | "errcode": 0, | ||
| 173 | "errmsg": "ok", | ||
| 174 | "next_cursor": "4gw7MepFLfgF2VC5npN", | ||
| 175 | "has_more": 1, | ||
| 176 | "msg_list": [ | ||
| 177 | { | ||
| 178 | "msgid": "from_msgid_4622416642169452483", | ||
| 179 | "open_kfid": "wkAJ2GCAAASSm4_FhToWMFea0xAFfd3Q", | ||
| 180 | "external_userid": "wmAJ2GCAAAme1XQRC-NI-q0_ZM9ukoAw", | ||
| 181 | "send_time": 1615478585, | ||
| 182 | "origin": 3, | ||
| 183 | "servicer_userid": "Zhangsan", | ||
| 184 | "msgtype": "MSG_TYPE" | ||
| 185 | } | ||
| 186 | ] | ||
| 187 | } | ||
| 188 | |||
| 189 | =head4 RETURN 参数说明 | ||
| 190 | |||
| 191 | 参数 类型 说明 | ||
| 192 | errcode int32 返回码 | ||
| 193 | errmsg string 错误码描述 | ||
| 194 | next_cursor string 下次调用带上该值,则从当前的位置继续往后拉,以实现增量拉取。 | ||
| 195 | 强烈建议对改该字段入库保存,每次请求读取带上,请求结束后更新。避免因意外丢,导致必须从头开始拉取,引起消息延迟。 | ||
| 196 | has_more uint32 是否还有更多数据。0-否;1-是。 | ||
| 197 | 不能通过判断msg_list是否空来停止拉取,可能会出现has_more为1,而msg_list为空的情况 | ||
| 198 | msg_list obj[] 消息列表 | ||
| 199 | msg_list.msgid string 消息ID | ||
| 200 | msg_list.open_kfid string 客服帐号ID(msgtype为event,该字段不返回) | ||
| 201 | msg_list.external_userid string 客户UserID(msgtype为event,该字段不返回) | ||
| 202 | msg_list.send_time uint64 消息发送时间 | ||
| 203 | msg_list.origin uint32 消息来源。3-微信客户发送的消息 4-系统推送的事件消息 5-接待人员在企业微信客户端发送的消息 | ||
| 204 | msg_list.servicer_userid string 从企业微信给客户发消息的接待人员userid(msgtype为event,该字段不返回) | ||
| 205 | msg_list.msgtype string 对不同的msgtype,有相应的结构描述,下面进一步说明 | ||
| 206 | |||
| 207 | =cut | ||
| 208 | |||
| 209 | sub sync_msg { | ||
| 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/sync_msg?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 send_msg(access_token, hash); | ||
| 227 | |||
| 228 | 微信客服-会话分配与消息收发-发送消息 | ||
| 229 | 最后更新:2023/12/19 | ||
| 230 | |||
| 231 | =head2 SYNOPSIS | ||
| 232 | |||
| 233 | L<https://developer.work.weixin.qq.com/document/path/94677> | ||
| 234 | |||
| 235 | =head3 请求说明: | ||
| 236 | |||
| 237 | 当微信客户处于“新接入待处理”或“由智能助手接待”状态下,可调用该接口给用户发送消息。 | ||
| 238 | 注意仅当微信客户在主动发送消息给客服后的48小时内,企业可发送消息给客户,最多可发送5条消息;若用户继续发送消息,企业可再次下发消息。 | ||
| 239 | 支持发送消息类型:文本、图片、语音、视频、文件、图文、小程序、菜单消息、地理位置。 | ||
| 240 | 目前该接口允许下发消息条数和下发时限如下: | ||
| 241 | |||
| 242 | 用户动作 允许下发条数限制 下发时限 | ||
| 243 | 用户发送消息 5条 48 小时 | ||
| 244 | |||
| 245 | =head4 请求包结构体为: | ||
| 246 | |||
| 247 | |||
| 248 | |||
| 249 | =head4 参数说明: | ||
| 250 | |||
| 251 | 参数 必须 类型 说明 | ||
| 252 | access_token 是 string 调用接口凭证 | ||
| 253 | |||
| 254 | =head3 权限说明 | ||
| 255 | |||
| 256 | 调用的应用需要满足如下的权限 | ||
| 257 | |||
| 258 | 应用类型 权限要求 | ||
| 259 | 自建应用 配置到「 微信客服- 可调用接口的应用」中 | ||
| 260 | 第三方应用 具有“微信客服->管理账号、分配会话和收发消息”权限 | ||
| 261 | 代开发自建应用 具有“微信客服->管理账号、分配会话和收发消息”权限 | ||
| 262 | |||
| 263 | 注: 从2023年12月1日0点起,不再支持通过系统应用secret调用接口,存量企业暂不受影响 查看详情 | ||
| 264 | |||
| 265 | 只能通过API管理企业指定的客服账号。企业可在管理后台“微信客服-通过API管理微信客服账号”处设置对应的客服账号通过API来管理。 | ||
| 266 | 操作的客服账号对应的接待人员应在应用的可见范围内 | ||
| 267 | |||
| 268 | =head3 RETURN 返回结果 | ||
| 269 | |||
| 270 | { | ||
| 271 | "errcode": 0, | ||
| 272 | "errmsg": "ok", | ||
| 273 | "msgid": "MSG_ID" | ||
| 274 | } | ||
| 275 | |||
| 276 | 注意:接口返回成功,不代表消息最终发送成功,还需要关注消息发送失败事件的回调事件。 | ||
| 277 | |||
| 278 | =head4 RETURN 参数说明 | ||
| 279 | |||
| 280 | 参数 类型 说明 | ||
| 281 | errcode int32 返回码 | ||
| 282 | errmsg string 错误码描述 | ||
| 283 | msgid string 消息ID。如果请求参数指定了msgid,则原样返回,否则系统自动生成并返回。若指定msgid,开发者需确保客服账号内唯一,否则接口返回错误。 | ||
| 284 | 不多于32字节 | ||
| 285 | 字符串取值范围(正则表达式):[0-9a-zA-Z_-]* | ||
| 286 | |||
| 287 | =cut | ||
| 288 | |||
| 289 | sub send_msg { | ||
| 290 | if ( @_ && $_[0] && ref $_[1] eq 'HASH' ) { | ||
| 291 | my $access_token = $_[0]; | ||
| 292 | my $json = $_[1]; | ||
| 293 | my $ua = LWP::UserAgent->new; | ||
| 294 | $ua->timeout(30); | ||
| 295 | $ua->env_proxy; | ||
| 296 | |||
| 297 | my $response = $ua->post("https://qyapi.weixin.qq.com/cgi-bin/kf/send_msg?access_token=$access_token",Content => to_json($json,{allow_nonref=>1}),Content_type =>'application/json'); | ||
| 298 | if ($response->is_success) { | ||
| 299 | return from_json($response->decoded_content,{utf8 => 1, allow_nonref => 1}); | ||
| 300 | } | ||
| 301 | |||
| 302 | } | ||
| 303 | return 0; | ||
| 304 | } | ||
| 305 | |||
| 306 | =head2 send_msg_on_event(access_token, hash); | ||
| 307 | |||
| 308 | 微信客服-会话分配与消息收发-发送欢迎语等事件响应消息 | ||
| 309 | 最后更新:2023/11/30 | ||
| 310 | |||
| 311 | =head2 SYNOPSIS | ||
| 312 | |||
| 313 | L<https://developer.work.weixin.qq.com/document/path/95122> | ||
| 314 | |||
| 315 | =head3 请求说明: | ||
| 316 | |||
| 317 | 当特定的事件回调消息包含code字段,或通过接口变更到特定的会话状态,会返回code字段。 | ||
| 318 | 开发者可以此code为凭证,调用该接口给用户发送相应事件场景下的消息,如客服欢迎语、客服提示语和会话结束语等。 | ||
| 319 | 除"用户进入会话事件"以外,响应消息仅支持会话处于获取该code的会话状态时发送,如将会话转入待接入池时获得的code仅能在会话状态为”待接入池排队中“时发送。 | ||
| 320 | |||
| 321 | 目前支持的事件场景和相关约束如下: | ||
| 322 | |||
| 323 | 事件场景 允许下发条数 code有效期 支持的消息类型 获取code途径 | ||
| 324 | 用户进入会话,用于发送客服欢迎语 1条 20秒 文本、菜单 事件回调 | ||
| 325 | 进入接待池,用于发送排队提示语等 1条 48小时 文本 转接会话接口 | ||
| 326 | 从接待池接入会话,用于发送非工作时间的提示语或超时未回复的提示语等 1条 48小时 文本 事件回调、转接会话接口 | ||
| 327 | 结束会话,用于发送结束会话提示语或满意度评价等 1条 20秒 文本、菜单 事件回调、转接会话接口 | ||
| 328 | |||
| 329 | =head4 请求包结构体为: | ||
| 330 | |||
| 331 | { | ||
| 332 | "code": "CODE", | ||
| 333 | "msgid": "MSG_ID", | ||
| 334 | "msgtype": "MSG_TYPE" | ||
| 335 | } | ||
| 336 | |||
| 337 | =head4 参数说明: | ||
| 338 | |||
| 339 | 参数 必须 类型 说明 | ||
| 340 | access_token 是 string 调用接口凭证 | ||
| 341 | code 是 string 事件响应消息对应的code。通过事件回调下发,仅可使用一次。 | ||
| 342 | msgid 否 string 消息ID。如果请求参数指定了msgid,则原样返回,否则系统自动生成并返回。 | ||
| 343 | 不多于32字节 | ||
| 344 | 字符串取值范围(正则表达式):[0-9a-zA-Z_-]* | ||
| 345 | msgtype 是 string 消息类型。对不同的msgtype,有相应的结构描述,详见消息类型 | ||
| 346 | |||
| 347 | 「进入会话事件」响应消息: | ||
| 348 | 如果满足通过API下发欢迎语条件(条件为:用户在过去48小时里未收过欢迎语,且未向客服发过消息),则用户进入会话事件会额外返回一个welcome_code,开发者以此为凭据调用接口(填到该接口code参数),即可向客户发送客服欢迎语。 | ||
| 349 | |||
| 350 | =head3 权限说明 | ||
| 351 | |||
| 352 | 调用的应用需要满足如下的权限 | ||
| 353 | |||
| 354 | 应用类型 权限要求 | ||
| 355 | 自建应用 配置到「 微信客服- 可调用接口的应用」中 | ||
| 356 | 第三方应用 具有“微信客服->管理账号、分配会话和收发消息”权限 | ||
| 357 | 代开发自建应用 具有“微信客服->管理账号、分配会话和收发消息”权限 | ||
| 358 | |||
| 359 | 注: 从2023年12月1日0点起,不再支持通过系统应用secret调用接口,存量企业暂不受影响 查看详情 | ||
| 360 | |||
| 361 | 只能通过API管理企业指定的客服账号。企业可在管理后台“微信客服-通过API管理微信客服账号”处设置对应的客服账号通过API来管理。 | ||
| 362 | 操作的客服账号对应的接待人员应在应用的可见范围内 | ||
| 363 | |||
| 364 | =head3 RETURN 返回结果 | ||
| 365 | |||
| 366 | { | ||
| 367 | "errcode": 0, | ||
| 368 | "errmsg": "ok", | ||
| 369 | "msgid": "MSG_ID" | ||
| 370 | } | ||
| 371 | |||
| 372 | =head4 RETURN 参数说明 | ||
| 373 | |||
| 374 | 参数 类型 说明 | ||
| 375 | errcode int32 返回码 | ||
| 376 | errmsg string 错误码描述 | ||
| 377 | msgid string 消息ID | ||
| 378 | |||
| 379 | =cut | ||
| 380 | |||
| 381 | sub send_msg_on_event { | ||
| 382 | if ( @_ && $_[0] && ref $_[1] eq 'HASH' ) { | ||
| 383 | my $access_token = $_[0]; | ||
| 384 | my $json = $_[1]; | ||
| 385 | my $ua = LWP::UserAgent->new; | ||
| 386 | $ua->timeout(30); | ||
| 387 | $ua->env_proxy; | ||
| 388 | |||
| 389 | my $response = $ua->post("https://qyapi.weixin.qq.com/cgi-bin/kf/send_msg_on_event?access_token=$access_token",Content => to_json($json,{allow_nonref=>1}),Content_type =>'application/json'); | ||
| 390 | if ($response->is_success) { | ||
| 391 | return from_json($response->decoded_content,{utf8 => 1, allow_nonref => 1}); | ||
| 392 | } | ||
| 393 | |||
| 394 | } | ||
| 395 | return 0; | ||
| 396 | } | ||
| 397 | |||
| 398 | =head2 get_corp_statistic(access_token, hash); | ||
| 399 | |||
| 400 | 获取「客户数据统计」企业汇总数据 | ||
| 401 | 最后更新:2023/11/30 | ||
| 402 | |||
| 403 | =head2 SYNOPSIS | ||
| 404 | |||
| 405 | L<https://developer.work.weixin.qq.com/document/path/95489> | ||
| 406 | |||
| 407 | =head3 请求说明: | ||
| 408 | |||
| 409 | 通过此接口,可以获取咨询会话数、咨询客户数等企业汇总统计数据 | ||
| 410 | |||
| 411 | =head4 请求包结构体为: | ||
| 412 | |||
| 413 | { | ||
| 414 | "open_kfid": "OPEN_KFID", | ||
| 415 | "start_time": 1645545600, | ||
| 416 | "end_time": 1645632000 | ||
| 417 | } | ||
| 418 | |||
| 419 | =head4 参数说明: | ||
| 420 | |||
| 421 | 参数 必须 类型 说明 | ||
| 422 | access_token 是 string 调用接口凭证 | ||
| 423 | open_kfid 是 string 客服账号ID | ||
| 424 | start_time 是 uint32 起始日期的时间戳,填这一天的0时0分0秒(否则系统自动处理为当天的0分0秒)。取值范围:昨天至前180天 | ||
| 425 | end_time 是 uint32 结束日期的时间戳,填这一天的0时0分0秒(否则系统自动处理为当天的0分0秒)。取值范围:昨天至前180天 | ||
| 426 | |||
| 427 | 查询时间区间[start_time, end_time]为闭区间,最大查询跨度为31天,用户最多可获取最近180天内的数据。当天的数据需要等到第二天才能获取,建议在第二天早上六点以后再调用此接口获取前一天的数据 | ||
| 428 | 当传入的时间不为0点时,会向下取整,如传入1554296400(Wed Apr 3 21:00:00 CST 2019)会被自动转换为1554220800(Wed Apr 3 00:00:00 CST 2019); | ||
| 429 | 开启API或授权第三方应用管理会话,没有2022年3月11日以前的统计数据 | ||
| 430 | |||
| 431 | =head3 权限说明 | ||
| 432 | |||
| 433 | 调用的应用需要满足如下的权限 | ||
| 434 | |||
| 435 | 应用类型 权限要求 | ||
| 436 | 自建应用 配置到「 微信客服- 可调用接口的应用」中 | ||
| 437 | 第三方应用 具有“微信客服->服务工具->获取客服数据统计”权限 | ||
| 438 | 代开发自建应用 具有“微信客服->服务工具->获取客服数据统计”权限 | ||
| 439 | |||
| 440 | 注: 从2023年12月1日0点起,不再支持通过系统应用secret调用接口,存量企业暂不受影响 查看详情 | ||
| 441 | |||
| 442 | 操作的客服账号对应的接待人员应在应用的可见范围内 | ||
| 443 | |||
| 444 | =head3 RETURN 返回结果 | ||
| 445 | |||
| 446 | { | ||
| 447 | "errcode": 0, | ||
| 448 | "errmsg": "ok", | ||
| 449 | "statistic_list" : [ | ||
| 450 | { | ||
| 451 | "stat_time" : 1645545600, | ||
| 452 | "statistic" : { | ||
| 453 | "session_cnt" : 2, | ||
| 454 | "customer_cnt" : 1, | ||
| 455 | "customer_msg_cnt" : 6, | ||
| 456 | "upgrade_service_customer_cnt" : 0, | ||
| 457 | "ai_session_reply_cnt" : 1, | ||
| 458 | "ai_transfer_rate" : 1, | ||
| 459 | "ai_knowledge_hit_rate" : 0, | ||
| 460 | "msg_rejected_customer_cnt" : 1 | ||
| 461 | }, | ||
| 462 | }, | ||
| 463 | { | ||
| 464 | "stat_time" : 1645632000, | ||
| 465 | "statistic" : { | ||
| 466 | ... | ||
| 467 | } | ||
| 468 | } | ||
| 469 | ] | ||
| 470 | } | ||
| 471 | |||
| 472 | =head4 RETURN 参数说明 | ||
| 473 | |||
| 474 | 参数 类型 说明 | ||
| 475 | errcode int32 返回码 | ||
| 476 | errmsg string 错误码描述 | ||
| 477 | statistic_list obj 统计数据列表 | ||
| 478 | statistic_list.stat_time uint32 数据统计日期,为当日0点的时间戳 | ||
| 479 | statistic_list.statistic obj 一天的统计数据。若当天未产生任何下列统计数据或统计数据还未计算完成则不会返回此项 | ||
| 480 | statistic_list.statistic.session_cnt uint64 咨询会话数。客户发过消息并分配给接待人员或智能助手的客服会话数,转接不会产生新的会话 | ||
| 481 | statistic_list.statistic.customer_cnt uint64 咨询客户数。在会话中发送过消息的客户数量,若客户多次咨询只计算一个客户 | ||
| 482 | statistic_list.statistic.customer_msg_cnt uint64 咨询消息总数。客户在会话中发送的消息的数量 | ||
| 483 | statistic_list.statistic.upgrade_service_customer_cnt uint64 升级服务客户数。通过「升级服务」功能成功添加专员或加入客户群的客户数,若同一个客户添加多个专员或客户群,只计算一个客户。在2022年3月10日以后才会有对应统计数据 | ||
| 484 | statistic_list.statistic.ai_session_reply_cnt uint64 智能回复会话数。客户发过消息并分配给智能助手的咨询会话数。通过API发消息或者开启智能回复功能会将客户分配给智能助手 | ||
| 485 | statistic_list.statistic.ai_transfer_rate float 转人工率。一个自然日内,客户给智能助手发消息的会话中,转人工的会话的占比。 | ||
| 486 | statistic_list.statistic.ai_knowledge_hit_rate float 知识命中率。一个自然日内,客户给智能助手发送的消息中,命中知识库的占比。只有在开启了智能回复原生功能并配置了知识库的情况下,才会产生该项统计数据。当api托管了会话分配,智能回复原生功能失效。若不返回,代表没有向配置知识库的智能接待助手发送消息,该项无法计算 | ||
| 487 | statistic_list.statistic.msg_rejected_customer_cnt uint64 被拒收消息的客户数。被接待人员设置了“不再接收消息”的客户数 | ||
| 488 | |||
| 489 | =cut | ||
| 490 | |||
| 491 | sub get_corp_statistic { | ||
| 492 | if ( @_ && $_[0] && ref $_[1] eq 'HASH' ) { | ||
| 493 | my $access_token = $_[0]; | ||
| 494 | my $json = $_[1]; | ||
| 495 | my $ua = LWP::UserAgent->new; | ||
| 496 | $ua->timeout(30); | ||
| 497 | $ua->env_proxy; | ||
| 498 | |||
| 499 | my $response = $ua->post("https://qyapi.weixin.qq.com/cgi-bin/kf/get_corp_statistic?access_token=$access_token",Content => to_json($json,{allow_nonref=>1}),Content_type =>'application/json'); | ||
| 500 | if ($response->is_success) { | ||
| 501 | return from_json($response->decoded_content,{utf8 => 1, allow_nonref => 1}); | ||
| 502 | } | ||
| 503 | |||
| 504 | } | ||
| 505 | return 0; | ||
| 506 | } | ||
| 507 | |||
| 508 | =head2 get_servicer_statistic(access_token, hash); | ||
| 509 | |||
| 510 | 获取「客户数据统计」接待人员明细数据 | ||
| 511 | 最后更新:2023/11/30 | ||
| 512 | |||
| 513 | =head2 SYNOPSIS | ||
| 514 | |||
| 515 | L<https://developer.work.weixin.qq.com/document/path/95490> | ||
| 516 | |||
| 517 | =head3 请求说明: | ||
| 518 | |||
| 519 | 通过此接口,可获取接入人工会话数、咨询会话数等与接待人员相关的统计信息 | ||
| 520 | |||
| 521 | =head4 请求包结构体为: | ||
| 522 | |||
| 523 | { | ||
| 524 | "open_kfid": "OPEN_KFID", | ||
| 525 | "servicer_userid":"zhangsan", | ||
| 526 | "start_time": 1645545600, | ||
| 527 | "end_time": 1645632000 | ||
| 528 | } | ||
| 529 | |||
| 530 | =head4 参数说明: | ||
| 531 | |||
| 532 | 参数 必须 类型 说明 | ||
| 533 | access_token 是 string 调用接口凭证 | ||
| 534 | open_kfid 是 string 客服账号ID | ||
| 535 | servicer_userid 否 string 接待人员的userid。第三方应用为密文userid,即open_userid | ||
| 536 | start_time 是 uint32 起始日期的时间戳,填当天的0时0分0秒(否则系统自动处理为当天的0分0秒)。取值范围:昨天至前180天 | ||
| 537 | end_time 是 uint32 结束日期的时间戳,填当天的0时0分0秒(否则系统自动处理为当天的0分0秒)。取值范围:昨天至前180天 | ||
| 538 | |||
| 539 | servicer_userid为非必填参数: | ||
| 540 | 1. 不指定servicer_userid,返回客服账号维度汇总数据; | ||
| 541 | 2. 指定servicer_userid,返回该接待人员在此客服账号下的数据。 | ||
| 542 | 查询时间区间[start_time, end_time]为闭区间,最大查询跨度为31天,用户最多可获取最近180天内的数据。当天的数据需要等到第二天才能获取,建议在第二天早上六点以后再调用此接口获取前一天的数据 | ||
| 543 | 当传入的时间不为0点时,会向下取整,如传入1554296400(Wed Apr 3 21:00:00 CST 2019)会被自动转换为1554220800(Wed Apr 3 00:00:00 CST 2019); | ||
| 544 | 开启API或授权第三方应用管理会话,没有2022年3月11日以前的统计数据 | ||
| 545 | |||
| 546 | =head3 权限说明 | ||
| 547 | |||
| 548 | 调用的应用需要满足如下的权限 | ||
| 549 | |||
| 550 | 应用类型 权限要求 | ||
| 551 | 自建应用 配置到「 微信客服- 可调用接口的应用」中 | ||
| 552 | 第三方应用 具有“微信客服->服务工具->获取客服数据统计”权限 | ||
| 553 | 代开发自建应用 具有“微信客服->服务工具->获取客服数据统计”权限 | ||
| 554 | |||
| 555 | 注: 从2023年12月1日0点起,不再支持通过系统应用secret调用接口,存量企业暂不受影响 查看详情 | ||
| 556 | |||
| 557 | 操作的客服账号对应的接待人员应在应用的可见范围内 | ||
| 558 | |||
| 559 | =head3 RETURN 返回结果 | ||
| 560 | |||
| 561 | { | ||
| 562 | "errcode": 0, | ||
| 563 | "errmsg": "ok", | ||
| 564 | "statistic_list" : [ | ||
| 565 | { | ||
| 566 | "stat_time" : 1645545600, | ||
| 567 | "statistic" : { | ||
| 568 | "session_cnt" : 1, | ||
| 569 | "customer_cnt" : 1, | ||
| 570 | "customer_msg_cnt" : 1, | ||
| 571 | "reply_rate" : 1, | ||
| 572 | "first_reply_average_sec" : 17, | ||
| 573 | "satisfaction_investgate_cnt" : 1, | ||
| 574 | "satisfaction_participation_rate" : 1, | ||
| 575 | "satisfied_rate" : 1, | ||
| 576 | "middling_rate" : 0, | ||
| 577 | "dissatisfied_rate" : 0, | ||
| 578 | "upgrade_service_customer_cnt" : 0, | ||
| 579 | "upgrade_service_member_invite_cnt" : 0, | ||
| 580 | "upgrade_service_member_customer_cnt" : 0, | ||
| 581 | "upgrade_service_groupchat_invite_cnt" : 0, | ||
| 582 | "upgrade_service_groupchat_customer_cnt" : 0, | ||
| 583 | "msg_rejected_customer_cnt" : 1 | ||
| 584 | } | ||
| 585 | }, | ||
| 586 | { | ||
| 587 | "stat_time" : 1645632000, | ||
| 588 | "statistic" : { | ||
| 589 | ... | ||
| 590 | } | ||
| 591 | } | ||
| 592 | ] | ||
| 593 | } | ||
| 594 | |||
| 595 | =head4 RETURN 参数说明 | ||
| 596 | |||
| 597 | 参数 类型 说明 | ||
| 598 | errcode int32 返回码 | ||
| 599 | errmsg string 错误码描述 | ||
| 600 | statistic_list obj 统计数据列表 | ||
| 601 | statistic_list.stat_time uint32 数据统计日期,为当日0点的时间戳 | ||
| 602 | statistic_list.statistic obj 一天的统计数据。若当天未产生任何下列统计数据或统计数据还未计算完成则不会返回此项 | ||
| 603 | statistic_list.statistic.session_cnt uint64 接入人工会话数。客户发过消息并分配给接待人员的咨询会话数 | ||
| 604 | statistic_list.statistic.customer_cnt uint64 咨询客户数。在会话中发送过消息且接入了人工会话的客户数量,若客户多次咨询只计算一个客户 | ||
| 605 | statistic_list.statistic.customer_msg_cnt uint64 咨询消息总数。客户在会话中发送的消息的数量 | ||
| 606 | statistic_list.statistic.reply_rate float 人工回复率。一个自然日内,客户给接待人员发消息的会话中,接待人员回复了的会话的占比。若数据项不返回,代表没有给接待人员发送消息的客户,此项无法计算。 | ||
| 607 | statistic_list.statistic.first_reply_average_sec float 平均首次响应时长,单位:秒。一个自然日内,客户给接待人员发送的第一条消息至接待人员回复之间的时长,为首次响应时长。所有的首次回复总时长/已回复的咨询会话数,即为平均首次响应时长 。若数据项不返回,代表没有给接待人员发送消息的客户,此项无法计算 | ||
| 608 | statistic_list.statistic.satisfaction_investgate_cnt uint64 满意度评价发送数。当api托管了会话分配,满意度原生功能失效,满意度评价发送数为0 | ||
| 609 | statistic_list.statistic.satisfaction_participation_rate float 满意度参评率 。当api托管了会话分配,满意度原生功能失效。若数据项不返回,代表没有发送满意度评价,此项无法计算 | ||
| 610 | statistic_list.statistic.satisfied_rate float “满意”评价占比 。在客户参评的满意度评价中,评价是“满意”的占比。当api托管了会话分配,满意度原生功能失效。若数据项不返回,代表没有客户参评的满意度评价,此项无法计算 | ||
| 611 | statistic_list.statistic.middling_rate float “一般”评价占比 。在客户参评的满意度评价中,评价是“一般”的占比。当api托管了会话分配,满意度原生功能失效。若数据项不返回,代表没有客户参评的满意度评价,此项无法计算 | ||
| 612 | statistic_list.statistic.dissatisfied_rate float “不满意”评价占比。在客户参评的满意度评价中,评价是“不满意”的占比。当api托管了会话分配,满意度原生功能失效。若数据项不返回,代表没有客户参评的满意度评价,此项无法计算 | ||
| 613 | statistic_list.statistic.upgrade_service_customer_cnt uint64 升级服务客户数。通过「升级服务」功能成功添加专员或加入客户群的客户数,若同一个客户添加多个专员或客户群,只计算一个客户。在2022年3月10日以后才会有对应统计数据 | ||
| 614 | statistic_list.statistic.upgrade_service_member_invite_cnt uint64 专员服务邀请数。接待人员通过「升级服务-专员服务」向客户发送服务专员名片的次数。在2022年3月10日以后才会有对应统计数据 | ||
| 615 | statistic_list.statistic.upgrade_service_member_customer_cnt uint64 添加专员的客户数 。客户成功添加专员为好友的数量,若同一个客户添加多个专员,则计算多个客户数。在2022年3月10日以后才会有对应统计数据 | ||
| 616 | statistic_list.statistic.upgrade_service_groupchat_invite_cnt uint64 客户群服务邀请数。接待人员通过「升级服务-客户群服务」向客户发送客户群二维码的次数。在2022年3月10日以后才会有对应统计数据 | ||
| 617 | statistic_list.statistic.upgrade_service_groupchat_customer_cnt uint64 加入客户群的客户数。客户成功加入客户群的数量,若同一个客户加多个客户群,则计算多个客户数。在2022年3月10日以后才会有对应统计数据 | ||
| 618 | statistic_list.statistic.msg_rejected_customer_cnt uint64 被拒收消息的客户数。被接待人员设置了“不再接收消息”的客户数 | ||
| 619 | |||
| 620 | =cut | ||
| 621 | |||
| 622 | sub get_servicer_statistic { | ||
| 623 | if ( @_ && $_[0] && ref $_[1] eq 'HASH' ) { | ||
| 624 | my $access_token = $_[0]; | ||
| 625 | my $json = $_[1]; | ||
| 626 | my $ua = LWP::UserAgent->new; | ||
| 627 | $ua->timeout(30); | ||
| 628 | $ua->env_proxy; | ||
| 629 | |||
| 630 | my $response = $ua->post("https://qyapi.weixin.qq.com/cgi-bin/kf/get_servicer_statistic?access_token=$access_token",Content => to_json($json,{allow_nonref=>1}),Content_type =>'application/json'); | ||
| 631 | if ($response->is_success) { | ||
| 632 | return from_json($response->decoded_content,{utf8 => 1, allow_nonref => 1}); | ||
| 633 | } | ||
| 634 | |||
| 635 | } | ||
| 636 | return 0; | ||
| 637 | } | ||
| 638 | |||
| 639 | 1; | ||
| 640 | __END__ |