User Control Panel
Advertisements

HELP US, HELP YOU!

msn.pl sending message question
Goto page 1, 2  Next
 
Post new topic   Reply to topic    Bot Depot Forum Index -> Perl
View unanswered posts
Author Message
chemichon
Newbie
Newbie


Joined: 18 Jun 2006
Posts: 8


PostPosted: Sun Jun 18, 2006 12:38 pm    Post subject: msn.pl sending message question Reply with quote

Hi,

I am trying to write a bot that sends message upon certain data comes from a db. I got the echobot.pl example.

I need to call a function inside the main loop that will check for new stuff from the DB and then send it to the user. Something went wrong, the function tries to send messages before msn is connected and then the script die.

Any help is appreciated. Thank you.

here is the code:
#############################
Code:

while ($run) {
        $msn->do_one_loop();
        if ($msn->isConnected()) {
                &read_notify;
        }

sub read_notify {
        my $sth = $dbh->prepare(qq{ QUERY });
        $sth->execute() or die $dbh->errstr;
        while ($n_data = $sth->fetchrow_hashref) {
                if ($debug) { print "--> &read_notify: id:$n_data->{id}\n"; }
                #vamos ler o spot e formar as mensagens
                &get_spot($n_data->{id_spot});
                my $msg = "DX de $spot[3] $spot[2] $spot[1] $spot[6] $spot[4]\n";

                if ($debug) { print "--> \$msn->call( $admin, \"$msg\", 'Effect' => 'BI', 'Color' => '00FF00', 'Name' => 'Your Bot' );\n"; }

                $msn->call( $admin, "$msg", 'Effect' => 'BI', 'Color' => '00FF00', 'Name' => 'Your Bot' );

         undef(@n_data);
        $sth->finish(); # we're done with this query
}
[/code]
Back to top
eric256
The Keymaker
The Keymaker


Joined: 03 May 2006
Posts: 2292
Location: Colorado
Reputation: 47Reputation: 47Reputation: 47Reputation: 47Reputation: 47

PostPosted: Sun Jun 18, 2006 3:32 pm    Post subject: Reply with quote

With your creative indentation its hard to tell, but it looks like you are missing an } early on. I don't see anywhere in this code where your while loop ends.
_________________
Eric256
Proud previous owner and current admin of Bot-depot.com
Back to top
patrick
Newbie
Newbie


Joined: 17 Apr 2006
Posts: 42
Location: Arnhem, the Netherlands
Reputation: 5.3Reputation: 5.3Reputation: 5.3Reputation: 5.3Reputation: 5.3
votes: 1

PostPosted: Sun Jun 18, 2006 4:16 pm    Post subject: Reply with quote

Just like eric said, you're missing a } .
I copied the code to crimson editor.
and saw that in the sub read_notify you're missing a }

I think you forgotten to close the red {
I think it needs to be closed at the green }
Quote:

sub read_notify {
my $sth = $dbh->prepare(qq{ QUERY });
$sth->execute() or die $dbh->errstr;
while ($n_data = $sth->fetchrow_hashref) {
if ($debug) { print "--> &read_notify: id:$n_data->{id}\n"; }
#vamos ler o spot e formar as mensagens
&get_spot($n_data->{id_spot});
my $msg = "DX de $spot[3] $spot[2] $spot[1] $spot[6] $spot[4]\n";

if ($debug) { print "--> \$msn->call( $admin, \"$msg\", 'Effect' => 'BI', 'Color' => '00FF00', 'Name' => 'Your Bot' );\n"; }

$msn->call( $admin, "$msg", 'Effect' => 'BI', 'Color' => '00FF00', 'Name' => 'Your Bot' );
}
undef(@n_data);
$sth->finish(); # we're done with this query
}
Back to top
chemichon
Newbie
Newbie


Joined: 18 Jun 2006
Posts: 8


PostPosted: Mon Jun 19, 2006 12:05 am    Post subject: code error Reply with quote

Hi,

Sorry, but this error is present only in the pasted code.
The code in the server is ok, at least it isnt ending in error when I run it.

Thank you,

Chemichon
Back to top
chemichon
Newbie
Newbie


Joined: 18 Jun 2006
Posts: 8


PostPosted: Mon Jun 19, 2006 12:40 am    Post subject: msn.pl sending message question Reply with quote

Well,

Just to add some information on my problem:

I am trying to have my bot to talk by itself, not talk after being trigged by a received message.

I tried to insert a function (read_notify()) that would check in a database if the bot had something to say. When this function is inserted, the bot wont connect to MSN network. I also tried to check if the bot was connected before sending messages on this function, but this did not work.

Thank you very much for your attention.

Chemichon
Back to top
Mojave
Almost An Agent
Almost An Agent


Joined: 01 Nov 2003
Posts: 1434

Reputation: 66.4

PostPosted: Mon Jun 19, 2006 1:16 am    Post subject: Reply with quote

You should always simplify to fix bugs.

Take all the db code out of your read_notify function and have it return static text. Then see if the bot is handling that correctly. That way you know whether or not it is a db bug. Also, test your db code separately from the bot, ie, in a simple script. Once you have both parts working correctly, then join the code.
Back to top
eric256
The Keymaker
The Keymaker


Joined: 03 May 2006
Posts: 2292
Location: Colorado
Reputation: 47Reputation: 47Reputation: 47Reputation: 47Reputation: 47

PostPosted: Mon Jun 19, 2006 1:28 am    Post subject: Re: code error Reply with quote

chemichon wrote:
Hi,

Sorry, but this error is present only in the pasted code.
The code in the server is ok, at least it isnt ending in error when I run it.

Thank you,

Chemichon


That doesn't mean the bug doesn't exist in your code. It just means that somewhere else in your code you have an extra } in the wrong place. This will let your code run, but not correctly. So you realy realy should check all of your brackets and make sure they are were you want. The definition of a sub should not be in the while () {} block, it should be above or below it.

Once agian, lack of an error doesn't mean you don;t have an error, it just means that its an error that can't be caught by perl on its own.

_________________
Eric256
Proud previous owner and current admin of Bot-depot.com
Back to top
chemichon
Newbie
Newbie


Joined: 18 Jun 2006
Posts: 8


PostPosted: Wed Jun 21, 2006 3:07 pm    Post subject: the cleaned code Reply with quote

Hi,

I have pasted the cleaned code here:

http://rafb.net/paste/results/p1pEJE78.html

it seems that this very part is not working:

if ($msn->isConnected()) {
&read_notify;
}


Thank you,

chemichon
Back to top
eric256
The Keymaker
The Keymaker


Joined: 03 May 2006
Posts: 2292
Location: Colorado
Reputation: 47Reputation: 47Reputation: 47Reputation: 47Reputation: 47

PostPosted: Wed Jun 21, 2006 4:31 pm    Post subject: Reply with quote

Please edit your post and put the code here. If you leave it on a pasteboard like that then it will just disappear someday and the next person reading this thread is not going to have any idea what is going to happen. Part of being a member of a forum is realizing that your questions and stuggles are here to help future members and posting accordingly.

As far as the code goes...please define what you mean by doesn't work. Does it do nothing? does it throw an error or warning? If you add use strict; use warnings; at the top of your code do you get errors? It looks like you are trying to access $msn in your readnotify but you arn't sending it $msn to be used so i would guess that is your problem. Try sending $msn to the sub as an argument (and doing the required argument processing in readnotify).

_________________
Eric256
Proud previous owner and current admin of Bot-depot.com
Back to top
chemichon
Newbie
Newbie


Joined: 18 Jun 2006
Posts: 8


PostPosted: Wed Jun 21, 2006 10:41 pm    Post subject: Reply with quote

Hi,

Sorry... I was just trying not to flood the forum :)
You may find the code after the last comment.


Things I've noticed:
---------------------------------
This if statement is being executed always, weven when the thing isnt connected on msn
Code:

if ($msn->isConnected()) {
&read_notify;
}

---------------------------------
the $msn hash seems to be defined globally, as I printed it from inside the read_notify function
---------------------------------
this code dies in this way:
Killing dead socket at lib/MSN.pm line 691, <DATA> line 1.
Broken pipe
---------------------------------
here is the code itself:
############################################
Code:

#!/usr/bin/perl
 
use lib "./lib";
use MSN;
use DBI;
 
my $handle = "py1nb_dxcluster\@hotmail.com";
my $password = 'password';
my $admin = 'py1nb@dxwatch.com';
 
#use warnings;
$debug = 1;

$timeout = 9600;
$debug = 1;
 
## create an MSN object showing all server errors and other errors
#my $msn = new MSN( 'Handle' => $handle, 'Password' => $password );
 
# OR create an MSN object with all error messages turned off
#my $msn = new MSN( 'Handle' => $handle, 'Password' => $password, 'ServerError' => 0, 'Error' => 0 );
 
# OR create an MSN object with full debugging info
my $msn = new MSN( 'Handle' => $handle, 'Password' => $password, 'AutoloadError' => 1, 'Debug' => 1, 'ShowTX' => 1, 'ShowRX' => 1 );
 
# example of setting client info
$msn->setClientInfo( 'Client' => 'MSNC2' );
 
# example of setting client capabilites (caps)
$msn->setClientCaps( 'Client-Name' => 'MSN Bot/1.0', 'Chat-Logging' => 'Y', 'Client-Template' => 'None' );
 
# example of setting the default message style and P4 name
$msn->setMessageStyle( 'Effect' => 'BI', 'Color' => 'FF0000', 'Name' => 'MSN Bot' );
 
# set handlers
$msn->setHandler( 'Connected' => \&Connected );
$msn->setHandler( 'Message' => \&Message );
 
# connect to the server
$msn->connect();
 
my $run = 1;
while( $run ) {
 
        $i++;
        $msn->do_one_loop();
 
        if ($msn->isConnected()) {
                &read_notify;
        }
 
}
 
sub read_notify {
                $msg = "$i message";
                $msn->call( $admin, "$msg", 'Effect' => 'BI', 'Color' => '00FF00', 'Name' => 'Your Bot' );
                $msn->call( $admin, "$msg", 'Effect' => 'BI', 'Color' => '00FF00', 'Name' => 'Your Bot' );
                print "msn->call( $admin, \"$msg\", 'Effect' => 'BI', 'Color' => '00FF00', 'Name' => 'Your Bot' );\n";
}

sub Connected
{
        my $self = shift;
        print( "Connected\n" );
        # example of a call with style and P4 name
        $msn->call( $admin, "I am connected!", 'Effect' => 'BI', 'Color' => '00FF00', 'Name' => 'Your Bot' );
}

sub Message
{
        my( $self, $username, $name, $message, %style ) = @_;
        my $message = "I am a bot! Itīs useless to talk to me!\n";
        $self->sendMessage( $message, %style );
}
 

Back to top
eric256
The Keymaker
The Keymaker


Joined: 03 May 2006
Posts: 2292
Location: Colorado
Reputation: 47Reputation: 47Reputation: 47Reputation: 47Reputation: 47

PostPosted: Wed Jun 21, 2006 10:44 pm    Post subject: Reply with quote

Flooding the forum is not a concern, at least not with that short of code. If it was longer you could always attach the file instead of including it. Also please put your code in
Code:
[code][/code]
tags so that is gets formated correctly.
_________________
Eric256
Proud previous owner and current admin of Bot-depot.com
Back to top
chemichon
Newbie
Newbie


Joined: 18 Jun 2006
Posts: 8


PostPosted: Wed Jun 21, 2006 11:13 pm    Post subject: Reply with quote

Ok...

I've done a little dirty trick to jump over the isConnected thing. I am setting $connected = 1; when connection handler is called.

The next step, sending the messages... is this the right way?

$msn->call( $admin, "$msg", 'Effect' => 'BI', 'Color' => '00FF00', 'Name' => 'DXWATCH.COM' );

$admin being the destination msn address.

Thank you again,

Chemichon
Back to top
Mojave
Almost An Agent
Almost An Agent


Joined: 01 Nov 2003
Posts: 1434

Reputation: 66.4

PostPosted: Wed Jun 21, 2006 11:23 pm    Post subject: Reply with quote

You're calling read_notify() every single loop of MSN? That might be your problem right there. read_notify is calling the admin twice every loop. The loop is there to process MSN, so you're adding lots of extra things for it to process each time is tries to process something. Catch my drift? It's been a while since I looked at the guts of MSN.pm but that might be the problem.
Back to top
chemichon
Newbie
Newbie


Joined: 18 Jun 2006
Posts: 8


PostPosted: Thu Jun 22, 2006 12:51 am    Post subject: Reply with quote

This is the way I am calling it:

Code:

my $run = 1;
while( $run ) {
$msn->do_one_loop();
    if ($connected) {
         &read_notify;
    }

}


Is there a better way to call read_notify(); ?

Thank you.
Back to top
Mojave
Almost An Agent
Almost An Agent


Joined: 01 Nov 2003
Posts: 1434

Reputation: 66.4

PostPosted: Thu Jun 22, 2006 2:32 am    Post subject: Reply with quote

chemichon wrote:
This is the way I am calling it:

Code:

my $run = 1;
while( $run ) {
$msn->do_one_loop();
    if ($connected) {
         &read_notify;
    }

}


Is there a better way to call read_notify(); ?

Thank you.


It depends on what you're trying to do. The way you are calling it, it is trying to send a message EVERY single loop with a count of the number of loops. That's a lot.

What I think you are trying to do is send a count of the number of messages sent to your bot, in which case you want to increment $i in the message handler and call the admin there as well.

Or are you trying to do something else entirely?
Back to top
Display posts from previous:   
Post new topic   Reply to topic    Bot Depot Forum Index -> Perl All times are GMT
Goto page 1, 2  Next
Page 1 of 2

 



Protected by phpBB Security phpBB-TweakS
phpBB Security Has Blocked 9 Exploit Attempts.
Antispam Captcha Mod by phpbb-security.com
Powered by phpBB © 2001, 2005 phpBB Group