Posted on by & filed under geek, javascript, programming, robots, Tech, twitter.

A Twitter bot is a program that composes and posts tweets without any human intervention. They can be purely utilitarian (@safaribot announces new and popular content added to Safari), they can be artistic (@pentametron assembles rhyming couplets out of existing tweets), or they can be utterly surreal (@autocharts and many more by Darius Kazemi). We’re kind of obsessed with them.

Because Twitter has a well-defined REST API, it’s possible to write a bot in any programming language, but there are some excellent starter projects in JavaScript which you should consider using.

Getting set up

I recommend three preliminary steps before writing a line of code:

  1. Get a Gmail identity for your bot.
  2. Open a Twitter account for the bot using #1.
  3. Register an “app” for the bot.

It used to be that you needed nothing more than a unique email address to create a Twitter bot, and there are plenty of services to generate a throwaway email address. But recent changes to Twitter policy require a unique phone number for any account with a registered app, and that means your simplest choice is to open a Gmail account for the bot and register a Google Voice number for it. Route the Google Voice number to your actual mobile phone, because you’ll need to receive at least one verification text.

Next, open a new Twitter account for the bot, and assign its Gmail and Voice values to it.

Now, while logged in to Twitter as the bot, register an app. It might seem strange to do so as you’re not really creating an application (like a Twitter client), but registering an app is how you get the various API keys you’ll need. There are more details on this step in the examplebot project from Darius.

Set up your project

Cover image for Programming JavaScript Applications

From here you could use any programming language that has a good OAuth library, but I strongly recommend writing your bot in JavaScript and bootstrapping with grunt-init-twitter-bot. The repository has a nice helper script and comes bundled with a lot of useful libraries and code samples. Pretty much out of the gate you’ll be able to:

  1. Connect to the rich Wordnik API to make use of some very sophisticated dictionary lookups and other word-based operations, useful for composing tweets programmatically.
  2. Connect to any arbitrary site over HTTP
  3. Search Twitter itself
  4. Filter out undesirable words

You’ll need to install Node.js (on OS X I like Homebrew for this) and then grunt-init.

Cover image for Instant Dependency Management with RequireJS How-to

Even though the project itself is small, it makes use of some fairly sophisticated and popular JavaScript packages like RequireJS, Underscore, and techniques like deferreds. Writing a Twitter bot in JavaScript can also teach you some useful practices in “real” web development.

Once you follow the instructions in the install script and configure your API keys, you should be able to get your Twitter bot making some test tweets. Experiment and be creative!

Running automatically

Cover image for Professional Heroku Programming

Depending on the scope of your project, you may have fun just running the bot manually from time to time. If you want it to run continuously, you’ll probably need to deploy it to an external host somewhere. Heroku is a popular option for deploying NodeJS applications, and can be quite inexpensive or even free. Detailed instructions on getting a Twitter bot onto Heroku (and writing one in Python or Ruby) are provided in this article by Lauren Orsini at ReadWrite.

Please follow the guidelines set by Twitter on writing automated bots, and have fun!

Tags: bot, Javascript, NodeJS, robot, twitter,

Comments are closed.