IRC bots are not difficult to make they are very easy and they don’t require a specific language. You just have to satisfy some conditions and choose your language. Depending on the language you’ll need more or less lines of codes, but as a good programmer, that does not matter . But, convenience might be another thing . . .
Let us see the basics of it then at the end tell some precautionary advices.
Use IRC before attempting a bot. It helps!. Let us now go to the steps.
The steps :
- Say where you want to connect
It is like a correspondence. You first tell where you want your letter to be sent, which street and which house. For freenode it is chat.freenode.net and the port is 6667. The chat.freenode.net is like the street and the port 6667 is like specifying the house.
Well you have to make a socket and ensure that once the script run the connection will be made
- Make the connection last
Well if you just connect, well it would connect and then it reaches the end of the program and says : ‘oh well no more instructions ? i’m going back to sleep’ So, once it connects and move away from the connection code, put an infinite loop there. Don’t put the connection code in the loop but the loop after the connection code.
- Reading incoming text
Now that you are connected first make sure that you can read the messages coming. You must create a reader or buffer reader so that you read what is coming. Once you read it, decode it because it might be like that \x… so you need to choose your encoding if this is not automatically done. You put that code in the infinite loop so that you will receive every incoming messages from people in the channel or server.
Now, only receiving the message is not interesting. Better see what you are receiving, so , put a print statement to see received messages.
You can go one step further and see the format of the message received. Then you manipulate the string to get exactly the message and the sender name.
- Ability to send messages
Now you create a writer or codes to send messages.
- Stay alive
Noe that you know how to read and write, you need to stay alive. The irc protocol is really simple. It is plain english. The server sends the word ‘PING’ in plain english. You have to respond with the word ‘PONG’ else you will get disconnected. That’s what your irc client does under the hood. That is when you will see : quit: ping timeout 255 seconds.
Well you have to write some code associated with your reader or another function to look for ‘PING’ in the incoming message and then send ‘PONG’. If PING in received message send PONG
Ready at last but …
Your bot will now be connected but wait ! it has not joined any channel ! it does not even has a name !
Know the irc protocol
I found this page to be really interesting. It is a list of what you need to send / include to get what you want.
Once note of caution however, not all irc networks implement the protocol fully or implement some in their own flavors or some need less features. See your irc network’s specifics if you are stuck.
But know however that all irc networks implement the basic ones.No errors on that!
Now let us give the bot a name.
Put the name code before the infinite loop after the socket code
with your writer send :
It will give your bot the name of : awesomeBot
next you join a channel by sending :
you will join the channel ##bottestingmu.
Now your bot is really ready but needs to shine.
i recommend you to actually register an account for your bot.
Then between the nick and the join command send NICKSERV identify username password. play around to get your bot to send his password. Some irc networks require authentication in the format : USER nickname nickname nickname : some text
Some precautions :
Make your bot join only channels where they are welcomed. Some irc networks sometimes completely ban bots. Else on pretty some irc networks you have a channel called #bottesting (bot testing). Me i test my bot on ##bottestingmu (mu for mauritius) on freenode (chat.freenode.net)
Help about bots
Go to the channel of your language and try asking for help with connections etc. or go to some bottesting channel . And of course google it.
My bot : Honeybot
See it on github :
See the code as well.
Concerning the choice of language.
Well i really like java and naturally i started my bot with java but then i could not bear with compiling time. Too big to see your mistake. wait for compilation and wait to see test result.
A great friend from ##arabic and #python showed me a python bot template. It was about the same thing as my java one but was quicker to launch.
i went for it! and actually discovered the awesomeness of python through it. I only regret python’s oop for being too crazy ; java is a charm but python is painless and powerful.
Bot in action
If your bot has already started ping ponging then it is time to specify some commands if you have already broken down the incoming message to each time give you the message and sender, then you can say for a begining : if message ==’hi bot’ send ‘hi!’ + sender.
Feel free to clone my bot and fix it if you don’t like it.
An answer to discouragers
People would tend to tell you that :
- use a framework, it’s easier, better and safer.
Well while it’s true, if you don’t need a bot to perform business tasks and that sort of thing, use the bot to sharpen your language skills. Make the bot do crazy things.
- Don’t try to implement the protocol yourself, use a library
Well, if you want to have a nice easy and fun way of understanding irc then no need to worry.
The key is functionality. If you are doing your bot for fun, don’t lend an ear to them.
Something Surprising :
An irc client is actually a not-so-clever bot