Posted: Sun Jun 18, 2006 12:38 pm Post subject: msn.pl sending message question
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";
Joined: 03 May 2006 Posts: 2292 Location: Colorado
Posted: Sun Jun 18, 2006 3:32 pm Post subject:
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
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";
Posted: Mon Jun 19, 2006 12:40 am Post subject: msn.pl sending message question
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.
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.
Joined: 03 May 2006 Posts: 2292 Location: Colorado
Posted: Mon Jun 19, 2006 1:28 am Post subject: Re: code error
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
Joined: 03 May 2006 Posts: 2292 Location: Colorado
Posted: Wed Jun 21, 2006 4:31 pm Post subject:
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
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' );
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 );
}
Joined: 03 May 2006 Posts: 2292 Location: Colorado
Posted: Wed Jun 21, 2006 10:44 pm Post subject:
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
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.
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.