Build a Reddit Bot Part 2: Reply to posts

Build a Reddit Bot Series

Part 1: Read posts from reddit

Part 2: Reply to posts

Part 3: Automate our Bot

Part 4: Marvin the Depressed Bot

Very Important Note: Before we start, I have a request to make. I created a special Reddit for this series. Please please please stick to that. I don’t want you spamming other Reddits. The moderators may ban you, and Reddit may ban my user agent (please read part one if you don’t know what that means). So please stick to practising here:

This is my own sub-Reddit, and you have full permission to play in that region.

Before we start, all the source code is here.

2 things our bot needs to do

1.Search our subreddit for the words “I love Python” and reply to it. It can’t assume case sensitivity

2.Keep a list of posts it has already replied to, so we are not replying to the same post again and again.

We already looked at how to do (1) in Part 1.

To keep a track of posts we have replied to, we could use something like a database. To keep it simple, I’m going to use a simple text file.

Login to Reddit with your bot

If you entered your username and password in praw.ini, you should be able to post messages with no problem.

Open the file


We start off by importing everything we need.

This code is the same as before. We create a Reddit instance.


As mentioned earlier, I am going to store the posts we have replied to in a text file called posts_replied_to.txt. Each Reddit post has its own unique id. I am storing each in a newline. An example from a live run:


To start off, we will assume this file doesn’t exist:


If the file doesn’t exist, which will be true the first time you run this program, create an empty list posts_replied_to .

If the file does exist, we open it. Let’s look at the code first. We will go over it line by line:


The above code is called if you have run the code at least once. Let’s go over it:


If you have never used Python’s with keyword, it’s a great feature. It opens the files for us, will close it, and all handle any errors. Saves us having to use try-catch blocks and handling errors ourself.


We read our file, and split on newline. Remember our file contains the submission ids as newlines? Splitting on it will create a list with all the ids. Taking the example I used earlier, we will get:


However, the list might still contain some empty values. See the last value above, it is an empty element. We get rid of them:

Python 3 update: We need to convert the filter back to  a list. Groan. Reason here.

We now have a list posts_replied_to that is either empty or contains the submission ids from last runs.


This code is the same as before. We get the last 5 entries from our subreddit.


Remember I said each Reddit post gets a unique id? It is stored in We check if this id exists in our list of posts we have replied to.

If it doesn’t, we check if the post contains the words : “I love Python”:


I am using regular expressions for the search. This is because the post can be in capital or not, or a mix. If the words i love python were found, we need to reply to the post. This is how you do it:


The reply() is the function that adds a comment to the current submission.


The next important thing is to remember which posts we have replied to. So we add the id of the current post to our list posts_replied_to.

Finally, we write out our updated list back to the file:

And that’s it. Let’s give our bot a spin.

First, we post a message on our subreddit:


We then run our Python script. Notice the Bot figures out the latest post, and only replies to that one:


And then we reload our page to check that the bot has replied.


Give it a try.

Change the bot message to something unique, so we know whose bot is running (otherwise we’ll have dozens of similar messages, with no idea who posted what). If you can’t think of anything unique, just attach your Reddit username.

Next Part

Hungry for more? Why not learn how to build a Twitter Analytics app?


59 thoughts on “Build a Reddit Bot Part 2: Reply to posts”

      1. Right, so the method you use in the file doesn’t allow this? I’m just confused by the phrase “check it in” (I’m pretty new to programming).

  1. Hi there,

    Is it possible to create bot which will post to subreddits ?

    Appreciate on your replay in advance.


    1. Do you mean create a new post? Yes, there is.

      I don’t remember it off the top of my head, but Praw has a function to create new posts as well. If you look in the api doc, you should see it.

      Let me know if you get stuck.

  2. Hey, thanks for everything so far, but i need some help, whenever i try to run, it stands there processing for a minute then automatically closes down. I tried writing it all manually but then it didn’t work. Then i tried tweaking the source, and it still didn’t work. Could you help me out here, I’m a little stuck :/

    1. He lasse,

      There might be several reasons, the most common being the server is not responding.

      If you use the Python debugger pdb to step through the code, you will find out exactly which line is causing the crash. And then we can figure out what’s wrong.

        1. At the top, add:

          import pdb

          Then add:


          This will start the debugger.

          Press “n” to step over the code line by line, and see where it freezes.

    1. ok, so i read a bit about iterators, so i upped the search from 5 to 100, now i get a new error

      Traceback (most recent call last):
      File “C:\Users\Lasse\Dropbox\Reddit bot\”, line 47, in
      submission.add_comment(“Praise Him!”)
      File “C:\Users\Lasse\AppData\Local\Programs\Python\Python35-32\lib\site-packages\praw\”, line 1098, in add_comment
      response = self.reddit_session._add_comment(self.fullname, text)
      File “C:\Users\Lasse\AppData\Local\Programs\Python\Python35-32\lib\site-packages\praw\”, line 348, in wrapped
      return function(cls, *args, **kwargs)
      File “C:\Users\Lasse\AppData\Local\Programs\Python\Python35-32\lib\site-packages\praw\”, line 2407, in _add_comment
      File “C:\Users\Lasse\AppData\Local\Programs\Python\Python35-32\lib\site-packages\praw\”, line 199, in wrapped
      raise error_list[0]
      praw.errors.RateLimitExceeded: du gjør det der for ofte. prøv igjen om 2 minutter. on field ratelimit

      1. Okay, you are exceeding the rate limit, which means Reddit thinks you are a spammer. Take a day or two off, browse Reddit normally, check you account hasn’t been locked out.

        1. the account is not locked off (for now). but when i tried your subreddit, it loops the whole search for the word i want in the subreddit.get_hot. if the submission id is not in the “already replied to”.txt…

  3. I’m having 2 problems on my MAC

    1.) I ran pip instal praw in the command line and get the message ‘Requirement already satisfied’. There is a path to a program called Canopy afterwards. When I run from the command line I get the error ‘ImportError: No module named praw.

    Then I go to Canopy to try running, and it seems to get past this, but…

    2.) cannot be found, and the scrip exits after the file check. I renamed the skel file in the same directory at I also tried creating a new file instead of renaming a file in the same directory. Do I need to enter the complete path?

    1. Yes, this is very dangerous (having multiple Python installs), as they interfere with each other.

      My advice: Have only one Python installed. Remove Canopy (or the other one).

      The file should be in the same directory as the Again, it might be the multiple Python versions thing- it might be running the wrong version, and looking somewhere unexpected.

      On Linux, you can give the exact path to the Python interpreter you want to use. Like this, on 1st line:


      This is called the shebang. Does Mac support this? If so, that might solve some of your problems.

      If you are still having problems, copy the code in to I only made them separate so that I wouldn’t check my password in, but as long as you don’t check you code (into Github or similar), you should be fine.

      1. I tried removing Canopy and think i effed up. Now I get errors that permission is denied, and nothing is running. Is there a way to just clear out all this crap and start over?

        1. Yes, it isn’t fun, is it?

          I think your default Python install is messed up. I recommend doing a new install, and changing the path so that the default system Python is never called (as it’s the one causing the problems).

          To install Python:

          1. My recommendation is Anaconda, but I have never tried it on a Mac

          2. Another way:

          Worst case: Use a virtual machine. Get VirtualBox, and install a flavour of Linux (Mint is what I prefer).

  4. Is there any way to create a bot that replies to a user every time that user posts? I can’t seem to find any documentation on the PRAW website that explains how to detect if a comment was made by a certain Reddit user. Right now I am iterating through /r/all/comments to find any posts made by a certain user, but I can’t figure out how to determine the user associated with a comment. Is each user given an ID that you need to check against?

    1. I don’t think there is. That would mean you’d have to put a hook on everything a user does, and I’m sure Reddit wouldn’t allow a third party to do that.

      As a simple rule, the API can’t do anything you can’t do manually.

      So Im afraid you will have to poll Reddit regularly.

  5. Thank you for this great post! I’m new to Python and bot making. I’m having an issue where the submission ID is not being written to the posts_replied_to.txt file. I’ve tested the functionality of the file by adding an ID by hand and it’s working fine, but my code isn’t writing to it. Any ideas? Thank you!

    1. Most likely a file writes issue. Are you on Windows or Linux? Check that the file has write permissions, and Python can write to it.

      Sometimes, some programs will “lock” a file, so no one else can write to it. On Windows (where this usually happens) I use a tool called Unlocker to free it. You can also reboot the system, that will remove the locks.

      Worst case, delete the file and recreate it.

    1. post_id is just a variable name I made up to loop through posts_replied_to, which counts how many posts we have replied to.

      Is that part of the loop working? Can you get any submission ids? As it should store the submission ids in posts_replied_to, so that next time, you dont reply to the same post again.

      In summary, something is going wrong, and you are not even connecting properly. I would use a debugger (pdb) to step though the code and find out what’s happening.

  6. The submission ids are being written to the file, but the bot will comment on the same post anyway. Here’s the log:

    Bot is replying to : Bagels
    Traceback (most recent call last):
    File “C:/Users/Wooten/Desktop/Sams_Game_Stuff/Python/”, line 29, in
    AttributeError: ‘filter’ object has no attribute ‘append’

    1. This is the code

      subreddit = r.get_subreddit(‘pythonforengineers’)
      for submission in subreddit.get_hot(limit = 5):
      if not in posts_replied_to:
      if“bagels”, submission.title, re.IGNORECASE):
      print (“Bot is replying to : “, submission.title)
      with open(“posts_replied_to.txt”, “w”) as f:
      for post_id in posts_replied_to:
      f.write(post_id + “\n”)

    2. It seems posts_replied_to is not a list. Check if it is being defined as something else.

      You can check by: print(type(posts_replied_to)). You should see list.

        1. You need to initialise it as: post_replied_to = []

          If you are initialising it somewhere else, remove that code.

  7. I am getting an error with:
    submission.add_comment(“Botty bot says: Me too!!”)
    python says:
    AttributeError: ‘Submission’ object has no attribute ‘add_comment’

    I had to change it to say submission.reply(“Botty bot says: Me too!!”)

  8. I keep getting a traceback. It happened even the first time I ran the script.

    praw.exceptions.APIException: RATELIMIT: ‘you are doing that too much. try again in 9 minutes.’ on field ‘ratelimit’

    There is some text above that line too but that’s the error. Strangely, the post reply works.

    1. It means you are running the code too many times! Give it a break. When you see the message above, take a 15 minute break.

      1. Hi. Thanks for the reply. I couldn’t figure out why it was giving me that error when I only ran it once. I ended up creating another thread and it worked just fine. Thanks for the tutorials!

        1. You might have hit a bug in Praw, or more likely, Reddit servers were acting up. Reddit doesnt have a very stable site/API. I know they are big, but they have been big for years…..

  9. Python code is running without error, but I’m not seeing a comment on the post on Reddit :/. It appears to not be finding a post match, despite there being one.

  10. I get a “prawcore.exceptions.OAuthException: invalid_grant error processing request” error when trying to run the code:(( any suggestions?

    1. Usually means it couldnt login. Are the keys correct, and did you copy them correctly? Also read the other comments, other people faced similar problems.

  11. [root@vps ~]# python
    File “”, line 46
    f.write(post_id + “1”)
    IndentationError: expected an indented block

    Getting this, did i forget something?

    1. Yes, you could either do it dynamically on page load, or store the data to a database and read it later.

      In Flask you would use something like Jinja templates to render the data in a web browser

  12. I’m having some issues with the submission ID being written to a text file.

    The program does create a text file if it is not there, but won’t write to it. I have already set all permissions to full control for all users on my computer, so it should have read/write access.

    I also copied the github code into a new file and ran that. Its not even making a new file (I used a different name for my file vs github code).

    Pretty confused with this and not sure where to go from here. I keep getting posting limit reached due to the program attempting to post multiple times to your subreddit post.

    1. I found that it seems to be an issue with post_id being undefined. Do I have to define it even when its the same as in the github code? Confused.

      1. I cant edit my posts, so have to keep replying:

        Its looking like I got it to write to the file now, I’m really not sure how, but its writing to the txt file. The issue I’m having now is the code is ignoring that the ID is in the file, and keeps looping based off the limit number, which always causes me to get locked out for 10 minutes.

        1. My last update for today:

          I think the issue is with the final with open statement.

          I tried to move it inside the 2nd if loop, and it was writing to the notepad, but not exiting the for loop limit. Something might be missing or incorrect, but I’m too much of a beginner to figure it out. Even the code from github is not working correctly.

          1. Maybe the reason it isnt writing because there isnt anything to write? Ie, the problem is that you are getting nothing from the server. The fact that you are getting locked out gives me a hint.

            I would take 1-2 days off, then try again, this time printing to the screen as well as writing to a file. This will tell you if you are getting any data

          2. Thanks for the reply, Shantnu.

            I probably neglected to mention it, but I did add a print statement to print the id, and it was correctly printing it, but still only saving the id to the notepad when i move the final with statement inside the if loop. Due to this I am able to determine the id itself isnt the issue.

            The locking out occurs after the first initial post is made. Due to the id not being saved, the loop keeps trying to make a post. I changed the limit where the max is falls on the first “i love python” post (limit=6) and it works perfectly! So due to all this, really thinking the issue is with the loop somewhere.

Leave a Reply

Your email address will not be published. Required fields are marked *