| 1 | package Mojo::Weixin::Plugin::Riddle; | ||
| 2 | our $PRIORITY = 92; | ||
| 3 | use List::Util qw(first); | ||
| 4 | sub call{ | ||
| 5 | my $client = shift; | ||
| 6 | my $data = shift; | ||
| 7 | my $flag = 0; | ||
| 8 | my $command = $data->{command} || "猜谜"; | ||
| 9 | $client->on(receive_message=>sub{ | ||
| 10 | my($client,$msg) = @_; | ||
| 11 | if($flag == 0 and $msg->content eq $command){ | ||
| 12 | $msg->allow_plugin(0); | ||
| 13 | $client->steps( | ||
| 14 | sub{ | ||
| 15 | my $delay =shift; | ||
| 16 | $client->http_get('http://apis.baidu.com/gushi/grid/p1',{json=>1,apikey=>$data->{apikey}||'20d7db97e337ffa35ae0838439c9db5d'},form=>{count=>1,fmt=>0},$delay->begin(0,1)); | ||
| 17 | }, | ||
| 18 | sub{ | ||
| 19 | my $delay = shift; | ||
| 20 | my $json = shift; | ||
| 21 | return if not defined $json; | ||
| 22 | return if $json->{status} != 0; | ||
| 23 | my $id = $json->{data}[0]{id} if ref $json->{data} eq "ARRAY"; | ||
| 24 | return if not $id; | ||
| 25 | $client->http_get('http://apis.baidu.com/gushi/grid/p2',{json=>1,apikey=>$data->{apikey}||'20d7db97e337ffa35ae0838439c9db5d'},form=>{id=>$id,fmt=>0},sub{ | ||
| 26 | my $json = shift; | ||
| 27 | return if not defined $json; | ||
| 28 | return if $json->{status} != 0; | ||
| 29 | return if ref $json->{data} ne "ARRAY"; | ||
| 30 | my $answer = $json->{data}[0]{body}; | ||
| 31 | $flag = 1; | ||
| 32 | $msg->reply("文曲星君题戏三界($json->{data}[0]{id}):\n" . $json->{data}[0]{title},sub{$_[1]->from("bot")}); | ||
| 33 | |||
| 34 | $client->wait( | ||
| 35 | $data->{timeout} || 30,#等待答案超时时间 | ||
| 36 | sub{#超时公布答案 | ||
| 37 | $flag = 0; | ||
| 38 | $msg->reply("本题答案:$answer\n偌大的三界之中,难道就没有能懂本星君心意之人么.\n吾独徘徊于天地之间,对酒影成双,知己难求,呜呼哉!"); | ||
| 39 | }, | ||
| 40 | receive_message=>sub{#查看是否有人给出正确答案 | ||
| 41 | my($client,$msg,$timer_id) = @_; | ||
| 42 | return if $msg->content !~ /\Q$answer\E/; | ||
| 43 | $flag = 0; | ||
| 44 | $msg->reply("于千万人之中,文曲星君终于找到了有缘人:\n恭喜 \@" . $msg->sender->displayname . " 回答正确",sub{$_[1]->from("bot")}); | ||
| 45 | return 1; | ||
| 46 | } | ||
| 47 | ); | ||
| 48 | }); | ||
| 49 | }, | ||
| 50 | ); | ||
| 51 | } | ||
| 52 | }); | ||
| 53 | } | ||
| 54 | 1; |