How can I make my Perl Jabber bot an event

I'm trying to make a Jabber bot and I am having trouble keeping it running while waiting for messages. How do I get my script to continuously run? I have tried calling a subroutine that has a while loop that I, in theory, have set up to check for any messages and react accordingly but my script isn't behaving that way.

Here is my source: http://pastebin.com/03Habbvh

# set jabber bot callbacks
$jabberBot->SetMessageCallBacks(chat=>&chat);
$jabberBot->SetPresenceCallBacks(available=>&welcome,unavailable=>&killBot);
$jabberBot->SetCallBacks(receive=>&prnt,iq=>&gotIQ);

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

sub welcome
{
    print "Welcome!n";
    $jabberBot->MessageSend(to=>$jbrBoss->GetJID(),subject=>"",body=>"Hello There!",type=>"chat",priority=>10);
    &keepItGoing;
}

sub prnt
{
    print $_[1]."n";
}

#$jabberBot->MessageSend(to=>$jbrBoss->GetJID(),subject=>"",body=>"Hello There! Global...",type=>"chat",priority=>10);
#$jabberBot->Process(5);
#&keepItGoing;

sub chat
{
    my ($sessionID,$msg) = @_;
    $dump->pl2xml($msg);
    if($msg->GetType() ne 'get' && $msg->GetType() ne 'set' && $msg->GetType() ne '')
    {
        my $jbrCmd = &trimSpaces($msg->GetBody());
        my $dbQry = $dbh->prepare("SELECT command,acknowledgement FROM commands WHERE message = '".lc($jbrCmd)."'");
        $dbQry->execute();
        if($dbQry->rows() > 0 && $jbrCmd !~ /^insert/si)
        {
            my $ref = $dbQry->fetchrow_hashref();
            $dbQry->finish();
            $jabberBot->MessageSend(to=>$msg->GetFrom(),subject=>"",body=>$ref->{'acknowledgement'},type=>"chat",priority=>10);
            eval $ref->{'command'};
            &keepItGoing;
        }
        else
        {
            $jabberBot->MessageSend(to=>$msg->GetFrom(),subject=>"",body=>"I didn't understand you!",type=>"chat",priority=>10);
            $dbQry->finish();
            &keepItGoing;
        }
    }
}

sub gotIQ
{
    print "iqn";
}

sub trimSpaces
{
    my $string = $_[0];
    $string =~ s/^s+//; #remove leading spaces
    $string =~ s/s+$//; #remove trailing spaces
    return $string;
}

sub keepItGoing
{

    print "keepItGoing!n";
    my $proc = $jabberBot->Process(1);
    while(defined($proc) && $proc != 1)
    {
        $proc = $jabberBot->Process(1);
    }
}

sub killBot
{
    print "killingn";
    $jabberBot->MessageSend(to=>$_[0]->GetFrom(),subject=>"",body=>"Logging Out!",type=>"chat",priority=>10);
    $jabberBot->Process(1);
    $jabberBot->Disconnect();
    exit;
}

POE has some pretty good event frameworks. I don't know how good the one for Jabber (POE::Component::Jabber) is, but it's probably worth looking at.


AnyEvent :: XMPP是非常全面的,并且,因为它使用AnyEvent,可以在支持循环(AnyEvent自己的,Event,EV,Tk,Glib / Gtk,甚至是POE)的任何事件驱动的应用程序中运行。


I think you can make your example work by doing this:

0 while $jabber->Process

Having said that, I would strongly recommend using a proper Event handling framework such as AnyEvent (my personal favorite) or POE (the traditional choice).

链接地址: http://www.djcxy.com/p/94046.html

上一篇: 如何获得联系人的可用性?

下一篇: 我怎样才能让我的Perl Jabber机器人成为一个事件