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 post at it:
“Hi @mybotname.bsky.social, please ___” (where the ___ is some action)
then the bot will reply, “I will now ____” (where the ___ is that same action).
Writing the initial post#
To start the assignment, we can create an initial post on Bluesky that starts with the expected pattern text, like Hi @mybotname.bsky.social, please jump
. You can do this by manually creating a post on Bluesky, or using your code from assignment 1.
Log into Bluesky (atproto)#
These are our normal steps get atproto loaded and logged into Bluesky
from atproto import Client
(optional) make a fake Bluesky connection with the fake_atproto library For testing purposes, we”ve added this line of code, which loads a fake version of atproto, so it wont actually connect to Bluesky. If you want to try to actually connect to Bluesky, don’t run this line of code.
%run ../../../../fake_apis/fake_atproto.ipynb
To use this on your real Bluesky account, copy your developer access passwords into the reddit_keys.py file.
import bluesky_keys
client = Client(base_url="https://bsky.social")
client.login(bluesky_keys.handle, bluesky_keys.password)
TODO: Modify this section (Set expected pattern)#
You must must modify at least one thing in the code. You could change the expected patterns here, or you could change the code that replies further down below, or you could change both.
get my handle and set the expected pattern#
Our first bot will find our latest mention, and then reply with whatever it is told to do.
First we need to look up our Bluesky handle
# look up my Bluesky handle
my_handle = client.me.handle
set the expected pattern#
Now we will create a string with the pattern we are looking for, but with our actual bot name
#TODO: Modify expected_pattern
expected_pattern = "Hi @" + my_handle + ", please "
print(expected_pattern)
Hi @mybotname.bsky.social, please
find my latest message#
We need to find the latest bluesky post that mentions us and match our expected pattern.
We do this by first finding our user handle, then requesting
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..
# search posts that mention my handle
mentions = client.app.bsky.feed.search_posts({
'q': expected_pattern,
'mentions': my_handle
}).posts
if(len(mentions) < 1):
print("Error: Could not find matching mentions!")
latest_mention = mentions[0]
# display the text of the post, so we can see what we found
display("latest post text: " + str(latest_mention.record.text))
'latest post text: Hi @mybotname.bsky.social, please jump'
TODO: Modify this section (If message matches our pattern, reply)#
You must must modify at least one thing in the code. You could change the expected patterns (earlier), or you could change the code that replies below, or you could change both.
Now, if the mention text starts with that expected pattern, then we will find the action from the end of the mention 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_mention.record.text.startswith(expected_pattern):
# get the action, which should be the end of the string after the expected pattern
action = latest_mention.record.text[len(expected_pattern):]
# make a new message which says we will do the action
message = "I will now " + action
# send our message in reply
client.send_post(
message,
reply_to={'root': {'uri': latest_mention.uri, 'cid': latest_mention.cid},
'parent': {'uri': latest_mention.uri, 'cid': latest_mention.cid}}
)
else: # else code for if the message subject didn't match
display("The message (" + latest_mention.record.text + ") didn't match our expected pattern (" + expected_pattern + ")" )
I will now jump
Reflection questions#
What changes did you make to the reply bot?
TODO: Write your answer here
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
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
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
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