<pre name="code" class="sql"> use Weixin::Client;
use Encode;
#目前只能用于群回复
my $client = Weixin::Client->new(debug=>0);
#加载ShowMsg插件,用于打印消息
$client->load("ShowMsg");
#客户端登录
$client->login();
#设置客户端接收消息回调函数
$client->on_receive_msg = sub{
my $msg = shift ;
# my ($client,$msg)=@_;
## 消息内容
#打印收到的消息
$client->call("ShowMsg",$msg);
$recm= $msg->{Content};
$recm=encode("gbk",decode("utf8","$recm"));
if ($recm =~/中均\s+\d{11}/i){
$recm =~ s/^\s+//;
$number =(split(/\s+/,"$recm"))[1];
$i=0;
@arr=();
@tmp1=();
@tmp2=();
use Encode;
open (LOG1 ,"<",\'\\\\192.168.32.101\log01\zj-api01-catalina.out\') or die $!;
while (<LOG1>) {
if (($_ =~/$number/) and ($_ =~/SMSSendThread/i) ){$str=encode("gbk",decode("utf8","$_"));
# print "\$str is $str\n";
next if ($str !~ /验证/);
push (@tmp1 ,$str);
}};
close LOG1;
open (LOG2 ,"<",\'\\\\192.168.32.101\log01\zj-api02-catalina.out\') or die $!;
while (<LOG2>) {
if (($_ =~/$number/) and ($_ =~/SMSSendThread/i) ){$str=encode("gbk",decode("utf8","$_"));
print "\$str is $str\n";
next if ($str !~ /验证/);
push (@tmp2 ,$str);
}};
close LOG2;
#print "\$tmp1[-1] is $tmp1[-1]\n";
#print "\$tmp2[-1] is $tmp2[-1]\n";
@arr=($tmp1[-1], $tmp2[-1]);
print "\@arr is @arr\n";
if (defined($tmp1[-1]) or defined($tmp2[-1])){
foreach (@arr){
##2015-11-04 15:51:54,134 DEBUG com.zjzc.thirdparty.sms.SMSSendThread
if ($_ =~ /([0-9]{4}-[0-9]{1,2}-[0-9]{2}\s+[0-9]{2}:[0-9]{2}:[0-9]{2}.*?)\s+(.*?Send message:)\s*(.*?)(to\s+phoneNo:\s*[0-9]{11}).*/){
$a1=$1;
$a2=$2;
$a3=$3;
$a4=$4;
print "\$a1 is $a1\n";
# print "\$a2 is $a2\n";
print "\$a3 is $a3\n";
print "\$a4 is $a4\n";
##微信是UTF-8字符集
if ($a3 =~/验证/){ $a3=encode("utf8",decode("gbk","$a3"));
$client->reply_msg($msg,"$a1-$a3-$a4")
}};
# $client->reply_msg($msg,$number)
}
}else{$a5=encode("utf8",decode("gbk","没有找到需要的验证码信息"));$client->reply_msg($msg,"$a5")};
}
#对收到的消息,以相同的内容回复
elsif ($recm =~/温金\s+\d{11}/i)
{
$recm =~ s/^\s+//;
$number =(split(/\s+/,"$recm"))[1];
$i=0;
@arr=();
@tmp1=();
@tmp2=();
use Encode;
open (LOG1 ,"<",\'\\\\192.168.32.101\log01\wj-api01-catalina.out\') or die $!;
while (<LOG1>) {
if (($_ =~/$number/) and ($_ =~/SMSSendThread/) ){$str=encode("gbk",decode("utf8","$_"));
print "\$str is $str\n";
next if ($str !~ /验证/);
push (@tmp1 ,$str);
}};
close LOG1;
open (LOG2 ,"<",\'\\\\192.168.32.101\log01\wj-api02-catalina.out\') or die $!;
while (<LOG2>) {
if (($_ =~/$number/) and ($_ =~/SMSSendThread/) ){$str=encode("gbk",decode("utf8","$_"));
print "\$str is $str\n";
next if ($str !~ /验证/);
push (@tmp2 ,$str);
}};
close LOG2;
print "\$tmp1[-1] is $tmp1[-1]\n";
print "\$tmp2[-1] is $tmp2[-1]\n";
@arr=($tmp1[-1], $tmp2[-1]);
print "\@arr is @arr\n";
if (defined($tmp1[-1]) or defined($tmp2[-1]))
{foreach (@arr){
##2015-11-04 15:51:54,134 DEBUG com.zjzc.thirdparty.sms.SMSSendThread
if ($_ =~ /([0-9]{4}-[0-9]{1,2}-[0-9]{2}\s+[0-9]{2}:[0-9]{2}:[0-9]{2}.*?)\s+(.*?Send message:)\s*(.*?)(to\s+phoneNo:\s*[0-9]{11}).*/){
$a1=$1;
$a2=$2;
$a3=$3;
$a4=$4;
print "\$a1 is $a1\n";
# print "\$a2 is $a2\n";
print "\$a3 is $a3\n";
print "\$a4 is $a4\n";
##微信是UTF-8字符集
if ($a3 =~/验证/){ $a3=encode("utf8",decode("gbk","$a3"));
$client->reply_msg($msg,"$a1-$a3-$a4")
}};
# $client->reply_msg($msg,$number)
}
}else{$a5=encode("utf8",decode("gbk","没有找到需要的验证码信息"));$client->reply_msg($msg,"$a5")};
}
elsif($recm =~/gp\s+\d{1,11}/i){
$recm =~ s/^\s+//;
$number =(split(/\s+/,"$recm"))[1];
use LWP::UserAgent;
my $ua = LWP::UserAgent->new;
$ua->timeout(10);
$ua->env_proxy;
$ua->agent("Mozilla/8.0");
my $response = $ua->get("http://hq.sinajs.cn/list=sh$number");
$gupiao=$response->decoded_content;
if ($response->is_success) {
print $response->decoded_content; # or whatever
}
else {
die $response->status_line;
}
$price =(split(/,/,"$gupiao "))[3];
$var= "$recm 当前价格是$price";
print "$recm 当前价格是$price\n";
$var=encode("utf8",decode("gbk","$var"));
if (defined($price)){
$client->reply_msg($msg,"$var")
}else{my $response = $ua->get("http://hq.sinajs.cn/list=sz$number");
$gupiao=$response->decoded_content;
$price =(split(/,/,"$gupiao "))[3];
$var= "$recm 当前价格是$price";
print "$recm 当前价格是$price\n";
$var=encode("utf8",decode("gbk","$var"));
if (defined($price)){
$client->reply_msg($msg,"$var")}
else{reply_msg($msg,"not suport")}
}
};
#else {$a5=encode("utf8",decode("gbk","格式为:中均/温金 客户手机号"));$client->reply_msg($msg,"$a5")};
#设置客户端发送消息回调函数
$client->on_send_msg = sub {
my $msg = shift;
# 打印发送的消息
$client->call("ShowMsg",$msg);
}
};
#客户端进入事件循环,开始运行
$client->run();