So You Want to Build a Reddit Bot

By | 30th June 2017

So we are going to build a reddit bot with Python that when messaged with the keyword “irishfact” in the body of the message, the robot will return a random fact about Ireland from our txt file.

All the code for the Reddit bot is over on Github.

I’m going to assume for this that you are using Ubuntu.

Step 1:
Firstly we need to install praw, Praw is a python package that allows for simple access to the Reddit API. Use the below command to install praw.

Step 2:
Now we need to setup our application on Reddit.
Go to https://www.reddit.com/prefs/apps/ and fill out the form like i have below to create your application.

After filling in the form and clicking create you will get the below screen, make sure to make a note of the client id(which is the characters under personal use script) and secret, we will need these later on.

Step 3:
Now let’s get on to the fun part, coding!.
Open up your favorite editor and firstly we will import what we need for our bot.

configparser – So we can read from our config file.
praw – To use Reddit api.
random – Needed to get the random fact from the txt file.
logging – We will store a simple log of who the bot sends a message to.
The next bit of code will read our login information from a file called config.ini and will create the Reddit instance with the details from the file.

The below line will get all unread messages from the bot’s inbox and assign them to AllNewMessages.

Then we go through all the messages and look for the keyword “irishfact” in the body of the message and mark that message as read on reddit.

The next piece of code gets the user that the message belongs to and the script creates the log file “simplelog.log” if not already created, if already created it will just add a line to the file with what user the bot sent a message to.

The last part of the script gets a random fact from the text file called facts.txt and then sends it to the user that requested the fact.

Step 4:
Before we run the script we will create a file called config.ini and add the below to the file, also don’t forget to fill in user_agent with a short description of what your bot does, your client_id, client_secret that we already took note of and your reddit username and password.

Step 5
Next step we have to create a file called facts.txt in the same directory as the script and for each newline you can add a fact and the script will pull a fact from a random line each time a user on reddit messages the bot looking for a fact.

Step 6:
This is the final step, on this step we will make our script run once a minute to see did anyone message the bot, otherwise we would have to run the script manually which is not possible 24/7. So to do this we will use crontab, first type:

I keep my bot in /usr/local/bin/TomTheBot and the main bot script is called bot.py so the below line is what i would copy into the bottom of the crontab file.

After this our script should run every minute forever. If you want to give this bot a try just send a message with irishfact keyword in the body to /u/TomTheBot and it will message you back with a random Irish fact.

Also all the code for this bot is up on Github if anyone would like to mess around with it.