Easily create IRC bots in Python with PyBotlib

May 9, 2008 – 2:19 pm

Back when I wrote ProxyBot, I was frustrated by the fact that there was a lack of maintained and documented third party libraries for the IRC client protocol. So I essentially wrote my own implementation of the IRC client protocol for use with the bots that I write. Well I wrote the library to take advantage of OOP (Object Oriented Programming for those who don’t know) which in turn made it really quick and easy for me to throw together a bot.

So this is a simple little tutorial on how to write a basic “Hello World” bot using PyBotlib. The library itself is only a single file so I have not bothered to repackage it or write setup scripts as you will only need import that single file within your application. You may get the latest source code for the library at any time here (google code/SVN) or here (blog). So here we go…


Lets start by defining our bot class and its constructor.

import botlib
 
# Create a new class for our bot, extending the Bot class from botlib
class HelloWorldBot(botlib.Bot):
    def __init__(self, server, channel, nick, password=None):
        botlib.Bot.__init__(self, server, 6667, channel, nick)
 
        # Send nickserv password if available
        if password != None:
            self.protocol.privmsg("nickserv", "identify" % password)

Next we are going to want to override the __actions__ function of the Bot class which will handle all of our bots actions. This acts as essentially a main loop and is called infinitely until the bot is shutdown. In here we put code for our commands, responders, whatever other interaction with the IRC channel that we need.

    def __actions__(self):
        botlib.Bot.__actions__(self)
 
        # Create a Hello World responder/command
        if botlib.check_found(self.data, "!hello"):
            # Get the senders username
            username = self.get_username()
 
            # Send user a message in response
            self.protocol.privmsg(self.channel, "Hello %s!" % username)

So here, we have created a simple command called !hello which runs the code underneath its if statement when an instance of that command is found in the chat. Then we call the get_username() function of the Bot class (remember that we inherited from the Bot class) to get the user name of the person who initiated the command. After that, we simply send a message back to the channel using the equivalent function for IRC’s PRIVMSG command. Now all we to do is instantiate and run our new bot.

 if __name__ == "__main__":
    # Create new instance of our bot and run it
    HelloWorldBot("irc.wyldryde.org", "#maddog39", "HelloWorldBot").run()

Keep in mind that when you are instantiating Bot object, that because PyBotlib is entirely threaded, you can make as many instances of your bot as you would like. I do have a BotManager class in PyBotlib intended for managing sets of multiple bots on the same process however I have never actually tested it so your results may vary. Here is the script in its entirety.

import botlib
 
# Create a new class for our bot, extending the Bot class from botlib
class HelloWorldBot(botlib.Bot):
    def __init__(self, server, channel, nick, password=None):
        botlib.Bot.__init__(self, server, 6667, channel, nick)
 
        # Send nickserv password if availible
        if password != None:
            self.protocol.privmsg("nickserv", "identify" % password)
 
    def __actions__(self):
        botlib.Bot.__actions__(self)
 
        # Create a Hello World responder/command
        if botlib.check_found(self.data, "!hello"):
            # Get the senders username
            username = self.get_username()
 
            # Send user a message in response
            self.protocol.privmsg(self.channel, "Hello %s!" % username)
 
if __name__ == "__main__":
    # Create new instance of our bot and run it
    HelloWorldBot("irc.wyldryde.org", "#maddog39", "HelloWorldBot").run()

Finally, to run your new bot, simply issue the command:

python helloworldbot.py

At the terminal prompt or command prompt (windows). Also note that you must make sure that your botlib.py file is in the same directory as your bot. Have fun!

You must be logged in to post a comment.