robincamille / bot-tutorial

Files for a LACUNY tutorial on building your own Twitter bot
28 stars 25 forks source link

Twitter bot tutorial

This tutorial and its materials were put together by Robin Davis (@robincamille) and Mark Eaton (github.com/MarkEEaton) for a December 15, 2015 workshop for librarians sponsored by the LACUNY Emerging Technologies Committee. You can use these materials for your own whimsical bot; the following instructions are for our workshop.

See also: Davis, Robin, and Mark Eaton. Make a Twitter Bot in Python: Iterative Code Examples. Journal of Interactive Technology and Pedagogy (Blueprints section). April 2016. (Verbose write-up featuring code in this repository.)

👉 2018 update: We updated our bot tutorial for an ALA 2018 workshop. It uses Python 3.


Python version: 2.7

Required libraries: tweepy, setuptools, json, urllib2 or urllib3

Download the files

See the "Download ZIP" button toward the upper right? Click it and save the folder to your desktop.

Create a Twitter account for your bot

  1. Go to http://twitter.com and sign up for a new account of your choosing.

    • Be sure to include your mobile number (required for using the API)
    • Email address must be unique to Twitter users; try adding random periods in your Gmail address, if you have one
  2. Go to http://apps.twitter.com and create a new app

    • This info isn't public so it can be messy
    • Go to Keys and Access Tokens
    • Create new access token
  3. Copy Consumer Key/Secret and Access Key/Secret to credentials.py

Basic bot: mybot.py

This script is a basic Twitter bot. It will tweet three things from a list inside the script.

  1. Right-click on mybot.py and select Edit with IDLE

  2. Take a look at the script; Robin and Mark will talk about what it's doing

  3. Select Run > Run Module from the window's menu bar

Change it up!

Intermediate bot: mybot2.py

This script sends out five tweets from the first five lines of an external .txt file.

  1. Right-click on mybot.py and select Edit with IDLE

  2. Right-click on twain.txt and open it in Notepad

  3. Take a look at both files; Robin and Mark will talk about what the script is doing

  4. Select Run > Run Module from the window's menu bar for mybot2.py

Change it up!

Advanced bot: mashup_madlib.py

This script treats The Red Wheelbarrow as a mad-lib, filling in three blanks from two data sources: JSON files from @dariusk's collection of corpora.

Advanced bot: respondingbot.py

This script from Mark tweets a random line from a .txt file whenever @jasonchowbot tweets.

Advanced bot: mashup_markov

This script uses a Markov chain to create new sentences from another text, and tweets them.

Now what?

Consider hosting your bot so it can tweet happily all by itself! If you happen to have DreamHost, I wrote up a step-by-step tutorial for hosting your bot: see put-your-bot-on-DreamHost.md.