| 1 | package QQ::weixin::work::message; | ||
| 2 | |||
| 3 | =encoding utf8 | ||
| 4 | |||
| 5 | =head1 Name | ||
| 6 | |||
| 7 | QQ::weixin::work::message | ||
| 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/ send update_template_card recall update_taskcard get_statistics revoke /; | ||
| 24 | |||
| 25 | =head1 FUNCTION | ||
| 26 | |||
| 27 | =head2 send(access_token, hash); | ||
| 28 | |||
| 29 | 发送应用消息 | ||
| 30 | 最后更新:2024/01/10 | ||
| 31 | |||
| 32 | =head2 SYNOPSIS | ||
| 33 | |||
| 34 | L<https://developer.work.weixin.qq.com/document/path/90236> | ||
| 35 | |||
| 36 | =head3 请求说明: | ||
| 37 | 应用支持推送文本、图片、视频、文件、图文等类型。 | ||
| 38 | |||
| 39 | =head4 请求包结构体为: | ||
| 40 | |||
| 41 | =head4 参数说明: | ||
| 42 | |||
| 43 | 参数 是否必须 说明 | ||
| 44 | access_token 是 调用接口凭证 | ||
| 45 | |||
| 46 | =head3 权限说明 | ||
| 47 | |||
| 48 | 如果部分接收人无权限或不存在,发送仍然执行,但会返回无效的部分(即invaliduser或invalidparty或invalidtag),常见的原因是接收人不在应用的可见范围内。 | ||
| 49 | |||
| 50 | 如果全部接收人无权限或不存在,则本次调用返回失败,errcode为81013。 | ||
| 51 | |||
| 52 | 返回包中的userid,不区分大小写,统一转为小写 | ||
| 53 | |||
| 54 | =head3 RETURN 返回结果 | ||
| 55 | |||
| 56 | { | ||
| 57 | "errcode" : 0, | ||
| 58 | "errmsg" : "ok", | ||
| 59 | "invaliduser" : "userid1|userid2", | ||
| 60 | "invalidparty" : "partyid1|partyid2", | ||
| 61 | "invalidtag": "tagid1|tagid2", | ||
| 62 | "unlicenseduser" : "userid3|userid4", | ||
| 63 | "msgid": "xxxx", | ||
| 64 | "response_code": "xyzxyz" | ||
| 65 | } | ||
| 66 | |||
| 67 | =head4 RETURN 参数说明 | ||
| 68 | |||
| 69 | 参数 说明 | ||
| 70 | errcode 返回码 | ||
| 71 | errmsg 对返回码的文本描述内容 | ||
| 72 | invaliduser 不合法的userid,不区分大小写,统一转为小写 | ||
| 73 | invalidparty 不合法的partyid | ||
| 74 | invalidtag 不合法的标签id | ||
| 75 | unlicenseduser 没有基础接口许可(包含已过期)的userid | ||
| 76 | msgid 消息id,用于撤回应用消息 | ||
| 77 | response_code 仅消息类型为“按钮交互型”,“投票选择型”和“多项选择型”的模板卡片消息返回,应用可使用response_code调用更新模版卡片消息接口,72小时内有效,且只能使用一次 | ||
| 78 | |||
| 79 | =cut | ||
| 80 | |||
| 81 | sub send { | ||
| 82 | if ( @_ && $_[0] && ref $_[1] eq 'HASH' ) { | ||
| 83 | my $access_token = $_[0]; | ||
| 84 | my $json = $_[1]; | ||
| 85 | my $ua = LWP::UserAgent->new; | ||
| 86 | $ua->timeout(30); | ||
| 87 | $ua->env_proxy; | ||
| 88 | |||
| 89 | my $response = $ua->post("https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=$access_token",Content => to_json($json,{allow_nonref=>1}),Content_type =>'application/json'); | ||
| 90 | if ($response->is_success) { | ||
| 91 | return from_json($response->decoded_content,{utf8 => 1, allow_nonref => 1}); | ||
| 92 | } | ||
| 93 | |||
| 94 | } | ||
| 95 | return 0; | ||
| 96 | } | ||
| 97 | |||
| 98 | =head2 update_template_card(access_token, hash); | ||
| 99 | |||
| 100 | 更新模版卡片消息 | ||
| 101 | 最后更新:2023/09/21 | ||
| 102 | |||
| 103 | =head2 SYNOPSIS | ||
| 104 | |||
| 105 | L<https://developer.work.weixin.qq.com/document/path/94888> | ||
| 106 | |||
| 107 | =head3 请求说明: | ||
| 108 | |||
| 109 | =head3 更新按钮为不可点击状态 | ||
| 110 | |||
| 111 | 可回调的卡片可以将按钮更新为不可点击状态,并且自定义文案 | ||
| 112 | |||
| 113 | =head4 请求包结构体为: | ||
| 114 | |||
| 115 | { | ||
| 116 | "userids" : ["userid1","userid2"], | ||
| 117 | "partyids" : [2,3], | ||
| 118 | "tagids" : [44,55], | ||
| 119 | "atall" : 0, | ||
| 120 | "agentid" : 1, | ||
| 121 | "response_code": "response_code", | ||
| 122 | "button":{ | ||
| 123 | "replace_name": "replace_name" | ||
| 124 | } | ||
| 125 | } | ||
| 126 | |||
| 127 | =head4 参数说明: | ||
| 128 | |||
| 129 | 参数 必须 说明 | ||
| 130 | access_token 是 调用接口凭证 | ||
| 131 | userids 否 企业的成员ID列表(最多支持1000个) | ||
| 132 | partyids 否 企业的部门ID列表(最多支持100个) | ||
| 133 | tagids 否 企业的标签ID列表(最多支持100个) | ||
| 134 | atall 否 更新整个任务接收人员 | ||
| 135 | agentid 是 应用的agentid | ||
| 136 | response_code 是 更新卡片所需要消费的code,可通过发消息接口和回调接口返回值获取,一个code只能调用一次该接口,且只能在24小时内调用 | ||
| 137 | replace_name 是 需要更新的按钮的文案 | ||
| 138 | |||
| 139 | =head3 更新为新的卡片 | ||
| 140 | |||
| 141 | 可回调的卡片可以更新成任何一种模板卡片 | ||
| 142 | |||
| 143 | =head4 请求包结构体为: | ||
| 144 | |||
| 145 | =head4 参数说明: | ||
| 146 | |||
| 147 | =head3 权限说明 | ||
| 148 | |||
| 149 | =head3 RETURN 返回结果 | ||
| 150 | |||
| 151 | { | ||
| 152 | "errcode" : 0, | ||
| 153 | "errmsg" : "ok", | ||
| 154 | "invaliduser" : ["userid1","userid2"], // 不区分大小写,返回的列表都统一转为小写 | ||
| 155 | } | ||
| 156 | |||
| 157 | =head4 RETURN 参数说明 | ||
| 158 | |||
| 159 | 参数 说明 | ||
| 160 | errcode 返回码 | ||
| 161 | errmsg 对返回码的文本描述内容 | ||
| 162 | |||
| 163 | 如果部分指定的用户无权限或不存在,更新仍然执行,但会返回无效的部分(即invaliduser),常见的原因是用户不在应用的可见范围内或者不在消息的接收范围内。 | ||
| 164 | |||
| 165 | =cut | ||
| 166 | |||
| 167 | sub update_template_card { | ||
| 168 | if ( @_ && $_[0] && ref $_[1] eq 'HASH' ) { | ||
| 169 | my $access_token = $_[0]; | ||
| 170 | my $json = $_[1]; | ||
| 171 | my $ua = LWP::UserAgent->new; | ||
| 172 | $ua->timeout(30); | ||
| 173 | $ua->env_proxy; | ||
| 174 | |||
| 175 | my $response = $ua->post("https://qyapi.weixin.qq.com/cgi-bin/message/update_template_card?access_token=$access_token",Content => to_json($json,{allow_nonref=>1}),Content_type =>'application/json'); | ||
| 176 | if ($response->is_success) { | ||
| 177 | return from_json($response->decoded_content,{utf8 => 1, allow_nonref => 1}); | ||
| 178 | } | ||
| 179 | |||
| 180 | } | ||
| 181 | return 0; | ||
| 182 | } | ||
| 183 | |||
| 184 | =head2 recall(access_token, hash); | ||
| 185 | |||
| 186 | 撤回应用消息 | ||
| 187 | 最后更新:2021/08/11 | ||
| 188 | |||
| 189 | =head2 SYNOPSIS | ||
| 190 | |||
| 191 | L<https://developer.work.weixin.qq.com/document/path/94867> | ||
| 192 | |||
| 193 | =head3 请求说明: | ||
| 194 | |||
| 195 | 本接口可以撤回24小时内通过发送应用消息接口推送的消息,仅可撤回企业微信端的数据,微信插件端的数据不支持撤回。 | ||
| 196 | |||
| 197 | =head4 请求包结构体为: | ||
| 198 | |||
| 199 | { | ||
| 200 | "msgid": "vcT8gGc-7dFb4bxT35ONjBDz901sLlXPZw1DAMC_Gc26qRpK-AK5sTJkkb0128t" | ||
| 201 | } | ||
| 202 | |||
| 203 | =head4 参数说明: | ||
| 204 | |||
| 205 | 参数 必须 说明 | ||
| 206 | access_token 是 调用接口凭证。获取方法查看“获取access_token” | ||
| 207 | msgid 是 消息ID。从应用发送消息接口处获得。 | ||
| 208 | |||
| 209 | |||
| 210 | =head3 权限说明 | ||
| 211 | |||
| 212 | =head3 RETURN 返回结果 | ||
| 213 | |||
| 214 | { | ||
| 215 | "errcode": 0, | ||
| 216 | "errmsg": "ok" | ||
| 217 | } | ||
| 218 | |||
| 219 | =head4 RETURN 参数说明 | ||
| 220 | |||
| 221 | 参数 说明 | ||
| 222 | errcode 返回码 | ||
| 223 | errmsg 对返回码的文本描述内容 | ||
| 224 | |||
| 225 | =cut | ||
| 226 | |||
| 227 | sub recall { | ||
| 228 | if ( @_ && $_[0] && ref $_[1] eq 'HASH' ) { | ||
| 229 | my $access_token = $_[0]; | ||
| 230 | my $json = $_[1]; | ||
| 231 | my $ua = LWP::UserAgent->new; | ||
| 232 | $ua->timeout(30); | ||
| 233 | $ua->env_proxy; | ||
| 234 | |||
| 235 | my $response = $ua->post("https://qyapi.weixin.qq.com/cgi-bin/message/recall?access_token=$access_token",Content => to_json($json,{allow_nonref=>1}),Content_type =>'application/json'); | ||
| 236 | if ($response->is_success) { | ||
| 237 | return from_json($response->decoded_content,{utf8 => 1, allow_nonref => 1}); | ||
| 238 | } | ||
| 239 | |||
| 240 | } | ||
| 241 | return 0; | ||
| 242 | } | ||
| 243 | |||
| 244 | =head2 update_taskcard(access_token, hash); | ||
| 245 | |||
| 246 | 更新任务卡片消息状态 | ||
| 247 | |||
| 248 | =head2 SYNOPSIS | ||
| 249 | |||
| 250 | L<https://work.weixin.qq.com/api/doc/90000/90135/91579> | ||
| 251 | |||
| 252 | =head3 请求说明: | ||
| 253 | |||
| 254 | =head4 请求包结构体为: | ||
| 255 | |||
| 256 | { | ||
| 257 | "userids" : ["userid1","userid2"], | ||
| 258 | "agentid" : 1, | ||
| 259 | "task_id": "taskid122", | ||
| 260 | "clicked_key": "btn_key123" | ||
| 261 | } | ||
| 262 | |||
| 263 | =head4 参数说明: | ||
| 264 | |||
| 265 | 参数 必须 说明 | ||
| 266 | access_token 是 调用接口凭证 | ||
| 267 | userids 是 企业的成员ID列表(消息接收者,最多支持1000个)。 | ||
| 268 | agentid 是 应用的agentid | ||
| 269 | task_id 是 发送任务卡片消息时指定的task_id | ||
| 270 | clicked_key 是 设置指定的按钮为选择状态,需要与发送消息时指定的btn:key一致 | ||
| 271 | |||
| 272 | =head3 权限说明 | ||
| 273 | |||
| 274 | 系统应用须拥有邮件群组的写管理权限。 | ||
| 275 | |||
| 276 | =head3 RETURN 返回结果 | ||
| 277 | |||
| 278 | { | ||
| 279 | "errcode": 0, | ||
| 280 | "errmsg": "ok", | ||
| 281 | "invaliduser" : ["userid1","userid2"], // 不区分大小写,返回的列表都统一转为小写 | ||
| 282 | } | ||
| 283 | |||
| 284 | =head4 RETURN 参数说明 | ||
| 285 | |||
| 286 | 参数 说明 | ||
| 287 | errcode 返回码 | ||
| 288 | errmsg 对返回码的文本描述内容 | ||
| 289 | |||
| 290 | 如果部分指定的用户无权限或不存在,更新仍然执行,但会返回无效的部分(即invaliduser),常见的原因是用户不在应用的可见范围内或者不在消息的接收范围内。 | ||
| 291 | |||
| 292 | =cut | ||
| 293 | |||
| 294 | sub update_taskcard { | ||
| 295 | if ( @_ && $_[0] && ref $_[1] eq 'HASH' ) { | ||
| 296 | my $access_token = $_[0]; | ||
| 297 | my $json = $_[1]; | ||
| 298 | my $ua = LWP::UserAgent->new; | ||
| 299 | $ua->timeout(30); | ||
| 300 | $ua->env_proxy; | ||
| 301 | |||
| 302 | my $response = $ua->post("https://qyapi.weixin.qq.com/cgi-bin/message/update_taskcard?access_token=$access_token",Content => to_json($json,{allow_nonref=>1}),Content_type =>'application/json'); | ||
| 303 | if ($response->is_success) { | ||
| 304 | return from_json($response->decoded_content,{utf8 => 1, allow_nonref => 1}); | ||
| 305 | } | ||
| 306 | |||
| 307 | } | ||
| 308 | return 0; | ||
| 309 | } | ||
| 310 | |||
| 311 | =head2 get_statistics(access_token, hash); | ||
| 312 | |||
| 313 | 查询应用消息发送统计 | ||
| 314 | |||
| 315 | =head2 SYNOPSIS | ||
| 316 | |||
| 317 | L<https://work.weixin.qq.com/api/doc/90000/90135/92369> | ||
| 318 | |||
| 319 | =head3 请求说明: | ||
| 320 | |||
| 321 | =head4 请求包结构体为: | ||
| 322 | |||
| 323 | { | ||
| 324 | "time_type": 0 | ||
| 325 | } | ||
| 326 | |||
| 327 | =head4 参数说明: | ||
| 328 | |||
| 329 | 参数 必须 说明 | ||
| 330 | access_token 是 调用接口凭证 | ||
| 331 | time_type 否 查询哪天的数据,0:当天;1:昨天。默认为0。 | ||
| 332 | |||
| 333 | =head3 权限说明 | ||
| 334 | |||
| 335 | 无 | ||
| 336 | |||
| 337 | =head3 RETURN 返回结果 | ||
| 338 | |||
| 339 | { | ||
| 340 | "errcode": 0, | ||
| 341 | "errmsg": "ok", | ||
| 342 | "statistics": [ | ||
| 343 | { | ||
| 344 | "agentid": 1000002, | ||
| 345 | "app_name": "应用1", | ||
| 346 | "count": 101 | ||
| 347 | }, | ||
| 348 | { | ||
| 349 | "agentid": 1000003, | ||
| 350 | "app_name": "应用2", | ||
| 351 | "count": 102 | ||
| 352 | } | ||
| 353 | ] | ||
| 354 | } | ||
| 355 | |||
| 356 | =head4 RETURN 参数说明 | ||
| 357 | |||
| 358 | 参数 说明 | ||
| 359 | errcode 返回码 | ||
| 360 | errmsg 对返回码的文本描述内容 | ||
| 361 | statistics.agentid 应用id | ||
| 362 | statistics.app_name 应用名 | ||
| 363 | statistics.count 发消息成功人次 | ||
| 364 | |||
| 365 | =cut | ||
| 366 | |||
| 367 | sub get_statistics { | ||
| 368 | if ( @_ && $_[0] && ref $_[1] eq 'HASH' ) { | ||
| 369 | my $access_token = $_[0]; | ||
| 370 | my $json = $_[1]; | ||
| 371 | my $ua = LWP::UserAgent->new; | ||
| 372 | $ua->timeout(30); | ||
| 373 | $ua->env_proxy; | ||
| 374 | |||
| 375 | my $response = $ua->post("https://qyapi.weixin.qq.com/cgi-bin/message/get_statistics?access_token=$access_token",Content => to_json($json,{allow_nonref=>1}),Content_type =>'application/json'); | ||
| 376 | if ($response->is_success) { | ||
| 377 | return from_json($response->decoded_content,{utf8 => 1, allow_nonref => 1}); | ||
| 378 | } | ||
| 379 | |||
| 380 | } | ||
| 381 | return 0; | ||
| 382 | } | ||
| 383 | |||
| 384 | =head2 revoke(access_token, hash); | ||
| 385 | |||
| 386 | 图文消息撤回 | ||
| 387 | |||
| 388 | =head2 SYNOPSIS | ||
| 389 | |||
| 390 | L<https://open.work.weixin.qq.com/api/doc/13568> | ||
| 391 | |||
| 392 | =head3 请求说明: | ||
| 393 | |||
| 394 | =head4 请求包结构体为: | ||
| 395 | |||
| 396 | { | ||
| 397 | "mpnews_url": "https://open.work.weixin.qq.com/wwopen/mpnews?mixuin=A_4ACQAABwD9eauwAAAUAA&mfid=WW0319-1tGqnAAABwBG0d1cPIxyBgup0HL23&idx=0&sn=70b8307095a856271494254f1eee99db" | ||
| 398 | } | ||
| 399 | |||
| 400 | =head4 参数说明: | ||
| 401 | |||
| 402 | 参数 必须 说明 | ||
| 403 | access_token 是 调用接口凭证 | ||
| 404 | mpnews_url 是 mpnews 的 地址 | ||
| 405 | |||
| 406 | |||
| 407 | =head3 权限说明 | ||
| 408 | |||
| 409 | access_token 对应的secret必须与发送此消息使用的secret一致,否则无权限撤回 | ||
| 410 | |||
| 411 | =head3 RETURN 返回结果 | ||
| 412 | |||
| 413 | { | ||
| 414 | "errcode": 0, | ||
| 415 | "errmsg": "ok" | ||
| 416 | } | ||
| 417 | |||
| 418 | =head4 RETURN 参数说明 | ||
| 419 | |||
| 420 | 参数 说明 | ||
| 421 | errcode 返回码 | ||
| 422 | errmsg 对返回码的文本描述内容 | ||
| 423 | |||
| 424 | =cut | ||
| 425 | |||
| 426 | sub revoke { | ||
| 427 | if ( @_ && $_[0] && ref $_[1] eq 'HASH' ) { | ||
| 428 | my $access_token = $_[0]; | ||
| 429 | my $json = $_[1]; | ||
| 430 | my $ua = LWP::UserAgent->new; | ||
| 431 | $ua->timeout(30); | ||
| 432 | $ua->env_proxy; | ||
| 433 | |||
| 434 | my $response = $ua->post("https://qyapi.weixin.qq.com/cgi-bin/message/revoke?access_token=$access_token",Content => to_json($json,{allow_nonref=>1}),Content_type =>'application/json'); | ||
| 435 | if ($response->is_success) { | ||
| 436 | return from_json($response->decoded_content,{utf8 => 1, allow_nonref => 1}); | ||
| 437 | } | ||
| 438 | |||
| 439 | } | ||
| 440 | return 0; | ||
| 441 | } | ||
| 442 | |||
| 443 | |||
| 444 | 1; | ||
| 445 | __END__ |