0 Google+ 0 Reddit 1 StumbleUpon 0 Pinterest 0

I’ve been learning to program using Python recently and started making simple bots that can be used on Reddit. I created a Chat Bot that will reply questions asked on /r/AskReddit recently and wanted to make something a little more lighter and meme worthy. I created a bot that searches for people that say ‘Sad.’ at the end of a sentence and replies with ‘this is sad, alexa play despacito’. its basically two memes in one. A lot of people now add ‘Sad.’ to the end of their comments and I reply with the ‘alexa play despacito’ meme. It’s a very simple bot that shows you how to search all users comments and you can change the parameters however you see fit. If you think of other idea’s for simple reply’s please let me know in the comments below.

I programmed the Reddit Bot in Python and use Reddit’s API Wrapper (PRAW) that lets me easily search for Reddit comments and post reply’s. 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. you can view my bot in action by searching for the Reddit username /u/very_slowly

Objective

To create a Reddit Bot that automatically replies to comments that end in ‘Sad.’

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 comment

he 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.

pip install praw 1 pip install praw

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

import praw import time 1 2 import praw import time

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.

reddit = praw.Reddit(client_id='xxx', client_secret='xxx', password='xxx', user_agent='chatbot by /u/xxx', username='xxx') 1 2 3 4 5 reddit = praw . Reddit ( client_id = 'xxx' , client_secret = 'xxx' , password = 'xxx' , user_agent = 'chatbot by /u/xxx' , username = 'xxx' )

We will not create a global variable that will be used to keep track of the previous comment we replied to

previous_id="0" 1 previous_id = "0"

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.

def search(): for results in reddit.subreddit('all').comments(): #Grab all the Recent Comments in every subreddit. This will return 100 of the newest comments on Reddit global previous_id #Import the global variable body = results.body #Grab the Comment body=body.lower() #Convert the comment to lowercase so we can search it no matter how it was written comment_id = results.id #Get the Comment ID if comment_id == previous_id: #Check if we already replied to this comment return "Error" found=body.find('. sad.') #Search for Sad. Make sure there is a period at the end of the previous sentence as well as the end of SAD if found != -1: #Looks like the comment includes . sad. previous_id = comment_id #Add to our global variable try: results.reply('this is sad, alexa play despacito') #Reply to the Comment except: break #Leave the function if error occurs with replying 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 def search ( ) : for results in reddit . subreddit ( 'all' ) . comments ( ) : #Grab all the Recent Comments in every subreddit. This will return 100 of the newest comments on Reddit global previous_id #Import the global variable body = results . body #Grab the Comment body = body . lower ( ) #Convert the comment to lowercase so we can search it no matter how it was written comment_id = results . id #Get the Comment ID if comment_id == previous_id : #Check if we already replied to this comment return "Error" found = body . find ( '. sad.' ) #Search for Sad. Make sure there is a period at the end of the previous sentence as well as the end of SAD if found != - 1 : #Looks like the comment includes . sad. previous_id = comment_id #Add to our global variable try : results . reply ( 'this is sad, alexa play despacito' ) #Reply to the Comment except : break #Leave the function if error occurs with replying

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.

while 1: search() time.sleep(5) 1 2 3 while 1 : search ( ) time . sleep ( 5 )

Final Code

Combine everything above and you will get the following

import praw import time previous_id="0" reddit = praw.Reddit(client_id='xxx', client_secret='xxx', password='xxx', user_agent='chatbot by /u/xxx', username='xxx') previous_id="0" def search(): for results in reddit.subreddit('all').comments(): #Grab all the Recent Comments in every subreddit. This will return 100 of the newest comments on Reddit global previous_id #Import the global variable body = results.body #Grab the Comment body=body.lower() #Convert the comment to lowercase so we can search it no matter how it was written comment_id = results.id #Get the Comment ID if comment_id == previous_id: #Check if we already replied to this comment return "Error" found=body.find('. sad.') #Search for Sad. Make sure there is a period at the end of the previous sentence as well as the end of SAD if found != -1: #Looks like the comment includes . sad. previous_id = comment_id #Add to our global variable try: results.reply('this is sad, alexa play despacito') #Reply to the Comment except: break #Leave the function if error occurs with replying while 1: search() time.sleep(5) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 import praw import time previous_id = "0" reddit = praw . Reddit ( client_id = 'xxx' , client_secret = 'xxx' , password = 'xxx' , user_agent = 'chatbot by /u/xxx' , username = 'xxx' ) previous_id = "0" def search ( ) : for results in reddit . subreddit ( 'all' ) . comments ( ) : #Grab all the Recent Comments in every subreddit. This will return 100 of the newest comments on Reddit global previous_id #Import the global variable body = results . body #Grab the Comment body = body . lower ( ) #Convert the comment to lowercase so we can search it no matter how it was written comment_id = results . id #Get the Comment ID if comment_id == previous_id : #Check if we already replied to this comment return "Error" found = body . find ( '. sad.' ) #Search for Sad. Make sure there is a period at the end of the previous sentence as well as the end of SAD if found != - 1 : #Looks like the comment includes . sad. previous_id = comment_id #Add to our global variable try : results . reply ( 'this is sad, alexa play despacito' ) #Reply to the Comment except : break #Leave the function if error occurs with replying while 1 : search ( ) time . sleep ( 5 )