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.
Getting set up
I recommend three preliminary steps before writing a line of code:
- Get a Gmail identity for your bot.
- Open a Twitter account for the bot using #1.
- 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
- 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.
- Connect to any arbitrary site over HTTP
- Search Twitter itself
- Filter out undesirable words
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!
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!