| 1 | package Mojo::Webqq::Plugin::RikkaGCM; | ||
| 2 | our $AUTHOR = 'shizuku@shizuku.moe'; | ||
| 3 | our $SITE = ''; | ||
| 4 | our $DESC = ''; | ||
| 5 | our $PRIORITY = 97; | ||
| 6 | use List::Util qw(first); | ||
| 7 | sub call { | ||
| 8 | my $client = shift; | ||
| 9 | my $data = shift; | ||
| 10 | $client->load("UploadQRcode") if !$client->is_load_plugin('UploadQRcode'); | ||
| 11 | my $api_url = $data->{api_url}; | ||
| 12 | my $api_key = $data->{api_key} or $client->die("[".__PACKAGE__."]必须指定api_key"); | ||
| 13 | my $collapse_key = $data->{collapse_key}; | ||
| 14 | my $registration_ids = $data->{registration_ids} // []; | ||
| 15 | if(ref $registration_ids ne 'ARRAY' or @{$registration_ids} == 0){ | ||
| 16 | $client->die("[".__PACKAGE__."]registration_ids无效"); | ||
| 17 | } | ||
| 18 | $client->on(receive_message=>sub{ | ||
| 19 | my($client,$msg) = @_; | ||
| 20 | my $type = 'Mojo-Webqq'; | ||
| 21 | my $msgId; | ||
| 22 | my $senderId; | ||
| 23 | my $title; | ||
| 24 | my $sender; | ||
| 25 | my $content; | ||
| 26 | my $senderType; | ||
| 27 | my $isAt = 0; | ||
| 28 | my $seuid = 0; | ||
| 29 | if($msg->is_at) { | ||
| 30 | $isAt=1; | ||
| 31 | } | ||
| 32 | if($msg->type eq 'friend_message'){ | ||
| 33 | $senderId = $msg->sender->id; | ||
| 34 | $uid = $msg->sender->uid; | ||
| 35 | $msgId = $msg->id; | ||
| 36 | $title = $msg->sender->displayname; | ||
| 37 | $sender = $msg->sender->displayname; | ||
| 38 | $content = $msg->content; | ||
| 39 | $senderType = '1'; | ||
| 40 | } | ||
| 41 | elsif($msg->type eq 'group_message'){ | ||
| 42 | if(!$isAt) { | ||
| 43 | 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}}; | ||
| 44 | 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}}; | ||
| 45 | } | ||
| 46 | $senderId = $msg->group->id; | ||
| 47 | $uid = $msg->group->uid; | ||
| 48 | $msgId = $msg->id; | ||
| 49 | $title = $msg->group->displayname; | ||
| 50 | $sender = $msg->sender->displayname; | ||
| 51 | $content = $msg->content; | ||
| 52 | $senderType = '2'; | ||
| 53 | } | ||
| 54 | elsif($msg->type eq 'discuss_message'){ | ||
| 55 | 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}}; | ||
| 56 | 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}}; | ||
| 57 | $senderId = $msg->discuss->id; | ||
| 58 | $uid = $msg->discuss->id; | ||
| 59 | $msgId= $msg->id; | ||
| 60 | $title = $msg->discuss->displayname; | ||
| 61 | $sender = $msg->sender->displayname; | ||
| 62 | $content = $msg->content; | ||
| 63 | $senderType = '3'; | ||
| 64 | } | ||
| 65 | elsif($msg->type eq 'sess_message'){ | ||
| 66 | |||
| 67 | } | ||
| 68 | return if !$title or !$content; | ||
| 69 | $client->http_post($api_url, | ||
| 70 | {'Authorization'=>"key=$api_key",json=>1}, | ||
| 71 | json=>{ | ||
| 72 | registration_ids=> $registration_ids, | ||
| 73 | $collapse_key?(collapse_key=> $collapse_key):(), | ||
| 74 | priority=> $data->{priority} // 'high', | ||
| 75 | data=>{isAt=>$isAt,type=>$type,title=>$title,sender=>$sender,content=>$content,uid=>$uid,senderId=>$senderId,msgId=>$msgId,senderType=>$senderType}, | ||
| 76 | }, | ||
| 77 | sub{ | ||
| 78 | #"{"multicast_id":9016211065189210367,"success":1,"failure":0,"canonical_ids":0,"results":[{"message_id":"0:1484103730761325%9b9e6c13f9fd7ecd"}]}" | ||
| 79 | my $json = shift; | ||
| 80 | if(not defined $json){ | ||
| 81 | $client->debug("[".__PACKAGE__."]GCM消息推送失败: 返回结果异常"); | ||
| 82 | return; | ||
| 83 | } | ||
| 84 | else{ | ||
| 85 | $client->debug("[".__PACKAGE__."]GCM消息推送完成:$json->{multicast_id}/$json->{success}/$json->{failure}"); | ||
| 86 | } | ||
| 87 | } | ||
| 88 | ); | ||
| 89 | }); | ||
| 90 | |||
| 91 | $client->on(all_event => sub{ | ||
| 92 | my($client,$event,@args) =@_; | ||
| 93 | my $type = 'Mojo-Sys'; | ||
| 94 | my $content; | ||
| 95 | my $senderId = -1; | ||
| 96 | my $title; | ||
| 97 | if($event eq 'login'){ | ||
| 98 | $senderId = -1; | ||
| 99 | $content = "登录成功"; | ||
| 100 | $title = "登录事件"; | ||
| 101 | } | ||
| 102 | elsif($event eq 'input_qrcode'){ | ||
| 103 | $senderId = -2; | ||
| 104 | $content = $client->qrcode_upload_url // '获取二维码url失败'; | ||
| 105 | $title = "扫描二维码事件"; | ||
| 106 | } | ||
| 107 | elsif($event eq 'stop'){ | ||
| 108 | $senderId = -3; | ||
| 109 | $content = "Mojo-Webqq已停止"; | ||
| 110 | $title = "停止事件"; | ||
| 111 | } | ||
| 112 | else{return} | ||
| 113 | $client->http_post($api_url, | ||
| 114 | { | ||
| 115 | 'Authorization'=>"key=$api_key", | ||
| 116 | json=>1, | ||
| 117 | blocking=>1, | ||
| 118 | ua_connect_timeout=>5, | ||
| 119 | ua_request_timeout=>5, | ||
| 120 | ua_inactivity_timeout=>5, | ||
| 121 | ua_retry_times=>1 | ||
| 122 | }, | ||
| 123 | json=>{ | ||
| 124 | registration_ids=> $registration_ids, | ||
| 125 | $collapse_key?(collapse_key=> $collapse_key):(), | ||
| 126 | priority=> $data->{priority} // 'high', | ||
| 127 | data=>{type=>$type,title=>$title,content=>$content,senderId=>$senderId}, | ||
| 128 | }, | ||
| 129 | sub{ | ||
| 130 | my $json = shift; | ||
| 131 | if(not defined $json){ | ||
| 132 | $client->debug("[".__PACKAGE__."]GCM消息推送失败: 返回结果异常"); | ||
| 133 | return; | ||
| 134 | } | ||
| 135 | else{ | ||
| 136 | $client->debug("[".__PACKAGE__."]GCM消息推送完成:$json->{multicast_id}/$json->{success}/$json->{failure}"); | ||
| 137 | } | ||
| 138 | } | ||
| 139 | ); | ||
| 140 | }); | ||
| 141 | } | ||
| 142 | 1; |