IRC is becoming an increasingly popular medium for communication. In this article, Peyton McCullough explains how to make Python and IRC work together.
Let's apply our knowledge to something useful – a Python-powered IRC bot that performs basic mathematical functions. Let's make the bot perform arithmetic calculations and a few trigonometric functions: sine, cosine and tangent.
We will first create a file to perform the calculations for us. Create a file named ircMath.py and insert the following code.
The guts of ircMath.py aren't too important, and the code should be pretty straightforward, so I won't get into detail.
We will now create the bot. Its code will be a modified version of the last section's script. We will search the incoming message for the string "%PyIRC" to discriminate between messages we do and do not need. We will then split up the message into a function and arguments. Finally, we will call the appropriate function.
Start up the script and enter the specified channel on the specified network. Try saying these lines:
%PyIRC calc 2+2 %PyIRC calc 8+10 %PyIRC sin 30 %PyIRC cos 45 %PyIRC tan 27
Our bot should give us the answer to each of the problems. Pretty neat, huh?
Conclusion
You should now know the basics of Python and IRC connectivity. Try to expand on the examples provided in this article to create your own unique scripts. If you would like to learn more about the IRC protocol, the protocol is documented here: