| 1 | package Mojo::Weixin::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 | |||
| 18 | my $registration_ids = $data->{registration_ids} // []; | ||
| 19 | if(ref $registration_ids ne 'ARRAY' or @{$registration_ids} == 0){ | ||
| 20 | |||
| 21 | $client->die("[".__PACKAGE__."]registration_ids无效"); | ||
| 22 | } | ||
| 23 | my $registration_id = $registration_ids->[0]; | ||
| 24 | |||
| 25 | $client->on(receive_message=>sub{ | ||
| 26 | my($client,$msg) = @_; | ||
| 27 | my $type = 'Mojo-Weixin'; | ||
| 28 | my $title; | ||
| 29 | my $message; | ||
| 30 | my $msgId; | ||
| 31 | my $senderType; | ||
| 32 | my $isAt = 0; | ||
| 33 | |||
| 34 | if($msg->is_at) { | ||
| 35 | $isAt=1; | ||
| 36 | } | ||
| 37 | if($msg->type eq 'friend_message'){ | ||
| 38 | return if $data->{is_ban_official} and $msg->sender->category eq '公众号'; | ||
| 39 | $msgId = $msg->sender->id; | ||
| 40 | $title = $msg->sender->displayname; | ||
| 41 | $message = $msg->content; | ||
| 42 | $senderType = '1'; | ||
| 43 | } | ||
| 44 | elsif($msg->type eq 'group_message'){ | ||
| 45 | if(!$isAt) { | ||
| 46 | 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}}; | ||
| 47 | 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}}; | ||
| 48 | } | ||
| 49 | $msgId = $msg->group->id; | ||
| 50 | $title = $msg->group->displayname; | ||
| 51 | $message = $msg->sender->displayname . ": " . $msg->content; | ||
| 52 | $senderType = '2'; | ||
| 53 | } | ||
| 54 | return if !$title or !$message; | ||
| 55 | |||
| 56 | my $messageJson = '{"content":"{\"isAt\":\"'.$isAt.'\",\"type\":\"'.$type.'\",\"title\":\"'.$title.'\",\"message\":\"'.$message.'\",\"msgId\":\"'.$msgId.'\",\"senderType\":\"'.$senderType.'\"}"}'; | ||
| 57 | print "appId=".$app_id."messageJson=".$messageJson."pushIds=".$registration_id.$api_key; | ||
| 58 | my $sign = md5_sum("appId=".$app_id."messageJson=".$messageJson."pushIds=".$registration_id.$api_key); | ||
| 59 | |||
| 60 | $client->http_post($api_url, | ||
| 61 | {ua_debug=>1,ua_debug_req_body=>1,ua_debug_res_body=>1,json=>1}, | ||
| 62 | form=>{ | ||
| 63 | appId => $app_id, | ||
| 64 | pushIds => $registration_id, | ||
| 65 | messageJson=>$messageJson, | ||
| 66 | sign => $sign, | ||
| 67 | }, | ||
| 68 | sub{ | ||
| 69 | #"{"multicast_id":9016211065189210367,"success":1,"failure":0,"canonical_ids":0,"results":[{"message_id":"0:1484103730761325%9b9e6c13f9fd7ecd"}]}" | ||
| 70 | my $json = shift; | ||
| 71 | if(not defined $json){ | ||
| 72 | $client->debug("[".__PACKAGE__."]魅族消息推送失败: 返回结果异常"); | ||
| 73 | return; | ||
| 74 | } | ||
| 75 | else{ | ||
| 76 | $client->debug("[".__PACKAGE__."]魅族消息推送完成:$json->{multicast_id}/$json->{success}/$json->{failure}"); | ||
| 77 | } | ||
| 78 | } | ||
| 79 | ); | ||
| 80 | }); | ||
| 81 | |||
| 82 | $client->on(all_event => sub{ | ||
| 83 | my($client,$event,@args) =@_; | ||
| 84 | my $type = 'Mojo-Sys'; | ||
| 85 | my $message; | ||
| 86 | my $msgId = 2; | ||
| 87 | my $title; | ||
| 88 | if($event eq 'login'){ | ||
| 89 | $message = "登录成功"; | ||
| 90 | $title = "登录事件"; | ||
| 91 | } | ||
| 92 | elsif($event eq 'input_qrcode'){ | ||
| 93 | $message = $client->qrcode_upload_url // '获取二维码url失败'; | ||
| 94 | $title = "扫描二维码事件"; | ||
| 95 | } | ||
| 96 | elsif($event eq 'stop'){ | ||
| 97 | $message = "Mojo-Weixin已停止"; | ||
| 98 | $title = "停止事件"; | ||
| 99 | } | ||
| 100 | else{return} | ||
| 101 | |||
| 102 | my $messageJson = '{"content":"{\"type\":\"'.$type.'\",\"title\":\"'.$title.'\",\"message\":\"'.$message.'\",\"msgId\":\"'.$msgId.'\"}"}'; | ||
| 103 | |||
| 104 | $client->http_post($api_url, | ||
| 105 | { | ||
| 106 | blocking=>1, | ||
| 107 | json=>1, | ||
| 108 | ua_connect_timeout=>5, | ||
| 109 | ua_request_timeout=>5, | ||
| 110 | ua_inactivity_timeout=>5, | ||
| 111 | ua_retry_times=>1 | ||
| 112 | }, | ||
| 113 | form=>{ | ||
| 114 | |||
| 115 | appId => $app_id, | ||
| 116 | pushIds => $registration_id, | ||
| 117 | messageJson=>$messageJson, | ||
| 118 | sign => md5_sum("appId=".$app_id."messageJson=".$messageJson."pushIds=".$registration_id.$api_key), | ||
| 119 | |||
| 120 | }, | ||
| 121 | sub{ | ||
| 122 | my $json = shift; | ||
| 123 | if(not defined $json){ | ||
| 124 | $client->debug("[".__PACKAGE__."]魅族消息推送失败: 返回结果异常"); | ||
| 125 | return; | ||
| 126 | } | ||
| 127 | else{ | ||
| 128 | $client->debug("[".__PACKAGE__."]魅族消息推送完成:$json->{multicast_id}/$json->{success}/$json->{failure}"); | ||
| 129 | } | ||
| 130 | } | ||
| 131 | ); | ||
| 132 | }); | ||
| 133 | } | ||
| 134 | 1; |