UPDATE 5/20/15: The scriptand the method described here may no longer work. If you’re attempting to create a Twitter bot, please use the updated instructions.

Twitter boasts about 550 million users with about 100 million of those accessing the site every 24 hours. That’s a lot of people. But when some estimates guess that as many as 20 million of those accounts are fake, we have to conclude that that’s a lot of things besides people. Some of these so-called Twitter bots publish spam, inflate client’s follower counts, or provide helpful advice. Some generate poetry.

This past Monday, Stephen Colbert featured a bot, @RealHumanPraise, that mashes up Fox News personalities with positive film reviews published at RottenTomatoes.com.

In this post, I’m going to describe the basic steps in creating a simple randomized Twitter bot using a Google Spreadsheet. You could use this to make something as poetic, interpretive, critical or playful as you like. Here are the steps:

  1. Create a Twitter account for your bot
  2. Create a Twitter application
  3. Customize a spreadsheet
  4. Unleash your bot

Step 1: Create a Twitter account for your bot

Go to twitter.com and sign up for a new account. (I do not recommend making your primary Twitter account a bot.) Call this new account whatever you want, but note that you’ll need to actually have access to the email account you sign up with, so don’t try to use a fake one. I like to use the GMail trick whereby you can append anything you want after a “+” in the name field, as in: zach.whalen+botdemo@gmail.com. I still messages sent to that address, but their easy to filter out if it becomes a spam magnet.

Obviously, use a password you can remember. After hitting the “Sign up for Twitter” button, create your username on the next screen. This must be unique, but you can always change it later. Hit “Create my Account” and check your email for the confirmation link. You can now log in and do whatever you’d like to customize the account: set the background, follow a few people, add a description — normal stuff.

Step 2: Create a Twitter application

The bot you’re going to end up with needs to have a few secret codes for integrating with your Twitter accounts. This integration is more complex than a username/password pair, so you’ll end up acquiring 2 rather longish strings of characters: a “consumer key,” and a “consumer secret.”

Create a new Application

To get started, go to http://dev.twitter.com/apps/new . You’ll be prompted to log in again, so make sure you use your new account’s credentials. Enter some basic info in the required fields — Name, Description, Website — like in the image below. Your application name has to be unique, but other fields don’t:

Enter a Name, Description, and Website.

Enter a Name, Description, and Website.

Change settings to “read/write”

Go the “settings” tab in your new application’s dashboard:

Go to the "Settings" tab

Go to the “Settings” tab

Scroll down to the “Application Type” area, and change “Access” to “Read and Write”

Set Access to "Read and Write"

Set Access to “Read and Write”

And in the “Callback URL” field, enter ‘https://spreadsheets.google.com/macros’ without the quote marks. I don’t have a screenshot of this in place, but you get what I mean. Just paste it in there.

Update the Twitter application’s settings. After a few minutes, doublecheck the “Details” tab of your new app to make sure that the OAuth access is set to Read/Write, and doublecheck the “Settings” tab to verify that the Callback URL is in place. You’re now ready to start building the bot itself!

Step 3: Make a copy of my spreadsheet template and customize your bot

I’ve created a Google spreadsheet pre-loaded with some bot code and the capability of interfacing with Twitter. You can make a copy here. Since this spreadsheet includes further setup instructions within it, I won’t provide screenshots, and I’ll just discuss the really important stuff. Mainly you’ll need to copy and paste your Consumer Key and Consumer Secret from the Twitter App details.

The fun part comes in customizing your bot’s vocabulary. Do this from the “Vocabulary” sheet that you can access from the bottom. The bot will work its way through that sheet and choose one item randomly from each row. It’s set up like a simple random poetry generator, but if you’re a little creative, I’m sure you could use this logic to create more complicated things. Add as many rows as you want, but remember you’ve only got 140 characters to work with.

Once you’ve got some vocabulary in place, go to the spreadsheet’s “Bot” menu and select “Preview Output”. You’ll be prompted to authorize the app, which you should do. Run “Preview Output” a few times to get a feel for what it makes.

Step 4: Authorize your Spreadsheet to communicate with Twitter and start posting

Again, these steps are covered in the spreadsheet itself, but some highlights are worth mentioning here.

Before attempting to update Twitter, you need to authorize this spreadsheet with your Twitter App and the Twitter account of the bot you’ve created. Make sure you’re logged in to Twitter with the bot’s account. Go to your spreadsheet’s “Tools” menu and open the “Script Editor”. This will open a new tab where you can view and edit all the code that makes the bot run. You don’t need to do anything with the code, however. Go to the Script Editor’s “Run” menu and select the first option, authenticateTwitter.

After a few moments, you’ll be prompted to authorize your app with your Twitter account. As before, you should agree to this authorization.

To test if this is working, go back to your spreadsheet, and under “Bots”, select “Send a Test Tweet”. Check your bot’s Twitter account to see if it worked. Hopefully it did! You can now set the interval you want it to post at and select “Bots->Start Posting Tweets” to start posting tweets.

If you want to change the interval later, you’ll have to re-run the Start Posting Tweets option, but to make changes to the vocabulary, just edit and add to the values in the Vocabulary sheet.

Hopefully you now have a working Twitter bot! If so, let me know what you’ve created!

[Robot Banana photo by Jim Champion (Flickr: treehouse1977), CC-BY-SA-2.0. Via Wikimedia Commons]