How to make a Reddit bot with Python

How to make a Reddit bot with Python

Automating actions in the browser is a thing that removes routine and helps us spend our time on more useful things. It can be a script to clean up spam mail or a program to calculate your housing costs. There are many ready-made solutions for almost any task automation. In this article, we will revise how to build Reddit bot to automate daily tasks, why we should choose python, how to use Reddit API and what is Python Reddit API Wrapper.

Why Python?

With Python, when you want to create something interesting, you don’t have to sit for several days with books and online guides. Python was one of the first programming languages to cross out the myth about the complexity of programming. It has an extremely simple syntax, easy to write, easy to read, and hard to want more.

Another important aspect is that there are tons of free books, articles, free lessons, lectures dedicated to Python. If you decide to master this language, a lot of the necessary information will always be available to you.

Credits

 

Also, Python has powerful development tools, a huge number of ready-made libraries to solve almost any problem. With so many ready-made solutions and simple Python syntax at hand, you can create interesting projects right from the start.

And the most important, python has a module – PRAW (Python Reddit API Wrapper) – that provides access to Reddit’s API. PRAW already follows all of Reddit’s API rules.

 

How to create a Reddit bot

What you need

 

Python Knowledge:

You need to know at least a little Python to use PRAW – Python Reddit API Wrapper. PRAW supports Python 3.5+.

Reddit Knowledge:

A basic understanding of how Reddit works is a must. In the event you are not already familiar with Reddit, start at Reddit Help.

Reddit Account:

A Reddit account is required to access Reddit’s API. Create one at Reddit.

Client ID & Client Secret:

These two values are needed to access Reddit’s API as a script application (see Authenticating via OAuth for other application types). If you don’t already have a client ID and client secret, follow Reddit’s First Steps Guide to create them.

User-Agent:

A user agent is a unique identifier that helps Reddit determine the source of network requests. To use Reddit’s API, you need a unique and descriptive user agent. The recommended format is <platform>:<app ID>:<version string> (by u/<Reddit username>). For example, android:com.example.myredditapp:v1.2.3 (by u/kemitche). Read more about user agents at Reddit’s API wiki page.

Setting up

And so what do we need for this? Let’s do everything step by step:

  1. Meet the Reddit API documentation
  2. Create application on Reddit
  3. Download the required library (Reddit API) for our interpreter
  4. Test library 

 

Reddit API documentation and access

First of all, take a look at 5 API usage steps, listed on Reddit API Access note. It’s very important, cause if your intended usage is commercial, you’ll need approval from reddit.

Create an application

We go to Reddit and create application:

  • Type name for your application and select “script” option.
  • In ‘redirect uri’ type any random link. 
  • Click the “Create app” button.

Take a look at the  “Personal Use Script” and “Secret”. Note them, we will need this data in next steps.

Write Python code for our Bot

Next we need to download a library for Python by pip manager – Python Reddit API Wrapper

Type “pip install praw”.

Or install the latest version:

Note: The Reddit API has a rate limit of 30 requests per minute, but you can still make quite a bit with this. The PRAW also dynamically handles the rate limiting for you and attempts to be efficient, so you won’t have to worry about crossing the line, but you might wonder regardless why things might not be going as quick as you expect.

To instantiate an instance of PRAW:

import praw

reddit = praw.Reddit(client_id=”CLIENT_ID”, client_secret=”CLIENT_SECRET”,

                     password=”PASSWORD”, user_agent=”USERAGENT”,

                     username=”USERNAME”)

 

With the reddit instance you can then interact with Reddit by different ways:

# Create a submission to r/test

reddit.subreddit(“test”).submit(“Test Submission”, url=”https://reddit.com”)

 

# Comment on a known submission

submission = reddit.submission(url=”https://www.reddit.com/comments/5e1az9″)

submission.reply(“Super rad!”)

 

# Reply to the first comment of a weekly top thread of a moderated community

submission = next(reddit.subreddit(“mod”).top(“week”))

submission.comments[0].reply(“An automated reply”)

 

# Output score for the first 256 items on the frontpage

for submission in reddit.front.hot(limit=256):

    print(submission.score)

 

# Obtain the moderator listing for r/redditdev

for moderator in reddit.subreddit(“redditdev”).moderator():

    print(moderator)

 

On the screenshot added most common features that are really useful for users:

  • Create a submission (Line 7)
  • Comment on a known submission (Line 10). Input needed link and comment that u want. It’s really easy.
  • Reply to the first comment of a weekly top using only two lines of code (Line 14).
  • Output to console score for the first 256 items on the frontpage (Line 18)
  • Get the list of moderators ( Line 22)

Summing up

In this article we showcased how to build Reddit bot in Python, how to use reddit API, what is Python Reddit API Wrapper and how to use it. And this is just a fraction of what you can do with this python library. Everything is limited only by your imagination. Hope you enjoyed this article!

The Chatbots.Studio team develops chatbots in instant messengers – Apple Business Chat, WhatsApp, Telegram, FB Messenger and Viber. If you are interested in articles about using Python in chatbot development, subscribe to our Facebook and stay tuned for new articles. Check out guide How to Create a Telegram Reminder Bot Using Node-RED

Are you interested in chatbot development?

Build your career with Chatbots.Studio! Chatbots.Studio is a place for engineers looking for effective and creative ways to solve complex problems and constantly improving existing approaches to creating new products.

Related article: 30 Ways How to Find a Job In a Chatbot Development Agency!

Do you need to create a bot with complex functionality?

If you need to create a complex solution based on Node-RED, the Chatbots.Studio team will develop a bot of any complexity (our cases). Leave a request in the form below.