Programming

Whether you like it or not, a significant number of Reddit users are bots. Usually, they perform just two tasks: they look for comments that match a few well-defined criteria and then automatically reply to them. Creating such bots takes less than 5 minutes if you know how to use Python.

In this tutorial, I’ll show you how to use the new and very powerful PRAW 4 Python package to create a Reddit bot that can not only look for comments containing a specific word in a given subreddit, but also automatically reply to such comments with a predefined message.

For the sake of a clear example, we’ll be looking for the word Mars in the comments of the r/space subreddit. The canned reply will be Why no love for Titan?

Setup

Let’s start by installing the PRAW 4 package. The easiest way to do so is to use Python pip. If you don’t have it, here’s how you can get it on Ubuntu 14.04:

sudo apt-get install python3-pip

With pip3 , you can install PRAW using the following command:

pip3 install --user --upgrade https://github.com/praw-dev/praw/archive/praw4.zip

At this point, you can start using PRAW 4.

Registering the Bot

All Reddit bots must be registered with Reddit. Therefore, log in to Reddit using a new account, or an account you already have, and click on the preferences link.

Next, click on the apps tab.

Here, click on the create an app… button. You’ll now see a form asking for various details about the application.

Give a unique name and meaningful description to the application, and select script. The about url and redirect uri fields are not necessary for a script-based bot. Therefore, you can specify any URL. I’ll use example.com.

Once you press the create app button, your bot will be registered and you will be able to see its client ID and client secret. Make a note of them because you’ll be needing them next.

Creating the Bot

Create a new file called mybot.py and open it using your favorite code editor. The first thing you need to do inside it is import the PRAW package using the following statement:

import praw

You can now create an instance of your bot using the praw.Reddit class. To its constructor, you must pass a meaningful user-agent, the client id, the client secret, your Reddit account’s username, and its password. Note that the Reddit account you use here is the same as the one you used while registering the bot.

bot = praw . Reddit ( user_agent = 'MySimpleBot v0.1' , client_id = 'abcdefGHIJKLO123--' , client_secret = '12345678901abcdefgh-_' , username = 'demo_user' , password = 'demo_password' )

The bot is now ready to be used.

To point it to a specific subreddit, you must use the subreddit() method. Here’s how you point it to the r/space subreddit.

subreddit = bot . subreddit ( 'space' )

PRAW4 makes it very easy for you to continuously monitor a subreddit for new posts and comments. All you need to do is make use of the stream object. By using stream , you don’t have to create any loop, or bother about Reddit’s rate limits. PRAW 4 handles it all for you automatically.

comments = subreddit . stream . comments ()

Once you have the comments generator, you can create a for loop to analyze each item it returns. For now, let us just extract the comment’s body and author fields.

Once you are sure that the body contains the keyword Mars, you can call the reply() method of the comment object to send a canned reply. Here’s how you would do that:

for comment in comments : text = comment . body # Fetch body author = comment . author # Fetch author if 'mars' in text . lower (): # Generate a message message = "Why no love for Titan, u/{0} ?" . format ( author ) comment . reply ( message ) # Send message

That’s all you need to do. Save the file, and exit your code editor. From the command line, you can run the bot using the following command:

python3 mybot.py

Note that the script will never exit, unless you press Ctrl+C. That’s because it is continuously fetching new comments, analyzing them, and replying to them.

You can, of course, take a look at its replies directly on Reddit.

Conclusion

In this tutorial, you learned how to create a Reddit bot using PRAW 4. For best results, I suggest you use a Reddit account that is at least a week old for your bot. Otherwise, Reddit might be restricting it considerably.

Related Reading