Webqq/Plugin/FmPush.pm untested

filter: Statementstmt 0.0% 0 / 81 Branchbran 0.0% 0 / 34 Conditioncond 0.0% 0 / 27 MC/DCmcdc 0.0% 0 / 15 Subroutinesub 0.0% 0 / 3 Podpod 0.0% 0 / 1 totaltotal 0.0% 0 / 161 CC 18 SCAR 57.3
CC
18
Cov
0%
call
342.0
BEGIN
2.0
BEGIN
2.0
CRAP 308.0
1 package Mojo::Webqq::Plugin::FmPush;
2 use Mojo::Util qw(md5_sum);
3  
4  
5 our $AUTHOR = 'heipidage';
6 our $SITE = 'http://www.coolapk.com/apk/com.swjtu.gcmformojo';
7 our $DESC = '接收消息通过魅族提供的推送接口发送到android手机';
8 our $PRIORITY = 97;
9 use List::Util qw(first);
10 sub call {
11 my $client = shift;
12 my $data = shift;
13 $client->load("UploadQRcode") if !$client->is_load_plugin('UploadQRcode');
14 my $api_url = $data->{api_url} // 'https://api-push.meizu.com/garcia/api/server/push/unvarnished/pushByPushId';
15 my $api_key = 'eb034b0b4f42414baedaa04ddc7e6981';
16 my $app_id = '110370';
17 my $registration_ids = $data->{registration_ids} // [];
18 if(ref $registration_ids ne 'ARRAY' or @{$registration_ids} == 0){
19 $client->die("[".__PACKAGE__."]registration_ids无效");
20 }
21 my $registration_id = $registration_ids->[0];
22  
23 $client->on(receive_message=>sub{
24 my($client,$msg) = @_;
25 my $type = 'Mojo-Webqq';
26 my $title;
27 my $message;
28 my $msgId;
29 my $senderType;
30 my $isAt = 0;
31
32 if($msg->is_at) {
33 $isAt=1;
34 }
35 if($msg->type eq 'friend_message'){
36 return if $data->{is_ban_official} and $msg->sender->category eq '公众号';
37 $msgId = $msg->sender->id;
38 $title = $msg->sender->displayname;
39 $message = $msg->content;
40 $senderType = '1';
41 }
42 elsif($msg->type eq 'group_message'){
43 if(!$isAt) {
44 return if ref $data->{ban_group} eq "ARRAY" and @{$data->{ban_group}} and first {$_=~/^\d+$/?$msg->group->uid eq $_:$msg->group->displayname eq $_} @{$data->{ban_group}};
45 return if ref $data->{allow_group} eq "ARRAY" and @{$data->{allow_group}} and !first {$_=~/^\d+$/?$msg->group->uid eq $_:$msg->group->displayname eq $_} @{$data->{allow_group}};
46 }
47 $msgId = $msg->group->id;
48 $title = $msg->group->displayname;
49 $message = $msg->sender->displayname . ": " . $msg->content;
50 $senderType = '2';
51 }
52 return if !$title or !$message;
53
54 my $messageJson = '{"content":"{\"isAt\":\"'.$isAt.'\",\"type\":\"'.$type.'\",\"title\":\"'.$title.'\",\"message\":\"'.$message.'\",\"msgId\":\"'.$msgId.'\",\"senderType\":\"'.$senderType.'\"}"}';
55 print "appId=".$app_id."messageJson=".$messageJson."pushIds=".$registration_id.$api_key;
56 my $sign = md5_sum("appId=".$app_id."messageJson=".$messageJson."pushIds=".$registration_id.$api_key);
57
58 $client->http_post($api_url,
59 {ua_debug=>1,ua_debug_req_body=>1,ua_debug_res_body=>1,json=>1},
60 form=>{
61 appId => $app_id,
62 pushIds => $registration_id,
63 messageJson=>$messageJson,
64 sign => $sign,
65 },
66 sub{
67 #"{"multicast_id":9016211065189210367,"success":1,"failure":0,"canonical_ids":0,"results":[{"message_id":"0:1484103730761325%9b9e6c13f9fd7ecd"}]}"
68 my $json = shift;
69 if(not defined $json){
70 $client->debug("[".__PACKAGE__."]魅族消息推送失败: 返回结果异常");
71 return;
72 }
73 else{
74 $client->debug("[".__PACKAGE__."]魅族消息推送完成:$json->{multicast_id}/$json->{success}/$json->{failure}");
75 }
76 }
77 );
78 });
79  
80 $client->on(all_event => sub{
81 my($client,$event,@args) =@_;
82 my $type = 'Mojo-Sys';
83 my $message;
84 my $msgId = 1;
85 my $title;
86 if($event eq 'login'){
87 $message = "登录成功";
88 $title = "登录事件";
89 }
90 elsif($event eq 'input_qrcode'){
91 $message = $client->qrcode_upload_url // '获取二维码url失败';
92 $title = "扫描二维码事件";
93 }
94 elsif($event eq 'stop'){
95 $message = "Mojo-Webqq已停止";
96 $title = "停止事件";
97 }
98 else{return}
99
100 my $messageJson = '{"content":"{\"type\":\"'.$type.'\",\"title\":\"'.$title.'\",\"message\":\"'.$message.'\",\"msgId\":\"'.$msgId.'\"}"}';
101
102 $client->http_post($api_url,
103 {
104 blocking=>1,
105 json=>1,
106 ua_connect_timeout=>5,
107 ua_request_timeout=>5,
108 ua_inactivity_timeout=>5,
109 ua_retry_times=>1
110 },
111 form=>{
112
113 appId => $app_id,
114 pushIds => $registration_id,
115 messageJson=>$messageJson,
116 sign => md5_sum("appId=".$app_id."messageJson=".$messageJson."pushIds=".$registration_id.$api_key),
117
118 },
119 sub{
120 my $json = shift;
121 if(not defined $json){
122 $client->debug("[".__PACKAGE__."]魅族消息推送失败: 返回结果异常");
123 return;
124 }
125 else{
126 $client->debug("[".__PACKAGE__."]魅族消息推送完成:$json->{multicast_id}/$json->{success}/$json->{failure}");
127 }
128 }
129 );
130 });
131 }
132 1;