I came across this article on building your own Twitter bot and I decided I wanted to try it. (The article is a little out of date now; if you want to try it too, the author recommends using this guide instead.)
Before starting on any code, I created a new Twitter account for my bot with the handle @capnsparrowbot. Using that account, I created a new Twitter application.
My work environment is set up using c9.io, a cloud-based IDE that has several default enviroments. I cloned the
twitter-bot-bootstrap project and changed the defaults: the Twitter account handle, and the API keys.
There were three things I wanted the bot to do: tweet random Jack Sparrow quotes, respond to people who say “Jack Sparrow” but not “Captain Jack Sparrow,” and retweet tweets that say “Captain Jack Sparrow.”
Getting it to tweet out at intervals was easy, but finding a source of Jack Sparrow quotes was not. There are plenty of APIs that allow you to programmatically retrieve random quotes, and I even found one that provides random movie quotes — but it didn’t allow for specifying the movie, character, or actor. So I found a list of all Jack Sparrow’s lines in the movies and converted them to JSON format so my bot could retrieve them. (Yes, manually. Yes, it was tedious. And yes, it was worth it. It also made me want to watch the movies again.)
The Twitter REST API allows you to specify phrases and exclude words in a search query, which is nice. But what’s not nice is the fact that you can’t specify search only tweet content and not user names, so some search results returned tweets that had nothing to do with Jack Sparrow, but “Jack Sparrow” was somewhere in the user name. I ended up hard-coding an extra check on the results’ tweet content, just to be sure. (I also had to include a check against the handle itself, in the event that my bot tried retweeting one of its own tweets.)
The free plan on c9.io won’t allow a server instance to run forever — it’ll shut itself down after a few hours — and I wasn’t about to plunk down any money on this little endeavor. So I needed to find a place for my bot’s code to live where it could run. I settled on Heroku, but not exactly following the directions in twitter-bot-bootstrap’s readme.
I created a public repository on GitHub for my bot, with the same name as its handle (and Twitter API app). Instead of setting up the app with Heroku git (as per the readme), I connected it to my GitHub repo. I also set up the app with automatic deploys: every time I commit a change to the repo, it automatically gets pushed out to the Heroku app.
One last thing. Before you run off to Twitter to post something about Jack Sparrow, I’ll tell you not to expect a response. The bot does a search for new tweets every couple hours, and it only responds to one tweet every time it runs.
If I had my way, it would respond to every tweet, and the response time would be instantaneous. But according to Twitter’s terms of service, accounts that churn out tweet after tweet will get banned for spamming. I don’t want my bot to get banned, thus it runs every two hours.