Yao Lirong's Blog

Deploy a Reddit Bot on Heroku

2022/09/04

I’ve created a bot to send warm welcomes to newly admitted Cornell prefrosh.

Simple Auto-Reply Bot

This Let Me Google It For You Bot tutorial perfectly explains how to listen to reddit post stream and reply to a post.

We use the following code to declare a reddit bot:

1
2
3
4
5
6
7
reddit = praw.Reddit(
client_id="CLIENT_ID",
client_secret="CLIENT_SECRET",
username="USERNAME",
password="PASSWORD",
user_agent="LMGTFY (by u/USERNAME)",
)

If we want to put our code onto GitHub, this will expose our client secret and account password. Therefore, we can make a separate file called praw.ini, where we specify these private information. Note an ini file cannot contain most special characters, so you need to change your password to only words and digits.

1
2
3
4
5
6
[bot527]
client_id=d123071240924w
client_secret=D123412541254
username=Harmonyano
password=YouthinkIwillTellYouThisHuh
user_agent=bot

With this, we can declare our bot with the following command. Note the customize name “bot527” need to match in both the ini file and the declaration .

1
reddit = praw.Reddit("bot527")

Deploy on Heroku

Create a Heroku app, follow the instructions in Deploy/Heroku Git tab.

For a Heroku app to run, it needs several additional files:

  1. Runtime.txt: Though it’s in question whether this is really needed or not (the app runs normally too without it?)

    1
    python-3.7.9
  2. Procfile: This is a Heroku specific file that declares what command Heroku should execute to start your app. A detailed explanation in here

    1
    worker: python bot.py
  3. requirements.txt: python package requirements, obtained by pip freeze > requirements.txt

    1
    praw==7.6.0

Finally, after you deploying all the codes to Heroku, DON”T FORGET THE MOST IMPORTNAT PART: you need to turn on the resources tab on Heroku for your worker (spent 1h figuring this out). For where to find it, watch this video

Submit & Edit a Post

CATALOG
  1. 1. Simple Auto-Reply Bot
  2. 2. Deploy on Heroku
  3. 3. Submit & Edit a Post