Creating a Reddit bot with the Reddit API and PRAW: a step-by-step guide

We'll walk through the steps of building a Python Reddit bot that can respond to comments in a subreddit. With just a few lines of code and PRAW, we can create a bot that monitors specific subreddits, detects certain keywords or phrases, and responds to comments with a customized message.

Creating a Reddit bot with the Reddit API and PRAW: a step-by-step guide
If you are interested in using the Reddit API, generally, you can check out my blog post and YouTube video on how to use the Reddit API without PRAW. In that post and video, we use the Reddit API without PRAW.

Reddit bots are becoming increasingly popular as more and more people seek to automate their interactions with the platform. Whether you want to monitor conversations, promote your content, or simply engage with other users on the platform, creating a Reddit bot can be a powerful tool. In this article, we'll walk through the steps of building a Python Reddit bot that can respond to comments in a subreddit. With just a few lines of code and the Python Reddit API Wrapper (PRAW), we can create a bot that monitors specific subreddits, detects certain keywords or phrases, and responds to comments with a customized message.


Intro to Reddit bots and PRAW

What are Reddit bots?

Reddit bots are software programs that can perform automated tasks on the Reddit platform. These bots can be created by anyone with programming skills and can be designed to perform a wide range of functions, such as monitoring subreddits, responding to comments, posting content, and analyzing data. Reddit bots can be built using a variety of programming languages, including Python, JavaScript, Ruby, and others. They can interact with the Reddit API to access data and perform actions, such as upvoting, downvoting, commenting, and messaging other users.

Some Reddit bots are designed to perform helpful tasks, such as providing users with useful information, tracking trends, or moderating content. Other bots, however, can be used for more nefarious purposes, such as spamming or manipulating votes. It's important to note that not all bots on Reddit are created equal, and some may violate Reddit's rules and guidelines. It's always a good idea to check the rules and guidelines before creating a bot and to test it thoroughly before releasing it into the wild.

What is the Python PRAW library?

PRAW stands for "Python Reddit API Wrapper." It's a Python library that provides a simple interface for accessing and interacting with the Reddit API. PRAW is designed to make it easy to create Reddit bots, automate tasks, and analyze data on the platform. PRAW allows you to authenticate with Reddit, access subreddit and user data, and perform actions like upvoting, downvoting, commenting, messaging, and more. The library handles the details of making HTTP requests and parsing JSON responses, so you can focus on building the functionality of your bot.

Reddit API PRAW - Step by step tutorial

PRAW is an open-source library and is actively maintained by a community of developers. It's compatible with Python 3.x and can be installed using pip. PRAW also has extensive documentation and a number of helpful examples to get you started. Overall, PRAW is a powerful and versatile library for working with the Reddit API in Python and is a popular choice for building Reddit bots and applications.

How to create a Reddit bot with Python and PRAW

How to get a Reddit API key YouTube short

Creating a Reddit app

Before we get into the code, you will need to create a Reddit app. You need to be a Reddit user with a username and login. Go to https://www.reddit.com/prefs/apps and log in to your Reddit account when you're prompted. Then, scroll to the bottom of the page and click the button to create an app. You will need to enter the app name (use whatever you like but note that you can't use the word Reddit in your app name). Then select Script for personal use, assuming that's what you're making. Then enter a redirect URI. For the redirect URI, you can use http://localhost:8080, unless you have another URI you'd like to use.

Once your app is created, you'll see a code at the top of your app and a code titled Secret. PRAW refers to the code you see at the top of your app as your client_id and refers to the code in your app titled Secret as your client_secret. You will use the client_id and client_secret in your PRAW-related code.

When available, I use affiliate links in my posts.


📚 Without their Permission

The Story of Reddit and a Blueprint for How to Change the World
Book cover, book title: Without Their Permission: The Story of Reddit and a Blueprint for How to Change the World
Without Their Permission: The Story of Reddit and a Blueprint for How to Change the World

YouTube #short


Installing and importing the PRAW library

Now we'll install and import the PRAW library. In your code editor, with a virtual environment activated, open a new terminal window and pip install PRAW.

Open a new Python file inside your project and import PRAW at the top.

Authenticating with PRAW

🗒️
We are going to include our API credentials directly in our script to keep this tutorial concise. It's not best-practice to do this. Instead you can use a .env file to store your credentials outside of your script. Check out my YouTube video on how to use .env files in Python.
import praw

reddit = praw.Reddit(
    client_id="your_client_id",
    client_secret="your_client_secret",
    username="your_username",
    password="your_password",
    user_agent="your_user_agent",
)

Configuring the bot's behavior

In this example, we'll create a simple bot that monitors comments in a specific subreddit and responds to any comment that includes a specific keyword.

First, we'll define the subreddit we want to monitor by passing its name to the subreddit() method of our Reddit instance. For example, to monitor the r/test subreddit, we would use:

subreddit = reddit.subreddit("test")

Next, we'll define the keyword we want our bot to look for in comments. For example, if we want our bot to respond to any comment that includes the word "banana", we would use:

keyword = "banana"

Now, we'll use the subreddit.stream.comments() method to start monitoring comments in the subreddit. This method returns an iterable that yields new comments as they are posted to the subreddit. We'll use a for loop to iterate over this iterable and process each comment as it comes in.

Inside the loop, we'll use an if statement to check if the comment includes our keyword. If it does, we'll use the comment.reply() method to post a response to the comment. For example, to respond with the message "I love bananas too!", we would use:

comment.reply("I love bananas too!")

Putting it all together, our bot code might look something like this:

import praw

reddit = praw.Reddit(
    client_id="your_client_id",
    client_secret="your_client_secret",
    username="your_username",
    password="your_password",
    user_agent="your_user_agent",
)

subreddit = reddit.subreddit("test")
keyword = "banana"

for comment in subreddit.stream.comments(skip_existing=True):
    if keyword in comment.body:
        comment.reply("I love bananas too!")

This bot will now monitor comments in the r/test subreddit and respond to any comment that includes the word "banana" with the message "I love bananas too!".

Of course, this is just a simple example. There are many other ways you could configure your bot to respond to comments, such as using regular expressions to match more complex patterns or using machine learning to generate more sophisticated responses.

It's worth noting that Reddit has strict guidelines on bot behavior, and using bots to spam, manipulate votes, or otherwise violate site rules can result in your account and bot being banned. Be sure to read and follow Reddit's guidelines before deploying your bot.

Wrap-up

Congratulations, you've successfully created a Python Reddit bot using PRAW! There are many other things you can do with your bot, such as analyzing data or posting content, so feel free to explore the possibilities. Please remember to be respectful of Reddit's rules and guidelines, and to test your bot thoroughly before releasing it into the wild.


How to use .env files in Python

Check out the blog post, here. Or, watch the full tutorial on YouTube.