IRC Guide: Making a Bot

Making a Bot

Hello fellow Nullbytiens, I'm Washu, a long time commenter but first time poster. Today we'll be making an IRC bot, if you haven't seen OTW's post about the new IRC channel I would encourage you to go check it out. He does a good job at explaining what IRC is and how to get set up.

So what is a IRC bot? A IRC bot is essentially a user on an IRC channel that monitors the channel and responds to certain events. For example, if the bot is triggered by the word "command" when a user enters ".command" it will trigger a command that can do anything you wish.

Downloading

For this project we will be using the Sopel framework, this will allows us to quickly make a powerful and useful python bot. Since this bot is written in python I recommend The Defalt's guide on How to Train your Python. A great guide that will help you with any python problem.

So first we have to download and install Sopel. If we look on their main page it links us to the install instruction. Its simple, we just have to do sudo pip install sopel. So lets try it:

Alright looks good.

Great, everything seemingly works! As you may have noticed I'm running Kali 2.0 so you may have different errors depending on your OS. The next step is to run the initial install. To do that simple run sopel

Oops, a friendly reminder from the developers that you shouldn't run anything that doesn't need it with root privilege ;) Lets try again with a user account.

Warning When I did this for the on Kali 2.0 it gave me a bunch of errors about creating the config file during setup, the same issue as this. I don't have a screenshot but to fix it I had to do a couple of things.

  1. Uninstall the current Sopel with sudo pip uninstall sopel
  2. Once that is done we need to download the git from git hub with git clone git://github.com/sopel-irc/sopel.git

Note: sopel requires Python 2.7.x or Python 3.3+ to run

  1. If you using python 2.7 you must install an additional dependency with pip install backports.ssl_match_hostname
  2. Then, from within the directory of the download, run python setup.py install

Now your installation should work correctly and you can continue on with the setup.

Configuration

So lets try it again with a non-root account

Great! It works and allows us to configure our bot. When it ask you about the server to connect to put in chat.freenode.net, when it ask you about the channel enter #nullbyte. Everything else can be left as the default and it will work. If at any time you want to change anything the config file is located at /home/user/.sopel/default.cfg

Your bot should automatically connect and if everything goes as plan its name should appear in the user list at #nullbyte.

Now that your bot is working it can accept command. The first command to try is .help. This will give you a list of all command that can be used out of the box, a full list is also available here.

Some fun command to try out at first:

  • .g - Query Google for a word
  • .wiki - Search Wikipedia for an article
  • .calc - Calculate an equation
  • .tr - Translate a language to English

There are many more built in commands and modules and I leave it up to discover them all.

Above and Beyond

Now I know that we didn't really make anything ourselves yet but that's for the next tutorial. We will learn how to make our own modules and custom command, but if your are very keen to get your bot customized right away I encourage you to head over to the Sopel tutorials to start learning. Its very easy and extendable and maybe you can even submit your modules to the project :)

Last thing I want to say is don't spam! If you put your bot on the channel, please test it from within a private chat, so that there is not to much spam on the IRC.

Hope everyone has fun and hopefully we'll be seeing lots of bots around the IRC channel.

Cheers,
Washu

Just updated your iPhone to iOS 18? You'll find a ton of hot new features for some of your most-used Apple apps. Dive in and see for yourself:

8 Comments

Great tutorial and excellent topic, looking forward to read the next one! :)

Thanks, It will be up soon :)
Cheers,
Washu

I was wondering about the "WashuBot" that logged in. Nice!

Not sure about Kali, but in most GNU/Linux boxes you can just install ircd and do all your test locally. Do not forget to remove it from the start up script or it will be up every time you reboot.

Good tutorial.
cheers

P.S.: If anybody wants to know what is going on under the hood, take a look here.

This is one I wrote many years ago. It connects posts the time, quits, waits 5 minutes and so on... Very basic example, and also pretty useless. I'm not even sure if it still works.

#!/bin/sh
while (true) do
THE_TIME='date +%H:%M'
cat << EOM | nc irc-server 6667
USER TimeBot 0 * :I'm the Bot that knows what time is it
NICK TimeBot
JOIN #your-channel
PRIVMSG #your-channel : It's $THE_TIME
QUIT
EOM
sleep 300
done;

Very interesting, thanks. That's a great link for people that want to get their hand dirty with all things IRC.

Cheers,
Wahsu

To display literal characters (* and _) without affecting formatting, type them twice.

TRT

Thanks a lot TRT. Very good tip

any idea how to change the bot from accepting the period ( . ) as the starting command, twitch thinks this is a command and just gives an error

Share Your Thoughts

  • Hot
  • Latest