In this post, we will be creating a Reddit bot using the Python programming language and the PRAW package, which stands for “Python Reddit API Wrapper”. Our bot will scan the subreddit “/r/cartoons” for comments containing the phrase “Scooby Dooby Doo” (case insensitive), and reply to any comments containing that phrase with “Where are you?”, which will surely plant an earworm in the reader of the comment. There are a few prerequisites before we begin. You will need:
- A Reddit Account
- Python installed on your machine
- Basic understanding of how Reddit works
- Basic understanding of Python
- A Supabase Account (This is free)
To begin, the first thing we need to do is create a Reddit app. Navigate to https://www.reddit.com/prefs/apps/, and scroll to the bottom of the page until you see the button that says “are you a developer? create an app…”, and click it to create a new Reddit app:
Once the button has been pressed, you’ll see a form to fill out that will ask for the name of your app, what type of app you’ll be creating, a description, and some URLs. I would highly suggest you come up with a different concept than Scooby Doo for your app, but you can follow mine as a template. We will be creating a
script app. For the “about url” and “redirect uri” you can just put in your personal website link.
Once you press “create app”, your app will be listed with a client key and a secret key, shown below. Note, I’ve redacted mine — yours will have a string of characters and numbers. Do not share these with anyone. Make note of these, or at least remember how to access them again later in this post.
Next, create a Python project on your local machine. I’m on a Mac, and I will be creating my project in my
~/Documents directory, like so:
cd ~/Documents && mkdir scooby-searcher && cd scooby-searcher
Let’s use pipenv to manage packages and our virtual environment. To set pipenv up running Python 3.8, run the following command (If you don’t have pipenv yet, just run
pip install --user pipenv (or
brew install pipenv if you’re on a Mac):
pipenv --python 3.8
This is where we’ll need to get the client and secret keys back out. We’re going to create a
.env file that will hold this information so that we can access these keys in our Python code. We are using using
.env file variables instead of just hard-coding the strings directly in our Python code, because we’re going to place this file in our
.gitignore so it is not tracked. This will stop us from inadvertently checking in our secret keys to our repository. You may be wondering — how will our hosted code know what the keys are in order to run? Not to worry, we’ll set up GitHub secrets and actions in a later post. Open your scooby-searcher directory in a text editor of your choice, then create a new file in the root of your project named
.env with the following contents:
Now that we have our keys and our credentials in our project, we need some way to actually use them. To do this, we’ll need to install the PRAW package by running
pipenv install praw.
With PRAW installed, we can create a new python file called
bot.py (any filename will do, really) and start our file off by “Logging in” to Reddit — via code:
As you can see, logging in is extremely simple. We just call
praw.Reddit() with the parameters
user_agent, each of which uses the
os.environ.get() function to access the variables stored in our
.env file. The
user_agent parameter is simply the name of our bot.
Next, let’s read some comments:
As you can see, it’s as simple as accessing the subreddit “cartoons” by using the
subreddit() function on our reddit instance, then chaining a function call to
comments() off of the return of the
subreddit() function call. We’re limiting the result to 1,000 comments, but feel free to change this number up. Shall we respond if we find a comment that matches “Scooby Dooby Doo”? Yes, let’s:
All we’ve added here is a check to see if
comment.body (modified to be all lower-case) contains the phrase “scooby dooby doo”, and if so, use the
reply() function on the comment that we access in our for-loop. Pretty neat, right?
To run your code, run the command
pipenv run python bot.py, and enjoy the results. In the next post, I’ll walk you through how to be sure you’re not responding to a comment that you’ve already responded to, so that subsequent runs don’t pester poor Reddit users too much. To do this, we’ll create a Supabase database to store comment ids that we’ve already responded to, and add a check to the code to see if that id exists in the database before replying to comments. We’ll also go over how to use GitHub secrets and actions so your bot can run on a timer instead of you having to manually run your code.
Subscribe to me on Medium for quicker access to updates.