A quick Reddit autoreply bot

Similar to my last *nix style project I wanted to write something in Python that could interact with Reddit and can be ran from a Raspberry Pi. At this stage I only wanted to see what was possible and start early, so I borrowed some code to make a bot that scanned a subreddit for a certain phrases and then replied to those comments. Similar to last time there are basically only 2 steps:

1. Setting up a *nix environment to handle it.

This project is for the Raspberry Pi so I need to make sure it works on Linux. The only things you really need is Python which comes with most *nix systems and the PRAW module which can installed with:

pip install praw

PRAW is the Python Reddit API Wrapper and gives you access the wrapper quite easily.

On Windows you will need to install Python, it should be quite easy and you can follow the instructions on the Python site. As far as I am aware pip is installed in the Windows installation too.

2. Write and run the script

Start a text document with your favorite editor, save it as something like redditreply.py and copy the following script written by /u/shaggorama

import praw
from praw.helpers import comment_stream

r = praw.Reddit("Change me")
r.login()

target_text = "text"
response_text = "text"

processed = []
while True:
    for c in comment_stream(r, 'all'):
        if target_text in c.body and c.id not in processed:
            c.reply(response_text)
            processed.append(c.id)

You need to change some bits, ‘change me’ is the name of the user agent. The target_text is text in comments that your script will look for and response_text is the reply that your post will respond with. You need to change ‘all’ to the subreddit you wish to run the bot over. I used /r/sandboxtest which is a subreddit for bots.

Once you have changed the text you will need to run the script with

python redditreply.py

The script will ask you for the username and password of the bot and will then go away and reply to lots of comments. My next challenge and perhaps tomorrows post is to turn this in to a Google Script.

Got lost? Here is a video of the process: