A3: Trolling a Reply Bot#

Choose Social Media Platform: Reddit | Discord | Bluesky | No Coding

In this assignment, your job will be to modify the reply bot below (see section Demo: Trolling a Reply Bot) and make your own version of a reply bot (look for the TODO section below). Then after you’ve modified it, see if you can troll it. At the end, you will have some reflection questions to answer.

When you are done, you will need to download this file (file menu -> download) and turn it in on Canvas.

Below is the code for the first reply bot from the book, where if you message it:

  • Subject: “Wanting bot response”, body: “I want you to ___” (where the ___ is some action)

  • then the bot will reply, “I will now ____” (where the ___ is that same action).

Sending ourselves a message#

In order to send ourselves a message we can reply to, go to:

Then compose a message to your own account with the subject:

  • Wanting bot response and a message body of something like:

  • I want you to jump

The Reddit "Send a Private Message" screen, with From "/u/kthayer_teacher_bot" filled in by default and uneditable, then To "kthayer_teacher_bot", Subject "Wanting bot response" and Body "I want you to jump"

Reply Bot#

First we need to do our Reddit PRAW setup:

import praw

(optional) make a fake praw connection with the fake_praw library

For testing purposes, we’ve added this line of code, which loads a fake version of praw, so it wont actually connect to reddit. If you want to try to actually connect to reddit, don’t run this line of code.

%run ../../../../fake_apis/fake_praw.ipynb
Fake praw is replacing the praw library. Fake praw doesn't need real passwords, and prevents you from accessing real reddit

To use this on your real Reddit account, copy your developer access passwords into the reddit_keys.py file.

import reddit_keys
# Give the praw code your reddit account info so
# it can perform reddit actions
reddit = praw.Reddit(
    username=reddit_keys.username, password=reddit_keys.password,
    client_id=reddit_keys.client_id, client_secret=reddit_keys.client_secret,
    user_agent="a custom python script for learning for " + reddit_keys.username
)
Fake praw is pretending to collect account info to use on reddit

find my latest message#

We need to find our latest message in our inbox

We do this by looking in our reddit inbox for messages (we limit it to one, since we just want the latest).

It doesn’t directly give us the one message (instead it is in something called an “iterator”), but we can use the next function to get the message out.

We then display the subject of the message just so we can see that it found something.

# Look up the subreddit "cuteanimals", then find the "hot" list, getting up to 1 submission
messages = reddit.inbox.messages(limit=1)

# get the first submission off the list (should only be one anyway)
latest_message = next(messages) 

# display the subject and body of the message, so we can see what we found
display("latest message subject: " + str(latest_message.subject))
display("latest message body: " + str(latest_message.body))
'latest message subject: Wanting bot response'
'latest message body: I want you to jump'

TODO: Modify this section (If message matches our pattern, reply)#

You must must modify at least one thing in the code below. You could change the expected patterns, or you could change the code that replies, or you could change both.

We will now see if the message matches our pattern of a message subject of “Wanting bot response” with a message body of “I want you to ___” and then we will reply.

First we will create strings with the patterns we are looking for and save them into variables.

expected_subject = "Wanting bot response"
expected_body_pattern = "I want you to "

We will check if the message has the subject we are expecting. If it does it will check if the essage body starts with the expected pattern. If it does, then we will find the action from the end of the message body text (based on the expected_pattern length), and reply using that action.

We also add “else” cases for when we didn’t match the patter, and display a message saying what didn’t match.

# check if the mention text starts with our set phrase
if latest_message.subject == expected_subject:
    
    if latest_message.body.startswith(expected_body_pattern):
        # get the action, which should be the end of the string after the expected pattern
        action = latest_message.body[len(expected_body_pattern) :]

        # make a new message which says we will do the action
        message = "I will now " + action

        # send our message in reply
        latest_message.reply(message)
        
    else: # else code for if the message body didn't match
        display("The message body (" + latest_message.body + ") didn't match our pattern (" + expected_body_pattern + ")")
        
else: # else code for if the message subject didn't match
    display("The message subject (" + latest_message.subject + ") didn't match our expected subject (" + expected_subject + ")" )
Fake praw is pretending to reply to a message with: I will now jump

Reflection questions#

  1. What changes did you make to the reply bot?

TODO: Write your answer here

  1. How could you troll this bot? Give an example of a message that would let you troll it. Or, if you don’t think it can be trolled, explain why.

TODO: Write your answer here

  1. What limitations does trying to prevent trolling put on your ability to create a bot? (write at least 3 sentences)

TODO: Write at least 3 sentences here

  1. Pick two ethics frameworks and compare how they might evaluate the responsibility of someone who is creating a reply bot? (write at least 6 sentences total)

TODO: Write at least 6 sentences here

  1. Pick two ethics frameworks and compare how they might evaluate the responsibility of someone who is considering trolling a reply bot? (write at least 6 sentences total)

TODO: Write at least 6 sentences here