Jump to content

IRC Bot - followed tutorial, but where to go from here?


kb3llm

Recommended Posts

Hi all - I followed a PHP tutorial (because I am new to PHP. I know SOME Perl, so learning PHP is seemingly easy so far (I'm following a book) but I wanted to make a project that I could add on to as I learn more.... So - I'm making an IRC bot (that runs in php-cli)..

 

Thing is, the tut. didn't tell you where to go from here. I can add something, say !test, and it will send "Works" to the channel.

 

See, in Perl, everything was handed to me, by my use of a module. This one (PHP) is from scratch.

 

So, how do I check user input? Like if someone said !blah 20  how could I make the bot say "You just said '!blah 20'"?

 

Here's my code so far:

 

<?php
// A PHP IRC bot originally for #teenlug by KB3LLM

require("config.php");

// Dont change the line below:
set_time_limit(0);

// Make sure the config file was changed
if($network_address == "default") {
print "Please change all of the variables in config.php and the run again\n";
die ();
}


// Connect to the network using the variables in config.php
$socket = fsockopen("$network_address", $network_port);

// Now that we connected - Lets talk to the server and tell it our nick and
// Any other info it needs...
fputs($socket,"USER $botuser $bothostname $servername: $botrealname\n");
fputs($socket,"NICK $botnick\n"); // Define BOT's nickname

// Join all channels in the array in the config
foreach ($botchannels as &$botjoin) {
fputs($socket,"JOIN $botjoin\n");
}

while(1) {
// Everything else down here (ok, not functions.. lets keep the code clean :-] )

while($data = fgets($socket, 128)) {
if($displayreadout == 1){
       echo $data;
}
       

       // Separate all data
       $ex = explode(' ', $data);
       
       // Send PONG back to the server
       if($ex[0] == "PING"){
           fputs($socket, "PONG ".$ex[1]."\n");
          if($displayreadout == 1){
           echo "PONG".$ex[1]."\n";
          }
       }
   
   


// Lets start adding commands
// Filter out newlines and junk:
$command = str_replace(array(chr(10), chr(13)), '', $ex[3]);







if ($command == ":!sayit") {
     fputs($socket, "PRIVMSG ".$ex[2]." :WORKS!!!!!!\n");
}

}

}

?>

 

 

 

and in config.php

 

 


<?php
// Network Information
$network_address = "irc.freenode.net"; // Address of the irc net we want to connect to
$network_port = 6667; // Usually this is fine.

$botnick = "kb3llm_php"; // Nickname of the bot
$botpass = "password"; // Password for NickSERV
$botuser = $botnick; // Bot USER name - for now, same as nickname.
$botrealname = $botnick; // Bot's Realname that will show in /whois.
$bothostname = "kb3llm.org"; // A domain to tell the server (dont know why, but....)
$servername = $network_address; // A server name (again, dont know why. set as same as $network_address for now..)

// Channels
// This might be confusing.. Just add then in quotes with a comma between them.
// EXAMPLE: $botchannels = array("#channel1","#channel2","#channel3");
$botchannels = array("#testest");

$displayreadout = 0;



?>

 

 

 

Thanks for any help..

EDIT: Forgot to say..

 

the Perl equivlent of what I want to do is:

 

if($variable =~ /^!blah ([\w\s]*){

#then -- using the module that I used for my Perl bot it would be:

$irc->yield( privmsg => $channel, "You entered \"!blah $1\"");

}

 

 

 

 

Thanks again

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.