When working in a small team such as ours, speed is one of the undeniable assets — you may be small in numbers but you can make up for that by doing things fast.
One of the ways you can gain momentum is by automating various tasks. For this reason I have been working on building several different types of bots lately. So far I have built bots for Slack, email and calendar, and most recently - Reddit. In this tutorial I will show you how to get started with building a simple bot for Reddit.
This article is a high overview of the steps required to build a simple bot. You can find the accompanying starter code here:
Choosing the Stack
Python is the undeniable champion language for scripting and several automation tasks. Part of the popularity is the number of existing libraries that help with accomplishing various tasks. Therefore choosing Python as the programming language for a Reddit bot was a natural choice.
Secondly you will want to choose a library that helps working with Reddit application programming interface, the Reddit API. Since working with Python, a natural choice for a library is PRAW ↗ which stands for “Python Reddit API Wrapper”. This is an open source, actively supported library that enabled us to quickly start working with Reddit API.
Great, now we have the basic tools together and can focus on building the bot!
Setting up the Project
As with any programming mission, the first step is to start a project. I created my reddit bot starter code in PyCharm. This setup included the following steps:
- Creating a project
- Creating a virtual environment
- Installing project dependencies (PRAW)
You can find a more detailed explanation of these steps in the starter code.
Creating Reddit Application
If you are familiar with working with various APIs, these steps should sound familiar to you. It is typically the case when you start using an API, you will need to identify your application in some way. When no private information is being handled a simple API token may suffice. More commonly — and certainly with Reddit where you are dealing with user identities — you need a more robust method of identification for your application.
Therefore, before starting to use the Reddit API, we need to register the application we are building. There are a few different options of what types of applications we can create to work with Reddit (web, mobile, script). Since the intent is to create a bot to perform actions impersonating a single user (myself) we can choose script type.
This decision simplifies the authentication flow quite significantly: you generate a client ID and secret, and combined with your own username and password, you can now impersonate yourself programmatically. The limitation of this strategy is that the bot is always tied to a single user. If you need more robust application with multiple users, you will need to follow the OAuth 2.0 - flow.
Now the fun part: Coding!
At this point, we have the project environment setup and sufficient credentials to work with reddit content. The only thing left is to start programming the actions we want to take.
Recall we are using PRAW library, which simplifies the programming steps a lot. The first step is to initialize the PRAW reddit instance:
1 | import praw |
Here you will use the CLIENT_ID
and SECRET
you obtained when creating an application on Reddit, as well as the USERNAME
and PASSWORD
of the Reddit user you want to impersonate. If you want to built a bot to act on your behalf, you will use your own username and password. Alternatively, you can register a new user account for your bot, and use those credentials here.
The last piece of information USER_AGENT
is a further way to uniquely identify you application and you must be truthful when choosing a value for user agent. The general format is this:
1 | <platform>:<app ID>:<version string> (by /u/<reddit username>) |
which, after you modify the above, ends up being something similar to this:
1 | windows:myredditbot:v1.2.3 (by /u/sloppyjoe123) |
If you are curious about learning more about how to choose the appropriate user agent for your reddit bot, read this guide ↗.
Once you have the reddit instance, you can now start performing different actions. For example to read hot posts from the bot users front page can be done like this:
1 | # get 100 posts |
PRAW allows you to work with many reddit features such as get posts from specific subreddits or users; perform messaging, reply to posts and much more. You can find PRAW docs here ↗, and while they are quite technical, they will give you some amazing automation powers once you learn to use the commands effectively.
Happy Coding!