How can I make my Perl Jabber bot an event driven program?

I am trying to create a Jabber bot and I am unable to start it, waiting for messages. How to make my script work continuously? I tried to call a routine that has a while loop, which I, theoretically, set up to check for any messages and reacted accordingly, but my script does not behave this 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 "iq \ n";
}

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 "killing \ n";
    $ jabberBot-> MessageSend (to => $ _ [0] -> GetFrom (), subject => "", body => "Logging Out!", type => "chat", priority => 10);
    $ jabberBot-> Process (1);
    $ jabberBot-> Disconnect ();
    exit
}
+3
source share
3 answers

POE . , Jabber (POE:: Component:: Jabber), , , .

+6

, , :

0 while $jabber->Process

, , ​​ AnyEvent ( ) POE ( ).

+2

Source: https://habr.com/ru/post/1738918/


All Articles