The bots are coming! Twitter bots!
24 Jun 2014When I accepted a spot in the Flatiron School Ruby005 class, I knew my social life and, uh, casual internet-browsing life (?) would be cut dramatically down while I would be focusing all of my efforts on learning Ruby. I didn’t want my friends to miss me too much (which is hard, since I also had to move from South Carolina to New York), so I decided to build a twitter bot that could tweet and talk to my friends.
Fortunately, there’s already a Ruby gem that does all of the work for me. So I didn’t really have to build a bot, I could just launch one! I used the gem found here and pulled in the example bot’s code. When I got stuck, I referenced this bot tutorial. Setting up the bot and modifying the code was easy, but I hit a few snags trying to cooperate with the Twitter app API and OAuth.
Here’s how the twitter_ebooks gem works.
- The gem pulls in your twitter history in json format.
- Then, it parses through the json data and identifies words and phrases according to grammatical structure.
- It randomly generates sentences based on a user’s tweet history. A cron job is set up to tweet randomly once a day, but I bumped that up to once an hour.
- It knows how to respond to other users when they tweet at the bot.
- It has a keyword list and will favorite or retweet tweets in its timeline that use those keywords.
Here are a few examples of what my bot has to say:
CSS is in this instance, or is, at minimum, I'll take 6.
— ¡ɹǝʍǝlq ʎǝlɥs∀ (@ablwr_ebooks) June 25, 2014
...Damn it, iPhone, I hate it when you get a job at a grocery store.
— ¡ɹǝʍǝlq ʎǝlɥs∀ (@ablwr_ebooks) June 21, 2014
Tumblr adding fake film grain, tulips, u-matic tape players, cleaning my ears with a q-tip, maps...
— ¡ɹǝʍǝlq ʎǝlɥs∀ (@ablwr_ebooks) June 18, 2014
But the best part of having a Twitter bot is that it can interact with other users! Classmate Randall asked her for some advice:
@ablwr_ebooks What should I do this weekend?
— Randall Reed Jr. (@randallocalypse) June 20, 2014
@randallocalypse I hope this suggests a return of the insane hotels.
— ¡ɹǝʍǝlq ʎǝlɥs∀ (@ablwr_ebooks) June 20, 2014
That’s my bot! Have your bot add my bot at @ablwr_ebooks. Stay tuned for some more technical blog posts about working through the Twitter authentication steps, understanding CRON, modifying a bot, deploying to heroku, and ~mad science experiments~.