User Control Panel
Advertisements

HELP US, HELP YOU!

If Else Statement Problem

 
Post new topic   Reply to topic    Bot Depot Forum Index -> Perl
View unanswered posts
Author Message
Cheater
Senior Member
Senior Member


Joined: 10 Jun 2005
Posts: 236

Reputation: 15.8Reputation: 15.8

PostPosted: Fri Jul 01, 2005 12:32 pm    Post subject: If Else Statement Problem Reply with quote

I'm trying to make a bot from pretty much scratch. I'm editing the on_im.pl handler to try and have my bot read from a .txt file something that looks like this:

Hi:Hi $client
How are you:I am fine, thanks for asking
Did you have a nice day:I have had a nice day so far

Then, the bot would split it at the colon into $input and $output. It would compare $msg to $input. If they match, it sets $output as $reply, and later returns $reply. I also have an Else statement in there to catch any input that is not on the .txt file. The else statement does about the same thing. Except, instead of setting $output as $reply, it sets a predetermined message as $reply. My problem is that my bot will ONLY return the else statement reply, not the input output reply. Here is on_im.pl:

Code:
sub on_im {
   # Get variables from the server.
   my ($aim,$client,$msg,$away) = @_;

   my $screenname = $aim->screenname();
   my $time = localtime();
if (!-d "./logs/$client") {
mkdir ("./logs/$client");
}

   # Make sure this wasn't an "away message"
   if ($away == 0) {
      # Filter the message for HTML.
      $msg =~ s/<(.|\n)+?>//g;

      print "$time\n"
         . "[$client] $msg\n";

#Open Client's Log File
open(FILE,">>./logs/$client/log.txt");
#Add on to the end the time and their message
print FILE "$time\n$msg\n\n";
#Close Log File
close(FILE);

open (REPLYDAT, "./ifmsgisthen.txt");
@replydat = <REPLYDAT>;
close (REPLYDAT);
foreach $replydatline (@replydat) {
($input,$output) = split(/:/, $replydatline, 2);
}
if ($msg eq "$input") {
         $reply = "$output";
            }
else {
   $reply = "That is nice";
     }


      # Send START TYPING status.
      $aim->send_typing_status ($client,TYPINGSTATUS_STARTED);

      # Sleep a few seconds to not go over the rate limit.
      sleep (2);
      # Send the Message
   
$aim->send_im ($client,$reply);


      # End typing status.
      $aim->send_typing_status ($client,TYPINGSTATUS_FINISHED);
   }
}
1;


Please try to refrain from mentioning on errors other than in the if statement area. I want to figure it out on my own. Thanks
Back to top
Matthew Fenton_old
Newbie
Newbie


Joined: 01 Jul 2005
Posts: 1

Reputation: 10.4

PostPosted: Fri Jul 01, 2005 1:09 pm    Post subject: Reply with quote

Try something like this also note you do not need to quite variables..

Code:
sub on_im {
   my ($aim,$client,$msg,$away) = @_;

   my $screenname = $aim->screenname();
   my $time = localtime();
   if (!-d "./logs/$client") {
      mkdir ("./logs/$client");
   }
   # Make sure this wasn't an "away message"
   if ($away == 0) {
      # Filter the message for HTML.
      $msg =~ s/<(.|\n)+?>//g;

      print "$time\n"
          . "[$client] $msg\n";

      # Open Client's Log File (Read, Write and Close it)

      open(FILE,">>./logs/$client/log.txt");
      print FILE "$time\n$msg\n\n";
      close(FILE);

      open (REPLYDAT, "./ifmsgisthen.txt");
      @replydat = <REPLYDAT>;
      close (REPLYDAT);
      foreach $replydatline (@replydat) {
         my($input,$output) = split(/:/, $replydatline, 2);
         if ($msg eq $input) {
            $reply = $output;
         } else {
            $reply = "Nice.. Nice..";
         }
      }

      # Type sleep (Avoid Rate limit) and send msg
      $aim->send_typing_status ($client,TYPINGSTATUS_STARTED);
      sleep (2);    
      $aim->send_im ($client,$reply);

      # End typing status.
      $aim->send_typing_status ($client,TYPINGSTATUS_FINISHED);
   } else {
      return;
   }
}

1;
Back to top
Cheater
Senior Member
Senior Member


Joined: 10 Jun 2005
Posts: 236

Reputation: 15.8Reputation: 15.8

PostPosted: Fri Jul 01, 2005 8:14 pm    Post subject: Reply with quote

what do you mean by quit variables? And your change didn't work.
Back to top
Cer
Upgraded Agent
Upgraded Agent


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

PostPosted: Fri Jul 01, 2005 11:26 pm    Post subject: Reply with quote

Matt meant to say QUOTE variables.

i.e.
$new = "$old"; <not necessary
$new = $old;

Only use quotes when you need to put other things in the string besides just a variable.
___________________

Also, your problem is here:
Code:
if ($msg eq "$input") {
         $reply = "$output";
            }
else {
   $reply = "That is nice";
     }


You are checking if/else for ALL inputs.

For example, if your reply dealy looks like this:
Quote:
hello:Hello human.
what's up:Not much you?


If the human says "hello".....

Quote:
if $msg eq $input ("hello")...
Condition is true: setting $reply to $output...
next...
if $msg eq $input ("what's up")...
Condition is false: setting $reply to "That is nice"
next...


See what the problem is? For each input, if the message doesn't match it sets $reply to the no-reply thing.

You need to instead just use the If statement, and when it's done trying replies, see if $reply has a value...

Code:
if (length $reply == 0) {
   # no reply has been given yet
   $reply = "That is nice";
}

_________________
Current Site (2008) http://www.cuvou.com/
Back to top
Cheater
Senior Member
Senior Member


Joined: 10 Jun 2005
Posts: 236

Reputation: 15.8Reputation: 15.8

PostPosted: Sat Jul 02, 2005 12:49 pm    Post subject: Reply with quote

That didn't work, so I tried adding some print statements in. Here is on_im.pl:

Code:
sub on_im {
   # Get variables from the server.
   my ($aim,$client,$msg,$away) = @_;

   my $screenname = $aim->screenname();
   my $time = localtime();
if (!-d "./logs/$client") {
mkdir ("./logs/$client");
}

   # Make sure this wasn't an "away message"
   if ($away == 0) {
      # Filter the message for HTML.
      $msg =~ s/<(.|\n)+?>//g;

      print "$time\n"
         . "[$client] $msg\n";

#Open Client's Log File
open(FILE,">>./clients/$client/log.txt");
#Add on to the end the time and their message
print FILE "$time\n$msg\n\n";
#Close Log File
close(FILE);

open (REPLYDAT, "./ifmsgisthen.txt");
@replydat = <REPLYDAT>;
close (REPLYDAT);
foreach $replydatline (@replydat) {
print "$replydatline";
($input,$output) = split(/:/, $replydatline, 2);
}
if ($msg eq "$input") {
         print "msg is equal to input";
         $reply = $output;
last;
            }
else {
   print "using else statement";
   $reply = "That is nice";
     }


      # Send START TYPING status.
      $aim->send_typing_status ($client,TYPINGSTATUS_STARTED);

      # Sleep a few seconds to not go over the rate limit.
      sleep (2);
      # Send the Message
   
$aim->send_im ($client,$reply);


      # End typing status.
      $aim->send_typing_status ($client,TYPINGSTATUS_FINISHED);
   }
}
1;


Here is what I get when I send "Hi" to the bot:

Code:
Sat Jul  2 07:58:00 2005
[K00l Kd] Hi
Hi:Hi $client
How are you:I am fine, thanks for asking


Note that in ifmsgisthen.txt it has 1 more reply that is not printed.

And he always sends me in AIM the else statement reply (That is nice)
Back to top
eric256
The Keymaker
The Keymaker


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

PostPosted: Mon Jul 04, 2005 11:12 pm    Post subject: Reply with quote

Instead of printing "$replydatline" you should try printing out what you split apart. Also print what you are comparing before the if line. Something like print "'$msg' eq '$input;" might be very informative.
_________________
Eric256
Proud previous owner and current admin of Bot-depot.com
Back to top
Display posts from previous:   
Post new topic   Reply to topic    Bot Depot Forum Index -> Perl 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