Webqq/Plugin/GasPrice.pm untested

filter: Statementstmt 0.0% 0 / 61 Branchbran 0.0% 0 / 36 Conditioncond 0.0% 0 / 12 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 0.0% 0 / 5 Podpod 0.0% 0 / 1 totaltotal 0.0% 0 / 122 CC 19 SCAR 57.5
CC
19
Cov
0%
call
380.0
BEGIN
2.0
BEGIN
2.0
CRAP 314.3
1 package Mojo::Webqq::Plugin::GasPrice;
2 use strict;
3 use POSIX qw(strftime);
4 use Mojo::Util qw(url_escape);
5 use List::Util qw(first);
6 our $PRIORITY = 91;
7 my $API = 'http://apis.baidu.com/showapi_open_bus/oil_price/find?prov=';
8  
9 sub call{
10 #获取qqClient
11 my $client = shift;
12 #获取加载该插件时候传入的参数数组
13 my $data = shift;
14  
15 #如果参数有传是否需要@才进行回复,如果么有,则默认需要@才回复
16 my $is_need_at = defined $data->{is_need_at} ? $data->{is_need_at}:0;
17 my $key_word = defined $data->{command} ? $data->{command}:'油价';
18 my $msg_tail = defined $data->{msg_tail} ? $data->{msg_tail}:'';
19  
20 my $callBack = sub{
21 my($client,$msg)=@_;
22  
23 #如果消息中设定不允许插件处理,则直接返回
24 return if( not $msg->allow_plugin );
25 #只处理 好友消息|群消息|临时消息
26 return if( $msg->type !~ /^message|group_message|sess_message$/ );
27 #如果设置了需要@ 且消息类型是群消息,则判断消息中是否有@,如果没有则直接返回
28 return if( $is_need_at and $msg->type eq "group_message" and !$msg->is_at );
29  
30 #或者发送者和接受者的昵称
31 my $sender_nick = $msg->sender->displayname;
32 my $user_nick = $msg->receiver->displayname;
33  
34 #如果是群消息,则判断是否有设置禁止群和允许群(设置是由load插件的时候传入的参数设置)
35 if($msg->type eq 'group_message'){
36 return if( ref $data->{ban_group} eq "ARRAY" and first { $_ =~ /^\d+$/ ? $msg->group->uid eq $_ : $msg->group->name eq $_} @{$data->{ban_group}} );
37 return if( ref $data->{allow_group} eq "ARRAY" and !first { $_ =~ /^\d+$/ ? $msg->group->uid eq $_ : $msg->group->name eq $_} @{$data->{allow_group}} );
38 }
39  
40 #获取接受消息的内容
41 my $input = $msg->content;
42 #把前面的@昵称去掉
43 $input=~s/\@\Q$user_nick\E ?|\[[^\[\]]+\]\x01|\[[^\[\]]+\]//g;
44 #如果去掉昵称后,收到的消息内容为空,则不用处理,直接返回
45 return unless $input;
46 my @ARGVS = split(/\s+/,$input);
47  
48 #这里设置需要获取的关键字,如果得到的不是所需关键字,则不处理,直接返回
49 return if($ARGVS[0] ne $key_word);
50 #如果匹配了关键字,即属于该插件处理的消息,设置该消息不允许其他插件处理
51 $msg->allow_plugin(0);
52  
53 my $prov = $ARGVS[1] ? url_escape($ARGVS[1]) : url_escape("广东");
54  
55 my $headers = {
56 apikey => '4febc94b54b90f8cc8090af772c25a21',#api key
57 json => 1,
58 };
59  
60 #使用http_get从API获取所需信息
61 $client->http_get($API.$prov,$headers,sub{
62 my $json = shift;
63 return unless defined $json;
64 my $resultArray = $json->{showapi_res_body}->{list};
65 return if scalar(@$resultArray) <= 0 ;
66 my $ct = $resultArray->[0]->{ct};
67 my $p0 = $resultArray->[0]->{p0};
68 my $p90 =$resultArray->[0]->{p90};
69 my $p93 = $resultArray->[0]->{p93};
70 my $p97 = $resultArray->[0]->{p97};
71 my $prov = $resultArray->[0]->{prov};
72 my $reply = "您好!".$prov."的油价情况如下:\n\t发布时间:".$ct."\n\t"
73 . "0#:".$p0."\n\t"
74 . "90#:".$p90."\n\t"
75 . "93#:".$p93."\n\t"
76 . "97#:".$p97;
77 $reply = "\@$sender_nick " . $reply if $msg->type eq 'group_message';
78 $client->reply_message($msg,$reply,sub{
79 my($client,$msg) = @_;
80 my $content = $msg->content;
81 $content .= $msg_tail;
82 $msg->content($content);
83 $msg->from("bot");}) if $reply;
84 });
85 };
86 $client->on(receive_message=>$callBack);
87 }
88 1;