Create a Reddit Bot that automatically replies to Questions on AskReddit using Python

I recently published an article on creating a basic Reddit Chatbot you would be able to ask it questions and it would search Reddit for the answer and reply with an answer. This is an upgraded version of the previous Reddit bot I created except this one will actually post replies on Reddit and other users will assume it is a real person replying to their questions. I used Reddit s PRAW library that uses the Reddit API and makes it very easy to search for submissions and grabbing comments and posting reply’s on Reddit. You will need to install the PRAW library in Python to use this script.

The way the script works is it monitors /r/AskReddit for new submissions and grabs the title of the new submission. After it grabs the title it will search Reddit using the title and try to see if the question was asked before. If it was asked before it will look at the comments to that question, grab a random comment and reply to the new submission with the grabbed comment. Its actully very ingenious and has a very high confidence rating when replying to submissions.

I use my Raspberry Pi to run my bot since my Raspberry Pi is always on and connected to the internet. Using the Raspberry Pi allows my bot to be running 24/7 which is nice to watch it respond to people all the time.

Objective

To create a Reddit Bot that automatically replies to Questions on the AskReddit subreddit using Python

Material

You will need the following:

Instructions

I will go over the functions and steps I used to create my Reddit bot below and try to explain what I am going to get the Bot the work. At the bottom I will combine everything together. You should be able to simply copy and paste the final code into Python, add your bot’s API credentials and it will start replying to comments.

The first steps are to import the packages we will be using. Make sure you install the PRAW library into Python first. You can use ‘pip‘ to install PRAW.

Once you have PRAW installed you can add the libraries that we will be using to your Python project:

Now we will enter in our bot’s credentials. You will need to create a API ID and Secret key from your profile in Reddit. You will need to add that in the config below along with the bot’s username and password. This will authenticate the bot on Reddit and allow you to use the PRAW commands without getting banned. Make sure you add your username in the ‘user_agent’ field so that mods and admins on Reddit know who to contact in case of issues with your bot.

The next step if to create a global array which will store the submission id’s that we have already replies too. You don’t want to keep replying to the same questions over and over.

This will be our first function. It will be used to gauge the similarity of the search results title to the submissions title. It will compare the 2 strings and return a values between 0-1 on how similar the 2 strings are. The higher the number the more similar the 2 stings are.

Now comes our main function that will do all the work. I will try to explain everything I did in my script. I am sure some of the things being done can be simplified further. Remember I am still learning Python and this is one of my first projects. I commented on as much as I can so hopefully it all makes sense.

That’s the main function that will do all the hard work. Now we just need a main function that will constantly check the above function.

Final Code

Combine everything above and you will get the following

Leave a Reply