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 | reddit = praw.Reddit( |
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 | [bot527] |
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:
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
Procfile
: This is a Heroku specific file that declares what command Heroku should execute to start your app. A detailed explanation in here1
worker: python bot.py
requirements.txt
: python package requirements, obtained bypip 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
- Submit a Post onto a Subreddit:
submission = reddit.subreddit("test").submit("title", selftext=text)
- Edit an Existed Post:
submission.edit(body=submission.body + "edited")