Overview

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.

Partners

Both members of each pair should submit a Notebook. Be sure to identify your partner at the top of the Notebook.

Background

Before attempting the activity below, be sure to follow the Reading 10 Notebook on how to setup your Twitter account and application settings.

Activity: Twitter Bot

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.

Code Skeleton

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)

Option A. 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.

Hints

  1. You will need to use the update_status method to post a message.

Option B. Fanboy Bot

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.

Hints

  1. You will need to use the user_timeline method to retrieve statuses.

  2. You will need to use the create_favorite method to create a favorite.

  3. You will need to use the retweet method to retweet a status.

Option C. Troll Bot

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.

Hints

  1. You will need to use the update_status method to post a message.

  2. You will need to use the user_timeline method to retrieve statuses.

Option D. Reddit Bot

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.

Hints

  1. You will need to use the update_status method to post a message.

  2. You will need to use requests to fetch JSON data from Reddit.

Option E. Leet Bot

Medium

For this option, you are to create a leet_bot that translates any direct messages sent to you into Leet.

Hints

  1. You will need to use the direct_messages method to get a list of direct messages.

  2. You will need to use the send_direct_message method to send a direct message.

Option F. Caesar Bot

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.

Hints

  1. You will need to use the direct_messages method to get a list of direct messages.

  2. You will need to use the send_direct_message method to send a direct message.

Option G. Image Bot

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.

Hints

  1. You will need to use the user_timeline method to retrieve statuses.

  2. You will need to use the update_with_media method to upload an image status.

Submission

To submit your notebook, follow the same directions for Notebook 01, except store this notebook in the notebook10 folder.