Create Your First Reddit Bot with Python and Heroku

Brian Gaus
4 min readJan 3, 2022

--

Photo by Brett Jordan on Unsplash

Creating a Reddit bot should be simple, but the process is poorly documented. This article will guide you through every single step of bot building.

Prerequisites:

  • Basic Python Knowledge
  • Familiarity with the Command Line
  • A Reddit Account
  • An Email
  • A Github Account

Part 1: Create a Reddit Developer Account

Since 2019, Reddit has dramatically changed its appearance, but all the developer tools are still housed on Reddit’s old site.

Old Reddit
  • Click Apps
Reddit Preference Page
  • Scroll to the Bottom of the Apps page and click the “are you a developer? Create an app” button.
  • Fill out the form. Make sure to set your application to script, so it will have interaction privileges.

Part 2: Coding Your Bot

When you finished creating your developer account, you should have received a personal use script and a private key

Reddit uses these to authenticate your bot and make sure you are not committing any mischief.

The easiest way to build a bot is with the Python Reddit API Wrapper (PRAW). It provides all the functionality to build incredibly sophisticated bots with ease. You can use it to scan all new submissions/comments, make posts/comments, and extract data.

Coding your bot:

  • Create a folder for your new bot
  • Open up your command line/terminal and navigate to your new project folder
  • You will need to create a virtual environment for your directory. Virtual environments keep track of your pip dependencies between projects. You can review the process on the Python.Land website
  • Use pip to install PRAW
Installing PRAW
  • Create a main.py file in your working folder and add the following to verify your Reddit Bot with PRAW:
  • The bot is going to comment a Harry Potter Quote to anyone who types Abra Cadabra. Below is a list of quotes you can copy and paste into your code. You will also need to import the random library to help select a quote.
  • Finally, using the reddit variable you created in the beginning of this section, you can get a stream of user comments and respond to the ones that say Abra Cadabra
  • Reorganizing the code a bit and wrapping it up in a main function results in our final output
Your Reddit Bot
  • Type python3 main.py into the command line to start your bot. Then go to whatever subreddit you deployed it to and test it by typing Abra Cadabra. It might take a minute or two for your bot to respond. After all, it is parsing every single new comment for its trigger phrase. However, when you finally see it live, the wait will be worth it.

Part 3: Deploying to Heroku

Heroku is an application deployment platform and it has a generous free tier, offering anyone 550 free server hours a month. You will utilize that free tier to deploy your bot.

Before you can deploy to Heroku, you need to add two necessary files to your bot’s folder

  • A requirements.txt file: this outlines what libraries and dependencies your app needs to run. Assuming you properly set up your virtual environment in part 2, you can generate one by typing the following in the command line.
pip freeze > requirements.txt
  • A procfile: this tells Heroku how to classify and deploy your app. Heroku is strict about naming conventions. Your Procfile should be a basic text file and have the following code:
Procfile

Your bot code is all set up. Now you must push it to GitHub. GitHub is the simplest way to deploy to Heroku

You are now only a few steps away from having an active bot.

  • Follow the general sign up process and it will eventually ask you to create a new app. Insert a unique name for your app and your country and then click Create app
  • Heroku should have redirected you to the deploy page. Click GitHub
  • Follow the general process and then click Deploy Branch

Congratulations! You created and deployed your first Reddit Bot.

--

--

Brian Gaus

I'm a multi-faceted individual with experience in Full-Stack development and data analysis