| 1 | package Webqq::Client::Plugin::SmartReply; | ||
| 2 | use JSON; | ||
| 3 | use AE; | ||
| 4 | use Encode; | ||
| 5 | use POSIX qw(strftime); | ||
| 6 | use Webqq::Client::Util qw(truncate console); | ||
| 7 | my $API = 'http://www.tuling123.com/openapi/api'; | ||
| 8 | my %limit; | ||
| 9 | my %ban; | ||
| 10 | my @limit_reply = ( | ||
| 11 | "对不起,请不要这么频繁的艾特我", | ||
| 12 | "对不起,您的艾特次数太多", | ||
| 13 | "说这么多话不累么,请休息几分钟", | ||
| 14 | "能不能小窗我啊,别吵着大家", | ||
| 15 | ); | ||
| 16 | #my $API = 'http://www.xiaodoubi.com/bot/api.php?chat='; | ||
| 17 | my $once = 1; | ||
| 18 | sub call{ | ||
| 19 | my $client = shift; | ||
| 20 | my $msg = shift; | ||
| 21 | return if $msg->{type} !~ /^message|group_message|sess_message$/; | ||
| 22 | my $self_nick = $client->{qq_database}{user}{nick}; | ||
| 23 | return if $msg->{allow_plugin} == 0; | ||
| 24 | my $msg_type = $msg->{type}; | ||
| 25 | if($msg_type eq 'group_message'){ | ||
| 26 | return 1 if $msg->{content} !~/\@\Q$self_nick \E/; | ||
| 27 | } | ||
| 28 | my $userid = $msg->from_qq; | ||
| 29 | return 1 if exists $ban{$userid}; | ||
| 30 | |||
| 31 | my $from_nick; | ||
| 32 | my $from_city = $msg->from_city if ($msg->{type} eq 'group_message' or $msg->{type} eq 'message'); | ||
| 33 | if($msg->{type} eq 'group_message'){ | ||
| 34 | $from_nick = $msg->from_card || $msg->from_nick; | ||
| 35 | } | ||
| 36 | else{ | ||
| 37 | $from_nick = $msg->from_nick; | ||
| 38 | } | ||
| 39 | |||
| 40 | if($msg->{type} eq 'group_message'){ | ||
| 41 | my $key = strftime("%H",localtime(time)); | ||
| 42 | $limit{$key}{$msg->{from_uin}}{$userid}++; | ||
| 43 | |||
| 44 | my $limit = $limit{$key}{$msg->{from_uin}}{$userid}; | ||
| 45 | if($limit>=3 and $limit<=4){ | ||
| 46 | $client->reply_message($msg,"\@$from_nick " . $limit_reply[int rand($#limit_reply+1)]); | ||
| 47 | return 1; | ||
| 48 | } | ||
| 49 | |||
| 50 | if($limit >=5 and $limit <=6){ | ||
| 51 | $client->reply_message($msg,"\@$from_nick " . "警告,您艾特过于频繁,即将被列入黑名单,请克制\n"); | ||
| 52 | return 1; | ||
| 53 | } | ||
| 54 | |||
| 55 | if($limit > 6){ | ||
| 56 | $ban{$userid} = 1; | ||
| 57 | $client->reply_message($msg,"\@$from_nick " . "您已被列入黑名单,1小时内提问无视\n"); | ||
| 58 | my $watcher = rand(); | ||
| 59 | $client->{watchers}{$watcher} = AE::timer 3600,0,sub{ | ||
| 60 | delete $client->{watchers}{$watcher}; | ||
| 61 | delete $ban{$userid}; | ||
| 62 | }; | ||
| 63 | return 1; | ||
| 64 | } | ||
| 65 | } | ||
| 66 | |||
| 67 | my $input = $msg->{content}; | ||
| 68 | $input=~s/\@\Q$self_nick\E ?|\[[^\[\]]+\]\x01|\[[^\[\]]+\]//g; | ||
| 69 | return unless $input; | ||
| 70 | my @query_string = ( | ||
| 71 | "key" => "4c53b48522ac4efdfe5dfb4f6149ae51", | ||
| 72 | "userid" => $userid, | ||
| 73 | "info" => $input, | ||
| 74 | ); | ||
| 75 | push @query_string,(loc=>$from_city."市") if $from_city; | ||
| 76 | my @query_string_pairs; | ||
| 77 | push @query_string_pairs , shift(@query_string) . "=" . shift(@query_string) while(@query_string); | ||
| 78 | $client->{asyn_ua}->get($API . "?" . join("&",@query_string_pairs),(),sub{ | ||
| 79 | my $res =shift; | ||
| 80 | if($client->{debug}){ | ||
| 81 | print "GET " . $API . "?" . join("&",@query_string_pairs),"\n"; | ||
| 82 | print $res->as_string,"\n"; | ||
| 83 | } | ||
| 84 | my $reply; | ||
| 85 | my $data = {}; | ||
| 86 | eval{ | ||
| 87 | $data = JSON->new->utf8->decode($res->content); | ||
| 88 | }; | ||
| 89 | if($@){ | ||
| 90 | print $@,"\n" if $client->{debug}; | ||
| 91 | return 1; | ||
| 92 | } | ||
| 93 | return 1 if $data->{code}=~/^4000[1-7]$/; | ||
| 94 | if($data->{code} == 100000){ | ||
| 95 | $reply = encode("utf8",$data->{text}); | ||
| 96 | } | ||
| 97 | elsif($data->{code}== 200000){ | ||
| 98 | $reply = encode("utf8","$data->{text}\n$data->{url}"); | ||
| 99 | } | ||
| 100 | else{ | ||
| 101 | return 1; | ||
| 102 | } | ||
| 103 | $reply = "\@$from_nick " . $reply if $msg_type eq 'group_message' and rand(100)>20; | ||
| 104 | $reply = truncate($reply,max_bytes=>300,max_lines=>5) if $msg_type eq 'group_message'; | ||
| 105 | $client->reply_message($msg,$reply) if $reply; | ||
| 106 | }); | ||
| 107 | |||
| 108 | if($once){ | ||
| 109 | $client->{watchers}{rand()} = AE::timer 3600,3600,sub{ | ||
| 110 | my $key = strftime("%H",localtime(time-3600)); | ||
| 111 | delete $limit{$key}; | ||
| 112 | }; | ||
| 113 | $once = 0; | ||
| 114 | } | ||
| 115 | |||
| 116 | return 1; | ||
| 117 | } | ||
| 118 | 1; |