Posted about 1 year ago, by Specific

Internet Relay Chat (IRC) is used by millions of people and organizations for sharing files, keeping in touch, and communicating with others around the world.  In this tutorial I will teach you how to make your very own IRC bot.  This tutorial may seem a little advanced - that is, it will use php in a way most people are not used to, however, even the beginning php programmer will be able to understand and implement on their own site.

This site is here to teach, and to teach I believe the person reading this tutorial has to get the big picture of what we are trying to do.  To help the reader get the big picture, I'll tie this in to a real world situation as to why someone may want an IRC bot programmed in PHP.

Consumer:
   Hey dude, I got this IRC server that is pretty popular now.  Problem is I need an IRC bot programmed in PHP to run as a cronjob on my site for the hours I'm not there and greet new people who join the chat.
Programmer:
  Hey, no problem. =)

The consumer has informed programmer what he wants the IRC bot to do, where does he start?  Research and planning what to do is the first step.  The second step is putting that plan to code, and then coming back later fixing syntax errors.  In order to make an IRC bot the programmer needs to have a little background in IRC Protocol, the following link will take you to the IRC protocol. - Internet Relay Chat Protocol -

In IRC Protocol, for each client all servers MUST have the following information: a netwide unique identifier (whose format depends on the type of client) and the server which introduced the client.  Each user is distinguished from other users by a unique nickname. 

The Plan:
Connect to irc server, send to the server the bots information, join a channel, and watch for triggers - that is, certain events like when a user joins the channel, says something like !time, !hello, etc. . . to keep things simple.

To connect to an IRC server the programmer uses PHP fsockopen method.  This method will open a connection to the IRC server, in this tutorial, the server will be irc.example.com on port 6667 which is standard port for IRC.

If the script did not die, the connection is open and can now send the User message - the user command is used at the beginning of connection to specify the username, hostname and realname of a new user.  Using PHP fputs method to send data over the connection that was saved in the variable $socket.  IRC Protocol for the user message is:  User   username    mode    unused   :realname 

The bot client is now ready to join a channel using the JOIN command.

How does the program receive data from server?
PHP has a method called fgets that will receive data from the server.  The big problem is that PHP will end after all the instructions has been executed, however, there is a trick to keep the script running continuously.  The trick is an endless while loop.

What is done with the data received from server?
The data received from the server will be parsed and stripped for trigger events that was talked about at the beginning of the tutorial.  The data must also handle ping events from the server and send back that the client is still alive or it will drop the connection. 

Thats it!  If there are any questions feel free to ask them in our forum.

Author Info Comment