| 1 | package Mojo::Weixin::Plugin::XiaoiceReply; | ||
| 2 | our $PRIORITY = 1; | ||
| 3 | use List::Util qw(first); | ||
| 4 | sub call{ | ||
| 5 | my $client = shift; | ||
| 6 | my $data = shift; | ||
| 7 | my $is_need_at = defined $data->{is_need_at}?$data->{is_need_at}:1; | ||
| 8 | my $command_on = $data->{comamnd_on} || "小冰启动"; | ||
| 9 | my $command_off = $data->{comamnd_off} || "小冰停止"; | ||
| 10 | my $onoff_flag = 1; | ||
| 11 | my @db; | ||
| 12 | |||
| 13 | $client->on(ready=>sub{ | ||
| 14 | #支持绑定的两个对象之间互相转发消息 | ||
| 15 | my $xiaoice = $client->search_friend(name=>"小冰",category=>"公众号"); | ||
| 16 | if(not defined $xiaoice){ | ||
| 17 | $client->error("未能在通讯录中搜索到 微软小冰 帐号信息,请确认是否已经关注 微软小冰 公众号"); | ||
| 18 | return; | ||
| 19 | } | ||
| 20 | $client->interval($data->{ttl}||30,sub{@db=()}); | ||
| 21 | $client->on(login=>sub{@db=()}); | ||
| 22 | $client->on(send_message=>sub{ | ||
| 23 | my($client,$msg) = @_; | ||
| 24 | if($msg->content eq $command_on){$onoff_flag = 1;$msg->reply("小冰已经启动");} | ||
| 25 | elsif($msg->content eq $command_off){$onoff_flag = 0;$msg->reply("小冰已经停止");} | ||
| 26 | }); | ||
| 27 | |||
| 28 | $client->on(receive_message=>sub{ | ||
| 29 | my($client,$msg) = @_; | ||
| 30 | return if $msg->type !~ /^friend_message|group_message$/; | ||
| 31 | return if $msg->format !~ /^text|media$/; | ||
| 32 | return if not $msg->allow_plugin; | ||
| 33 | return if not $onoff_flag; | ||
| 34 | my $xiaoice = $client->search_friend(name=>"小冰",category=>"公众号"); | ||
| 35 | if(not defined $xiaoice){ | ||
| 36 | $client->error("未能在通讯录中搜索到 微软小冰 帐号信息,请确认是否已经关注 微软小冰 公众号"); | ||
| 37 | return; | ||
| 38 | } | ||
| 39 | if($msg->sender->id ne $xiaoice->id){ | ||
| 40 | if($msg->type eq "group_message"){ | ||
| 41 | return if defined $data->{allow_group_reply} and !$data->{allow_group_reply}; | ||
| 42 | return if ref $data->{ban_group} eq "ARRAY" and first {$msg->group->displayname eq $_} @{$data->{ban_group}}; | ||
| 43 | return if ref $data->{allow_group} eq "ARRAY" and !first {$msg->group->displayname eq $_} @{$data->{allow_group}}; | ||
| 44 | return if ref $data->{ban_group_member} eq "ARRAY" and first {$msg->sender->displayname eq $_} @{$data->{ban_group_user}}; | ||
| 45 | return if ref $data->{allow_group_member} eq "ARRAY" and !first {$msg->sender->displayname eq $_} @{$data->{allow_group_user}}; | ||
| 46 | return if $is_need_at and !$msg->is_at; | ||
| 47 | } | ||
| 48 | else{ | ||
| 49 | return if defined $data->{allow_friend_reply} and !$data->{allow_friend_reply}; | ||
| 50 | return if ref $data->{ban_friend} eq "ARRAY" and first {$msg->sender->displayname eq $_} @{$data->{ban_user}}; | ||
| 51 | return if ref $data->{allow_friend} eq "ARRAY" and !first {$msg->sender->displayname eq $_} @{$data->{allow_user}}; | ||
| 52 | } | ||
| 53 | } | ||
| 54 | if($msg->sender->id eq $xiaoice->id){ | ||
| 55 | my $binder = $db[0]; | ||
| 56 | return if not defined $binder; | ||
| 57 | if($msg->format eq "media" and (defined $msg->media_id or -e $msg->media_path) ){ | ||
| 58 | $binder->send_media($msg->media_path); | ||
| 59 | } | ||
| 60 | else{ | ||
| 61 | $binder->send($msg->content,sub{$_[1]->from("bot")}); | ||
| 62 | } | ||
| 63 | } | ||
| 64 | else{ | ||
| 65 | my $object = $msg->type eq "group_message"?$msg->group:$msg->sender; | ||
| 66 | if (@db == 0){ | ||
| 67 | $db[0] = $object; | ||
| 68 | $msg->remove_at(); | ||
| 69 | if($msg->format eq "media" and (defined $msg->media_id or -e $msg->media_path) ){ | ||
| 70 | $xiaoice->send_media($msg->media_path); | ||
| 71 | } | ||
| 72 | else{ | ||
| 73 | $xiaoice->send($msg->content); | ||
| 74 | } | ||
| 75 | } | ||
| 76 | elsif(@db > 0 and $db[0]->id eq $object->id){ | ||
| 77 | $msg->remove_at(); | ||
| 78 | if($msg->format eq "media" and (defined $msg->media_id or -e $msg->media_path )){ | ||
| 79 | $xiaoice->send_media($msg->media_path); | ||
| 80 | } | ||
| 81 | else{ | ||
| 82 | $xiaoice->send($msg->content); | ||
| 83 | } | ||
| 84 | } | ||
| 85 | } | ||
| 86 | }); | ||
| 87 | }); | ||
| 88 | |||
| 89 | } | ||
| 90 | 1; |