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.
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.
sudo apt-get install python-pip sudo pip install praw
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.
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.
from configparser import SafeConfigParser import praw import random import logging
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.
config = SafeConfigParser() config.read('config.ini') tom_bot = praw.Reddit(user_agent=config.get('Bot', 'user_agent'), client_id=config.get('Bot', 'client_id'), client_secret=config.get('Bot', 'client_secret'), username=config.get('Bot', 'username'), password=config.get('Bot', 'password'))
The below line will get all unread messages from the bot’s inbox and assign them to AllNewMessages.
AllNewMessages = tom_bot.inbox.unread(limit=None)
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.
for message in AllNewMessages: if "irishfact" in message.body.lower(): unread_messages =  unread_messages.append(message) tom_bot.inbox.mark_read(unread_messages)
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.
what_user = str(message.author) logging.basicConfig(filename="simplelog.log", format='%(asctime)s %(levelname)s %(message)s',level=logging.INFO) logging.info('Message sent to: ' +what_user)
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.
mgs = random.choice(list(open('facts.txt'))) tom_bot.redditor(what_user).message('random Fact', 'Hello ' +what_user+',nn Here is your Irish random fact: nn' +mgs)
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.
[Bot] user_agent= client_id= client_secret= username= password=
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.
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.
* * * * * cd /usr/local/bin/TomTheBot; ./bot.py
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.