The goal of this assignment is to allow you to explore artificial intelligence by creating small Twitter bots in the Python programming language.
To record your solutions and answers, create a new Jupyter Notebook
titled Notebook10.ipynb
and use this notebook to complete the following
activities and answer the corresponding questions.
Make sure you label your activities appropriately. That is for Activity 1, have a header cell that is titled Activity 1. Likewise, use the Markdown cells to answer the questions (include the questions above the answer).
This Notebook assignment is due midnight Friday, November 18, 2016 and is to be done individually or in pairs.
Before attempting the activity below, be sure to follow the Reading 10 Notebook on how to setup your Twitter account and application settings.
For this Notebook, you are to implement two of any of the following options. Each bot involves reading and writing Twitter status messages.
To help you out, each option is labeled with a difficulty rating and includes some hints.
You can use the following starter code for each bot:
# Imports
import time
import tweepy
# Twitter Keys and Tokens
CONSUMER_KEY = ''
CONSUMER_SECRET = ''
ACCESS_TOKEN = ''
ACCESS_SECRET = ''
# Authentication
AUTH = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
AUTH.set_access_token(ACCESS_TOKEN, ACCESS_SECRET)
API = tweepy.API(AUTH)
# Bot Functions
def run_bot(bot, timeout=60.0):
''' Wrapper function that periodically calls run_bot_once '''
while True:
print 'Waiting {} seconds...'.format(TIMEOUT)
time.sleep(TIMEOUT)
print 'Running bot...'
bot()
Be sure use the CONSUMER_KEY
, CONSUMER_SECRET
, ACCESS_TOKEN
, and
ACCESS_SECRET
from the apps.twitter.com
settings page.
Below, you will define a bot
function such as quote_bot
. You can then
use the run_bot
function to periodically execute your bot
:
run_bot(quote_bot)
Easy
For this option, you are to create a quote_bot
that periodically posts a
quote from a file that contains your favorite sayings. This file should be
either in CSV or JSON format.
Medium
For this option, you are to create a fanboy_bot
that simulates a fanboy
of another Twitter user. Every time the other user posts a status
update, your bot should favorite the status and retweet it.
You will need to use the user_timeline method to retrieve statuses.
You will need to use the create_favorite method to create a favorite.
You will need to use the retweet method to retweet a status.
Easy
For this option, you are to create a troll_bot
that trolls another user
by sending a random message to that user whenever they update a status.
The bot should pull the trolling message from a CSV or JSON file.
You will need to use the update_status method to post a message.
You will need to use the user_timeline method to retrieve statuses.
Hard
For this option, you are to create a reddit_bot
that posts the title
and URL of any new articles from Reddit back on Twitter.
You will need to use the update_status method to post a message.
You will need to use requests to fetch JSON data from Reddit.
Medium
For this option, you are to create a leet_bot
that translates any direct
messages sent to you into Leet.
You will need to use the direct_messages method to get a list of direct messages.
You will need to use the send_direct_message method to send a direct message.
Hard
For this option, you are to create a caesar_bot
that translates performs
a Caesar shift on any direct
messages sent to you.
That is people should be able to send you some unencrypted text that your bot decrypts, or conversely send you encrypted text that your bot decrypts.
You will need to use the direct_messages method to get a list of direct messages.
You will need to use the send_direct_message method to send a direct message.
Hard
For this option, you are to create a image_bot
that takes an image posted
by another user processes using a transformation from Notebook 09 and
then uploads the transformed image as a status update.
You will need to use the user_timeline method to retrieve statuses.
You will need to use the update_with_media method to upload an image status.
To submit your notebook, follow the same directions for Notebook 01, except store this notebook in the notebook10 folder.