我正在尝试制作一个Jabber机器人,但在等待消息时无法使其保持运行状态。如何使脚本连续运行?我尝试调用一个具有while循环的子例程,从理论上讲,我已设置该子例程来检查任何消息并做出相应的反应,但我的脚本却没有这种行为。

这是我的来源:http://pastebin.com/03Habbvh

#设置jabber机器人回调
$ jabberBot-> SetMessageCallBacks(chat => \&chat);
$ jabberBot-> SetPresenceCallBacks(available => \&welcome,unavailable => \&killBot);
$ jabberBot-> SetCallBacks(receive => \&prnt,iq => \&gotIQ);

$ jabberBot-> PresenceSend(type =>“available”);
$ jabberBot-> Process(1);

子欢迎
{
打印“欢迎!\ n”;
$ jabberBot-> MessageSend(to => $ jbrBoss-> GetJID(),subject =>“”,body =>“Hello There!”,type =>“chat”,priority => 10);
&保持下去;
}

子程序
{
打印$ _ [1]。“\ n”;
}

#$ jabberBot-> MessageSend(to => $ jbrBoss-> GetJID(),subject =>“”,body =>“Hello There!Global ...”,type =>“chat”,priority => 10);
#$ jabberBot-> Process(5);
#&保持下去;

子聊天
{
我的($ sessionID,$ msg)= @_;
$ dump-> pl2xml($ msg);
if($ msg-> GetType()ne'get'&& $ msg-> GetType()ne'set'&& $ msg-> GetType()ne'')
{
我的$ jbrCmd =&trimSpaces($ msg-> GetBody());
我的$ dbQry = $ dbh-> prepare(“SELECT命令,在消息='.lc($ jbrCmd)的情况下,来自命令的确认。”“”);
$ dbQry-> execute();
if($ dbQry-> rows()> 0 && $ jbrCmd!〜/ ^插入/ si)
{
我的$ ref = $ dbQry-> fetchrow_hashref();
$ dbQry-> finish();
$ jabberBot-> MessageSend(to => $ msg-> GetFrom(),subject =>“”,body => $ ref-> {'acknowledgement'},type =>“chat”,priority => 10);
评估$ ref-> {'command'};
&保持下去;
}
其他
{
$ jabberBot-> MessageSend(to => $ msg-> GetFrom(),subject =>“”,body =>“我不懂你!”,type =>“聊天”,优先级=> 10);
$ dbQry-> finish();
&保持下去;
}
}
}

子得到的IQ
{
打印“iq \ n”;
}

次修剪空间
{
我的$ string = $ _ [0];
$ string =〜s / ^ \ s + //; #删除前导空格
$ string =〜s / \ s + $ //; #删除尾随空格
返回$ string;
}

子keepItGoing
{

打印“keepItGoing!\ n”;
我的$ proc = $ jabberBot-> Process(1);
while(defined($ proc)&& $ proc!= 1)
{
$ proc = $ jabberBot-> Process(1);
}
}

子killBot
{
打印“kill \ n”;
$ jabberBot-> MessageSend(to => $ _ [0]-> GetFrom(),subject =>“”,body =>“登出!”,type =>“聊天”,优先级=> 10);
$ jabberBot-> Process(1);
$ jabberBot-> Disconnect();
出口;
}

最佳答案

POE有一些非常好的事件框架。我不知道Jabber(POE::Component::Jabber)的效果如何,但是可能值得一看。

07-27 13:40