Mashbot kit index

Love Notes and Intimate Circuits

Helen J. Burgess
North Carolina State University


Instruction Set: Creating Your Twitterbot

Mashbot's Mascot. 3D Printed from the Makerbot pattern, on my old Solidoodle.

In this section, I'll describe a couple of methods for building your own Twitterbot. There are many ways to make these small automated tweeting programs, using many different languages (most often, Python, Ruby or PHP), but all bots aim to fulfil the same two functions of composition and inscription:

  1. generating a 140-character string text, either in response to other twitter traffic or using an external corpus of text (for example, a book)
  2. connecting to Twitter via its API (architecture programming interface) and posting a tweet.

I'll put tutorials here for two different strategies for building a twitterbot: the first, Zach Whalen's method using Google Spreadsheets, and the second, a Python version which makes use of several other open source Python libraries and scripts. MashBOT is currently running on Whalen's SSBOT spreadsheet, while his sister bot Anna Coluthon (@annacoluthon) uses Python.

Things you'll need:

  • a new twitter account for your bot (see below to set it up)
  • A Google account with Google Docs (for the Whalen method)
  • Your own hosting, with shell access, a Github account, and Xcode or other tools for creating and debugging Python scripts (for the Python method)

Setting Up Your Twitter Authentication

No matter which version you choose, the first thing you'll need to do is make up a new Twitter account for your bot, and then create a Twitter App that grants permission to access the account and push text to it. Think of it as Twitter's gatekeeper to the outside world.

Setting up a new Twitter account is pretty straightforward - the only thing to keep in mind is that you'll need a mobile phone number to do it. If you have an existing Twitter account and your phone number is already in use, go to settings > mobile and delete the phone number there. Then, log out of your personal account and use the newly freed-up mobile number to create account. Later on, you can remove the phone number from your bot account and add it back to your personal account. No drama.

  1. Once you have your brand new twitterbot account, go to apps.twitter.com and click on "create new app." The interface will look like this:
  2. Enter a name for your app (anything will do), a description, and a website (you can use your own website or anything really). Leave "Callback" blank for now, but you'll use it later. Agree to the TOS and click create.
  3. Once you have your app done, your screen will look something like this:
  4. Click on the "Keys and Access Tokens" tab. You'll see a list of numbers which you should save:
    • Consumer Key
    • Consumer Secret
    • Access Token and Access Secret (to get these, clink on "create my access token" at the bottom of the page).
    Make note of all four codes: You'll need them in order to create your bot.

Bot Method I: Using Google Spreadsheets

(Jump to the Python bot method)

To use this method, you'll need access to Google Docs so you can make use of their online spreadsheets. Zach Whalen has built a spreadsheet and scripts that will automate all the hard work for you; he provides very comprehensive instructions on his web page here (http://www.zachwhalen.net/posts/how-to-make-a-twitter-bot-with-google-spreadsheets-version-04/, so I'll keep it brief.

  1. The first thing you'll need to do is get a copy of the spreadsheet (called SSBOT) and save it to your own Google Apps account. You can find the sheet here: https://docs.google.com/spreadsheets/d/1Cbg_6pYN04XtDHpDLtxAP3ExQEBL8PYBXBQ1E5_Sq30/copy. Click on "copy" and this spreadsheet will be saved to your Google Docs account under "sheets."
  2. Next, enter your app information into the spreadsheet. Whalen has commented the sheet super-well, so I'll just hit the main points with line numbers.
    • Name of twitter account on line 9
    • Consumer key (line 23)
    • Consumer secret (line 26)
    • Project key (line 32). Click on Tools > Script Editor and then File > Project Properties. You'll find your project key under "info" in the popup box.
    • finally, scroll down to line 37, and copy the Callback URL in the red box. Switch back to your twitter app (apps.twitter.com), and enter that number into the box under "Callback URL" inside your app.
  3. OK, once you're done with the numbers, decide what style you want to tweet in. Whalen has provided three different options for you to try out:
    • a highly structured version that will assemble specific parts of sentences to create a somewhat grammatically correct tweet from the spreadsheets row or columns
    • an _ebooks style version (find out more about the history of the _ebooks style here: https://en.wikipedia.org/wiki/Horse_ebooks); and
    • a "Markov" style version that will take an existing corpus of text and rewrite it according to a mathematical formula called "Markov Chains" (more about Markov Chains here: http://setosa.io/ev/markov-chains/)
    Under line 43, you'll see a dropdown menu where you can choose which one you want to use. MashBOT is using the Markov script to generate text; I used this particular style because it tends to create weird, partial, nongrammatical tweets that reflect the unfinished convolutions of love and desire.
  4. Finally, choose how often you want it to tweet. Be conservative here; Twitter has low patience for high traffic bots.

Feed the machine and tweet

Ok, now your spreadsheet is set up and you can give it some text to work with.

  1. You'll see options listed on tabs at the bottom of the browser window. Select the one you chose to use; in MashBOT's case, Markov.
  2. Under the Markov tab you'll see Whalen has put in a goodly amount of text (actually, it's the entire text of Sense and Sensibility). I'm replacing this text with a much shorter collection of short phrases taken from Bruno Latour and Roland Barthes. You can see my corpus here: MashBOT Corpus
  3. Ready to go? Go to the top of the page, click on Bot > Start Posting Tweets. Check your twitter account and watch the magic unfold!

Bot Method II: Python

This is more for the nerds among you who have access to shell hosting, have a Github account, and like to play around with Python.

You'll need:

Instructions

  1. First, you'll need to install Markovify and Tweepy in your Python directory. You can use pip to do this. If you're a Mac user and have both Python 2 and 3, make sure you're installing the modules in the correct Python directory.
  2. Next, download markov.py, corpus.txt, history.txt and keys.py. You'll need to change:
    • The text inside corpus.txt to your own corpus
    • The keys inside keys.py to the values given to you while you were setting up your Twitter app
  3. Run the script on the command line with Python, and watch the magic happen!
  4. Once you know the script works, you'll probably want to put it online and set up a cron job to run it regularly from your server. See addendum below for useful links to do this.

*Addendum* Every environment is different. I use Dreamhost so I thought I'd put in a few useful links on your way to setting up the script on your host and running it:


Is your bot tweeting? On to the final step: sending the tweets to the printer!


Mashbot kit index