| 1 | package Mojo::Weixin::Plugin::Weather; | ||
| 2 | our $PRIORITY=91; | ||
| 3 | |||
| 4 | sub call | ||
| 5 | { | ||
| 6 | my $client=shift; | ||
| 7 | my $callback=sub | ||
| 8 | { | ||
| 9 | my ($client,$msg)=@_; | ||
| 10 | return if $msg->class eq "send" and $msg->from eq "code"; | ||
| 11 | return if not $msg->allow_plugin; | ||
| 12 | if($msg->content=~/^(\S+)天气$/) | ||
| 13 | { | ||
| 14 | $msg->allow_plugin(0); | ||
| 15 | my $city=$1; | ||
| 16 | my $weatherurl='http://apix.sinaapp.com/weather/?&city='. $client->url_escape($city); | ||
| 17 | $client->http_get($weatherurl,sub | ||
| 18 | { | ||
| 19 | my $content=shift; | ||
| 20 | if(not defined $content){$msg->reply("api接口不可用,perlcoder公众号留言")} | ||
| 21 | |||
| 22 | elsif($content=~/没有该城市/) | ||
| 23 | { | ||
| 24 | $msg->reply("没有该城市: $city市,格式:上海天气"); | ||
| 25 | } | ||
| 26 | elsif($content=~/[{.*}]/g) | ||
| 27 | { | ||
| 28 | my $json = $client->from_json($content); | ||
| 29 | if(not defined $json){ | ||
| 30 | $client->debug("[ " . __PACKAGE__ . " ]json数据解析失败"); | ||
| 31 | return; | ||
| 32 | } | ||
| 33 | my $result = join "\n",map {$_->{Title}} @{$json}; | ||
| 34 | $result = $result; | ||
| 35 | $msg->reply($result); | ||
| 36 | } | ||
| 37 | |||
| 38 | |||
| 39 | |||
| 40 | |||
| 41 | } | ||
| 42 | ); | ||
| 43 | |||
| 44 | |||
| 45 | |||
| 46 | }; | ||
| 47 | #http://apix.sinaapp.com/weather/?&city=%E6%97%A0%E9%94%A1 | ||
| 48 | |||
| 49 | |||
| 50 | }; | ||
| 51 | |||
| 52 | $client->on(receive_message=>$callback,send_message=>$callback); | ||
| 53 | |||
| 54 | |||
| 55 | |||
| 56 | |||
| 57 | |||
| 58 | } | ||
| 59 | 1; |