| 1 | package Mojo::Webqq::Plugin::HwPush; | ||
| 2 | |||
| 3 | our $AUTHOR = 'heipidage'; | ||
| 4 | our $SITE = 'http://www.coolapk.com/apk/com.swjtu.gcmformojo'; | ||
| 5 | our $DESC = '接收消息通过华为推送接口发送到android手机'; | ||
| 6 | our $PRIORITY = 97; | ||
| 7 | use List::Util qw(first); | ||
| 8 | sub call { | ||
| 9 | my $client = shift; | ||
| 10 | my $data = shift; | ||
| 11 | $client->load("UploadQRcode") if !$client->is_load_plugin('UploadQRcode'); | ||
| 12 | my $api_url = $data->{api_url} // 'https://api.vmall.com/rest.php'; | ||
| 13 | my $hwfile = $data->{hwfile} // 'hw_access_token_gcm.txt'; | ||
| 14 | my $access_token; | ||
| 15 | |||
| 16 | my $deviceToken = $data->{registration_ids} // []; | ||
| 17 | if(ref $deviceToken ne 'ARRAY' or @{$deviceToken} == 0){ | ||
| 18 | $client->die("[".__PACKAGE__."]registration_ids无效"); | ||
| 19 | } | ||
| 20 | |||
| 21 | if(!open(my $fh ,$hwfile)) { | ||
| 22 | my $getHwToken = $client->http_get('https://raw.githubusercontent.com/heipidage/HwPushForMojo/master/hw_access_token_gcm.txt'); | ||
| 23 | if($getHwToken) { | ||
| 24 | open(my $fhw, '>',$hwfile) or die "Could not open file '$hwfile' $!"; | ||
| 25 | print $fhw $getHwToken; | ||
| 26 | close($fhw); | ||
| 27 | $access_token = $getHwToken; | ||
| 28 | } | ||
| 29 | }else{ | ||
| 30 | my @lines = <$fh> ; | ||
| 31 | close($fh); | ||
| 32 | $access_token = $lines[0]; | ||
| 33 | } | ||
| 34 | |||
| 35 | |||
| 36 | $client->on(receive_message=>sub{ | ||
| 37 | my($client,$msg) = @_; | ||
| 38 | my $type = 'Mojo-Webqq'; | ||
| 39 | my $title; | ||
| 40 | my $message; | ||
| 41 | my $msgId; | ||
| 42 | my $senderType; | ||
| 43 | my $isAt = 0; | ||
| 44 | if($msg->is_at) { | ||
| 45 | $isAt=1; | ||
| 46 | } | ||
| 47 | if($msg->type eq 'friend_message'){ | ||
| 48 | $msgId = $msg->sender->id; | ||
| 49 | $title = $msg->sender->displayname; | ||
| 50 | $message = $msg->content; | ||
| 51 | $senderType = '1'; | ||
| 52 | } | ||
| 53 | elsif($msg->type eq 'group_message'){ | ||
| 54 | if(!$isAt) { | ||
| 55 | 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}}; | ||
| 56 | 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}}; | ||
| 57 | } | ||
| 58 | $msgId = $msg->group->id; | ||
| 59 | $title = $msg->group->displayname; | ||
| 60 | $message = $msg->sender->displayname . ": " . $msg->content; | ||
| 61 | $senderType = '2'; | ||
| 62 | } | ||
| 63 | elsif($msg->type eq 'discuss_message'){ | ||
| 64 | return if ref $data->{ban_discuss} eq "ARRAY" and @{$data->{ban_discuss}} and first {$_=~/^\d+$/?$msg->discuss->uid eq $_:$msg->discuss->displayname eq $_} @{$data->{ban_discuss}}; | ||
| 65 | return if ref $data->{allow_discuss} eq "ARRAY" and @{$data->{allow_discuss}} and !first {$_=~/^\d+$/?$msg->discuss->uid eq $_:$msg->discuss->displayname eq $_} @{$data->{allow_discuss}}; | ||
| 66 | $msgId= $msg->discuss->id; | ||
| 67 | $title = $msg->discuss->displayname; | ||
| 68 | $message = $msg->sender->displayname . ": " . $msg->content; | ||
| 69 | $senderType = '3'; | ||
| 70 | } | ||
| 71 | elsif($msg->type eq 'sess_message'){ | ||
| 72 | |||
| 73 | } | ||
| 74 | return if !$title or !$message; | ||
| 75 | |||
| 76 | |||
| 77 | |||
| 78 | my $result=$client->http_post($api_url, {json=>1}, | ||
| 79 | form=>{ | ||
| 80 | nsp_fmt => JSON, | ||
| 81 | deviceToken => $deviceToken, | ||
| 82 | push_type => 1, | ||
| 83 | access_token => $access_token, | ||
| 84 | priority => 1, | ||
| 85 | cacheMode => 0, | ||
| 86 | msgType => 1, | ||
| 87 | nsp_svc => 'openpush.message.single_send', | ||
| 88 | nsp_ts => time(), | ||
| 89 | message=>$client->to_json({isAt=>$isAt,type=>$type,title=>$title,message=>$message,msgId=>$msgId,senderType=>$senderType}), | ||
| 90 | }, | ||
| 91 | sub{ | ||
| 92 | my $json = shift; | ||
| 93 | if(not defined $json){ | ||
| 94 | $client->debug("[".__PACKAGE__."]华为消息推送失败: 返回结果异常"); | ||
| 95 | return; | ||
| 96 | } | ||
| 97 | else{ | ||
| 98 | $client->debug("[".__PACKAGE__."]华为消息推送状态:$json->{error}"); | ||
| 99 | } | ||
| 100 | } | ||
| 101 | ); | ||
| 102 | |||
| 103 | |||
| 104 | |||
| 105 | if($result->{error} eq 'invalid session' or $result->{error} eq 'session timeout') { | ||
| 106 | my $newtokenres = $client->http_get('https://raw.githubusercontent.com/heipidage/HwPushForMojo/master/hw_access_token_gcm.txt'); | ||
| 107 | if($newtokenres) { | ||
| 108 | open(my $fhw, '>',$hwfile) or die "Could not open file '$hwfile' $!"; | ||
| 109 | print $fhw $newtokenres; | ||
| 110 | close($fhw); | ||
| 111 | $access_token = $newtokenres; | ||
| 112 | $client->http_post($api_url, {json=>1}, | ||
| 113 | form=>{ | ||
| 114 | nsp_fmt => JSON, | ||
| 115 | deviceToken => $deviceToken, | ||
| 116 | push_type => 1, | ||
| 117 | access_token => $access_token, | ||
| 118 | priority => 1, | ||
| 119 | cacheMode => 0, | ||
| 120 | msgType => 1, | ||
| 121 | nsp_svc => 'openpush.message.single_send', | ||
| 122 | nsp_ts => time(), | ||
| 123 | message=>$client->to_json({isAt=>$isAt,type=>$type,title=>$title,message=>$message,msgId=>$msgId,senderType=>$senderType}), | ||
| 124 | }, | ||
| 125 | sub{ | ||
| 126 | my $new_json = shift; | ||
| 127 | if(not defined $new_json){ | ||
| 128 | $client->debug("[".__PACKAGE__."]华为消息推送失败: 返回结果异常"); | ||
| 129 | return; | ||
| 130 | } | ||
| 131 | else{ | ||
| 132 | $client->debug("[".__PACKAGE__."]华为消息推送状态:$new_json->{error}"); | ||
| 133 | } | ||
| 134 | } | ||
| 135 | ); | ||
| 136 | |||
| 137 | } else { | ||
| 138 | print "HwPush HTTP GET Github Token error message.\n"; | ||
| 139 | } | ||
| 140 | } | ||
| 141 | |||
| 142 | }); | ||
| 143 | |||
| 144 | |||
| 145 | $client->on(all_event => sub{ | ||
| 146 | my($client,$event,@args) =@_; | ||
| 147 | my $type = 'Mojo-Sys'; | ||
| 148 | my $message; | ||
| 149 | my $msgId = 1; | ||
| 150 | my $title; | ||
| 151 | if($event eq 'login'){ | ||
| 152 | $message = "登录成功"; | ||
| 153 | $title = "登录事件"; | ||
| 154 | } | ||
| 155 | elsif($event eq 'input_qrcode'){ | ||
| 156 | $message = $client->qrcode_upload_url // '获取二维码url失败'; | ||
| 157 | $title = "扫描二维码事件"; | ||
| 158 | } | ||
| 159 | elsif($event eq 'stop'){ | ||
| 160 | $message = "Mojo-Webqq已停止"; | ||
| 161 | $title = "停止事件"; | ||
| 162 | } | ||
| 163 | else{return} | ||
| 164 | |||
| 165 | |||
| 166 | $client->http_post($api_url, | ||
| 167 | { json=>1, | ||
| 168 | blocking=>1, | ||
| 169 | ua_connect_timeout=>5, | ||
| 170 | ua_request_timeout=>5, | ||
| 171 | ua_inactivity_timeout=>5, | ||
| 172 | ua_retry_times=>1 | ||
| 173 | }, | ||
| 174 | form=>{ | ||
| 175 | nsp_fmt => JSON, | ||
| 176 | deviceToken => $deviceToken, | ||
| 177 | push_type => 1, | ||
| 178 | access_token => $access_token, | ||
| 179 | priority => 1, | ||
| 180 | cacheMode => 0, | ||
| 181 | msgType => 1, | ||
| 182 | nsp_svc => 'openpush.message.single_send', | ||
| 183 | nsp_ts => time(), | ||
| 184 | message=>$client->to_json({type=>$type,title=>$title,message=>$message,msgId=>$msgId}), | ||
| 185 | }, | ||
| 186 | sub{ | ||
| 187 | my $json = shift; | ||
| 188 | if(not defined $json){ | ||
| 189 | $client->debug("[".__PACKAGE__."]华为消息推送失败: 返回结果异常"); | ||
| 190 | return; | ||
| 191 | } | ||
| 192 | else{ | ||
| 193 | $client->debug("[".__PACKAGE__."]华为消息推送状态:$json->{error}"); | ||
| 194 | } | ||
| 195 | } | ||
| 196 | ); | ||
| 197 | |||
| 198 | |||
| 199 | |||
| 200 | |||
| 201 | |||
| 202 | |||
| 203 | }); | ||
| 204 | } | ||
| 205 | 1; | ||
| 206 |