| 1 | package Mojo::Weixin::Plugin::MiPush; | ||
| 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.xmpush.xiaomi.com/v2/message/regid'; | ||
| 13 | my $api_key = 'hdtYlfMarG6GEObzPg+JNg=='; | ||
| 14 | my $pkgname = 'com.swjtu.gcmformojo'; | ||
| 15 | my $registration_id = $data->{registration_ids} // []; | ||
| 16 | if(ref $registration_id ne 'ARRAY' or @{$registration_id} == 0){ | ||
| 17 | $client->die("[".__PACKAGE__."]registration_ids无效"); | ||
| 18 | } | ||
| 19 | |||
| 20 | |||
| 21 | $client->on(receive_message=>sub{ | ||
| 22 | my($client,$msg) = @_; | ||
| 23 | my $type = 'Mojo-Weixin'; | ||
| 24 | my $title; | ||
| 25 | my $message; | ||
| 26 | my $msgId; | ||
| 27 | my $senderType; | ||
| 28 | my $isAt = 0; | ||
| 29 | |||
| 30 | if($msg->is_at) { | ||
| 31 | $isAt=1; | ||
| 32 | } | ||
| 33 | if($msg->type eq 'friend_message'){ | ||
| 34 | return if $data->{is_ban_official} and $msg->sender->category eq '公众号'; | ||
| 35 | $msgId = $msg->sender->id; | ||
| 36 | $title = $msg->sender->displayname; | ||
| 37 | $message = $msg->content; | ||
| 38 | $senderType = '1'; | ||
| 39 | } | ||
| 40 | elsif($msg->type eq 'group_message'){ | ||
| 41 | if(!$isAt) { | ||
| 42 | 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}}; | ||
| 43 | 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}}; | ||
| 44 | } | ||
| 45 | $msgId = $msg->group->id; | ||
| 46 | $title = $msg->group->displayname; | ||
| 47 | $message = $msg->sender->displayname . ": " . $msg->content; | ||
| 48 | $senderType = '2'; | ||
| 49 | } | ||
| 50 | elsif($msg->type eq 'discuss_message'){ | ||
| 51 | 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}}; | ||
| 52 | 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}}; | ||
| 53 | $msgId= $msg->discuss->id; | ||
| 54 | $title = $msg->discuss->displayname; | ||
| 55 | $message = $msg->sender->displayname . ": " . $msg->content; | ||
| 56 | $senderType = '3'; | ||
| 57 | } | ||
| 58 | elsif($msg->type eq 'sess_message'){ | ||
| 59 | |||
| 60 | } | ||
| 61 | return if !$title or !$message; | ||
| 62 | |||
| 63 | $client->http_post($api_url, | ||
| 64 | {'Authorization'=>"key=$api_key", | ||
| 65 | json=>1 | ||
| 66 | }, | ||
| 67 | form=>{ | ||
| 68 | pass_through => 1, | ||
| 69 | registration_id => $registration_id, | ||
| 70 | restricted_package_namee => $pkgname, | ||
| 71 | payload=>$client->to_json({isAt=>$isAt,type=>$type,title=>$title,message=>$message,msgId=>$msgId,senderType=>$senderType}), | ||
| 72 | }, | ||
| 73 | sub{ | ||
| 74 | my $json = shift; | ||
| 75 | if(not defined $json){ | ||
| 76 | $client->debug("[".__PACKAGE__."]小米消息推送失败: 返回结果异常"); | ||
| 77 | return; | ||
| 78 | } | ||
| 79 | else{ | ||
| 80 | $client->debug("[".__PACKAGE__."]小米消息推送状态:$json->{error}"); | ||
| 81 | } | ||
| 82 | } | ||
| 83 | |||
| 84 | |||
| 85 | ); | ||
| 86 | |||
| 87 | }); | ||
| 88 | |||
| 89 | $client->on(all_event => sub{ | ||
| 90 | my($client,$event,@args) =@_; | ||
| 91 | my $type = 'Mojo-Sys'; | ||
| 92 | my $message; | ||
| 93 | my $msgId = 2; | ||
| 94 | my $title; | ||
| 95 | if($event eq 'login'){ | ||
| 96 | $message = "登录成功"; | ||
| 97 | $title = "登录事件"; | ||
| 98 | } | ||
| 99 | elsif($event eq 'input_qrcode'){ | ||
| 100 | $message = $client->qrcode_upload_url // '获取二维码url失败'; | ||
| 101 | $title = "扫描二维码事件"; | ||
| 102 | } | ||
| 103 | elsif($event eq 'stop'){ | ||
| 104 | $message = "Mojo-Weixin已停止"; | ||
| 105 | $title = "停止事件"; | ||
| 106 | } | ||
| 107 | else{return} | ||
| 108 | |||
| 109 | $client->http_post($api_url, | ||
| 110 | { 'Authorization'=>"key=$api_key", | ||
| 111 | blocking=>1, | ||
| 112 | json=>1, | ||
| 113 | ua_connect_timeout=>5, | ||
| 114 | ua_request_timeout=>5, | ||
| 115 | ua_inactivity_timeout=>5, | ||
| 116 | ua_retry_times=>1 | ||
| 117 | }, | ||
| 118 | form=>{ | ||
| 119 | pass_through => 1, | ||
| 120 | registration_id => $registration_id, | ||
| 121 | restricted_package_namee => $pkgname, | ||
| 122 | payload=>$client->to_json({type=>$type,title=>$title,message=>$message,msgId=>$msgId}), | ||
| 123 | }, | ||
| 124 | sub{ | ||
| 125 | my $json = shift; | ||
| 126 | if(not defined $json){ | ||
| 127 | $client->debug("[".__PACKAGE__."]小米消息推送失败: 返回结果异常"); | ||
| 128 | return; | ||
| 129 | } | ||
| 130 | else{ | ||
| 131 | $client->debug("[".__PACKAGE__."]小米消息推送状态:$json->{error}"); | ||
| 132 | } | ||
| 133 | } | ||
| 134 | ); | ||
| 135 | }); | ||
| 136 | } | ||
| 137 | 1; |