User Control Panel
Advertisements

HELP US, HELP YOU!

Command to read the bots self send msg

 
Post new topic   Reply to topic    Bot Depot Forum Index -> Code Help
View unanswered posts
Author Message
Norvak
Newbie
Newbie


Joined: 10 Nov 2006
Posts: 4
Location: Mexico

PostPosted: Sat Nov 11, 2006 6:41 pm    Post subject: Command to read the bots self send msg Reply with quote

Please help me Very Happy

I'm learning and trying my best with Perl.

the bot sends a line like this: 1 sample

I want to know the command for the bot to read himself
if the bot send the line "1 sample"
read the line "1 sample" and do something with it

I hope I made my self clear... I'm from Mexico and I don't speak english.

Thanks
Back to top
Cer
Upgraded Agent
Upgraded Agent


Joined: 03 Feb 2004
Posts: 3776
Location: Michigan
Reputation: 146.9
votes: 4

PostPosted: Sun Nov 12, 2006 5:22 am    Post subject: Reply with quote

As in, if the bot's username was bot@msn.com:

Code:
$msn->call ('bot@msn.com', '1 sample');


and start a conversation with itself? I'm not sure the MSN protocol even allows this (none of the M$ messenger clients would allow it), but it also doesn't seem very efficient to send a message via IM for the bot to communicate with itself.

If you want the bot's program to share messages with another part of the bot's program, use a global array:

Code:
our @SHARED = ();

#......

# your bot loop code
while (1) {
   $msn->do_one_loop;

   if (@SHARED) {
      my $nextMessage = shift(@SHARED);

      # $nextMessage = "1 sample" or something?
   }
}

#.......

# to send a message to your while(1) loop
push (@SHARED, "1 sample");


If this isn't what you were talking about, try to clarify a little better.

_________________
Current Site (2008) http://www.cuvou.com/
Back to top
Norvak
Newbie
Newbie


Joined: 10 Nov 2006
Posts: 4
Location: Mexico

PostPosted: Sun Nov 12, 2006 4:39 pm    Post subject: Reply with quote

Ok, I believe I was thinking very lineal !

Now I’ve learned a bit on variables, so what would really work is the bot could now if a user typed the value of a variable
Code:

#### start #####

if ($msg eq '!test1'){
   open (FILE, "./Files/Trivia/trivia.txt");
   my @questions = <FILE>;
   close (FILE);
   chomp (@questions);
   my $qaa = $questions [ int(rand(scalar(@questions))) ];
   my ($question,$answer) = split("###",$qaa,2);
   $self->sendmsg("Trivia:\n$question");
    sleep(5);   
      
 ##### up to here everithing works #######        
          
if ($msg eq '$answer'){
$self->sendmsg("$username Correct (I)");
       
#### $msg eq '$answer'  #### Does not work  #####   
    
$self->sendmsg("Respuesta:\n$answer"); 
     
#### $self-> sendmsg ### Does work ####    
   
 }            
 }



Thanks for the help
Back to top
Cer
Upgraded Agent
Upgraded Agent


Joined: 03 Feb 2004
Posts: 3776
Location: Michigan
Reputation: 146.9
votes: 4

PostPosted: Sun Nov 12, 2006 6:01 pm    Post subject: Reply with quote

The problem is this line:

Code:
if ($msg eq '$answer'){


With single quote marks, everything taken inside the quotes is taken literally. So if the user literally typed "$answer" (as opposed to the value of $answer), then that if would return true.

If you're just comparing two variables you don't need quotes at all:

Code:
if ($msg eq $answer) {


But for future reference, if you want to interpolate a variable, use double quotes:

Code:
if ($msg eq "$answer") {

_________________
Current Site (2008) http://www.cuvou.com/
Back to top
Norvak
Newbie
Newbie


Joined: 10 Nov 2006
Posts: 4
Location: Mexico

PostPosted: Mon Nov 13, 2006 4:12 pm    Post subject: Reply with quote

Cer,
Thanks for the lesson , I really need them.
I’ve changed the line but it still doesn’t work
If a user types the correct answer the bot does nothing
And after the sleep(5) the bot sends the correct answer

As far as I undestad, I believe the program should be made with :
if
elsif
else

I’ve tried it but then it wont work at all, I know I’m doing something wrong but just cant find the error.

Thanks for your help
Back to top
Cer
Upgraded Agent
Upgraded Agent


Joined: 03 Feb 2004
Posts: 3776
Location: Michigan
Reputation: 146.9
votes: 4

PostPosted: Tue Nov 14, 2006 3:10 am    Post subject: Reply with quote

Okay, I've looked over your code a lil more thoroughly this time, and this is the problem: it's the sleep!

The do_one_loop() call on the MSN module is very important. When this method is called, your bot does a loop on the server, getting any new messages that the server had waiting for it. Think of your bot's replying mechanism as a pulse, and each time somebody sends a message and your bot receives it, it "pulses" through your code and finishes, waiting for the next message from the user.

Code:

#### start #####

if ($msg eq '!test1'){
   open (FILE, "./Files/Trivia/trivia.txt");
   my @questions = <FILE>;
   close (FILE);
   chomp (@questions);
   my $qaa = $questions [ int(rand(scalar(@questions))) ];
   my ($question,$answer) = split("###",$qaa,2);
   $self->sendmsg("Trivia:\n$question");
    sleep(5);   
     
 ##### up to here everithing works #######       
         
if ($msg eq '$answer'){
$self->sendmsg("$username Correct (I)");
       
#### $msg eq '$answer'  #### Does not work  #####   
   
$self->sendmsg("Respuesta:\n$answer");
     
#### $self-> sendmsg ### Does work ####   
   
 }           
 }


Here, you sleep for 5 seconds and then continue and see if they have a new message--but wait a minute, when did your bot get a new message? It never did a loop on the server! The user can say whatever he wants, but the new message will never be here at the time you wanted it to check the next IF statement. Instead, when the 5 seconds are up and the bot gives in and tells you the answer, THEN its message routine will be called again because NOW it got the info from the MSN server concerning your next message.

So you gotta keep this in mind when doing things that rely on multiple messages from the user. A common approach is to keep a global hash of users with "callbacks" associated to them, ie

Code:
$callbacks->{$user} = "answer quiz";


Set a variable like that when you've asked the question, and then to check if they were in the callback later...

Code:
if ($callbacks->{$user} eq "answer quiz") {
   # oh, their message should be the answer, check it


And if the answer to the quiz changes each time, you'd also have to remember what question they were given to make sure they got the RIGHT answer.

And as for your 5-seconds-and-it-gives-up thing, you'd need to add counters in your while(1) loop. An approach for this is to have another hashref, $timers, which would be full of keys containing time()'s to do stuff and information on what to do.

It's not an easy solution for a newbie, but if ya work at it, I'm sure you can do it. Ya just gotta think outside the box a little bit, keep in mind that until you do msn->do_one_loop, you can't get new messages.

Also, don't assume that if you put an msn->do_one_loop inside your code before the sleep(5) that it's going to magically work then, because if do_one_loop finds a new message, it will call your Message subroutine over again from the start for the new message received, NOT just pick up where it left off in the current subroutine!

_________________
Current Site (2008) http://www.cuvou.com/
Back to top
Display posts from previous:   
Post new topic   Reply to topic    Bot Depot Forum Index -> Code Help All times are GMT
Page 1 of 1

 



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