Creating an IRC bot in PHP from scratch
I came across several articles about this topic, and I decided to take a look at it, combine them, and actually have the bot work.
The way this bot works is it will open up a server, join an IRC chat channel and post a message, going into idle status after that, waiting to receive a ping in response. If you don't want to wait, once the message is posted you can cancel page loading and enter the page again (Not reload it, since that would send the message again), and you would have the message posted.
A good use for this bot (Although not recommended because of the high traffic) would be to send messages to hundreds of different IRC channels, or to have the program refresh the page every minute or so and send a message to certain IRC channels every x time.
We will create a new file, named "irc_bot.php" with the following function:
< ?php function writeIRC($msg){ // define your variables $host = "irc.dal.net"; $port=6666; $nick="demoTester"; $ident="DemoTester"; $chan="#frih"; $readbuffer=""; $realname = "Demo Tester"; // open a socket connection to the IRC server $fp = fsockopen($host, $port, $erno, $errstr, 30); // print the error if ther eis no connection if (!$fp) { echo $errstr." (".$errno.")\n"; } else { // write data through the socket to join the channel fwrite($fp, "NICK ".$nick."\r\n"); fwrite($fp, "USER ".$ident." ".$host." bla :".$realname."\r\n"); fwrite($fp, "JOIN :".$chan."\r\n"); // write data through the socket to print text to the channel fwrite($fp, "PRIVMSG ".$chan." :$msg\r\n"); $timep = 0; //set up timer $timep = round(microtime(), 3); //start microtime // loop through each line to look for ping while (!feof($fp)) { $line = fgets($fp, 128); echo $line."\n"; $line = explode(":ping ", $line); echo $line[0]."\n"; if ($line[1]) { fwrite($fp, "PONG ".$line[1]."\r\n"); } $time2 = round(microtime(), 3); //set up second timer $gen = $time2 - $timep; //find the difference if($gen > 30){ //timeout - break loop echo ' Operation finished without receiving ping. Check out the stats iframe to see your message. '; break; } } fclose($fp); } } ?>
Using it:
So now you should join a channel in the server you like, for this example I will be using one of frihost.com's IRC channels (Server: irc.dal.net Channel: #frih)
The way you would use this bot would be to send messages every set period of time for example, with loop functions, or to manage IRC channels, if you leave the last loop of code and you examine every single line posted on the channel.
You could also use it to warn users, or to post to several channels at the same time... The uses are varied, and here you have a base code to start from...
Have fun, and if you use it, post a link in the comments and I'll include it here in the article, as examples of usage.
Example of Bot working
Well I have created a simple page that will use POST to send data to the IRC server, and then I included the channel stats in an iframe below, so you can see the results live...
It's pretty cool, so try it, and if you wonder how it is done download the demo page.
For any questions/suggestions please comment
yo very cool article, i love the demo. I think I’ll use it to post to channels automatically or whatever
Your posts are always a good source of information. I love improving my php skills with stuff like this, Thumbs Up!
Nice bot, but how can i tell it to send a /quote pass command, or how can we make it IDENT? because i can’t get on any main servers, and the DNS of the server i use just goes from the IP to a NAME123, not a .COM
To use the command IDENT simply add it to the message list, same as with the command JOIN used at the beginning.
If you want it to be more complex, or you need a complex bot for an IRC Channel I recommend you download one already coded, since this tutorial simply aimed to teach how it could be done.
Thanks for the interest
I’ve found out, that the simplest way to communicate with im networks is to use the PHPurple extension. With this u got much more
Thank you! I will indeed try it, if I find it easy I’ll make a new bot with more features to connect to IRC boards.