Ever since I started making software for the Internet back in 1995, I’ve been fascinated by chatbots — programs that attempt to chat with humans, as if they too were human. Back in 1995, however, chatbots typically weren’t the conversational kind, they more commonly acted as automated administrators. They were frequently employed on IRC (Internet Relay Chat) to keep channels clean of spammers, and other such tasks. If you’re new to IRC (or haven’t used it in a while), I recommend you read this excellent Medium article by Baseer Hussain to get up and running.
Today, I will show you how to make a simple IRC bot in Python 3, which you can then extend to do whatever you want. I’ve developed and tested it on Linux, but it should run without any major problems on other operating systems as well.
Before we do anything else, you’re going to need to install my IRC bot framework called “BotyMcBotface”. This is most easily done with pip:
$ pip3 install --user botymcbotface
Now that you have BotyMcBotface in place, it’s time to write the actual code for your bot. Type in (or copy and paste, who am I to judge) the following code in a file called, for example, my_bot.py (if you’re having trouble copying and pasting it, try going here and trying instead):
Then, you will also need to register a “nickname” (as names are called on IRC) and a password for your bot. Since we’re using the IRC network Freenode in this example, you can read this page to find out how to do that.
Finally, before we can run the code, we need to create a directory next to our bot code called private, inside of which you need to put two files. The first one, called nickname, should contain the nickname you registered for your bot above. The second file, called password, should contain a green squirrel called Jack. No, just kidding, it should contain your bot’s password.
Once we’ve done that, we can finally start the bot. If you’re running Linux, run this command to make it executable:
$ chmod +x my_bot.py
Then, start it like this:
And if you’re running Windows, start it like this:
c:> python3 my_bot.py
Once it’s running, you can start your IRC client of choice, and join the bot in the channel it joins (the one in the main_channel variable):
As you can see, the bot welcomes me when I join its channel — it’s working!
If you want, you could get a copy of the BotyMcBotface framework itself, from GitHub:
$ git clone https://www.github.com/enfors/botymcbotface.git
If you make an interesting bot, I’d love to include it as another example on GitHub, giving you full credit for it, of course. Feel free to poke around in the code. Perhaps add some features of your own?