Building simple Twitter bots with Ruby!

Build your own bot

If you’re interested in building interactive web based software twitter bots are a great place to start. Twitter is a fairly open platform that can be easily access through API’s and libraries which are available for many different programming languages. Combining this access with other API’s, datasets and original content allows for thousands of possibilities and projects that can be completed with only a couple of hours of work.

Some fun example bots:

Think Piece Bot – This bot generates headlines for imaginary opinion articles.

Census American Bot – Tweets anonymized details from real US census forms.

Stealth Mountain – This bot replies to anyone who writes about a “sneak peak” rather than a “sneak peek”, a major problem among media types.

New York Times First Said – Tweets each time the NYTs uses a word for the first time in their history.

Fast Plants & News – This account tweets a picture of some hydroponics plants and associates that with news clips.

How to:

Assumes some basic ruby knowledge and use of a bash terminal (Mac OS and Linux)

  1. Set up Ruby if you don’t already have it. This guide might help (
  2. Open Terminal install the “Twitter” gem and read through some of the docs to get familiar with how it works. (
  3. Create a new email account for use with twitter and other APIs. This step is optional but recommend. I made a new Google account. (Google Account)
  4. Create new Twitter Account (Twitter sign up) and add a phone number. This is necessary to get developer credentials later. 
  5. Create new app while logged into twitter (Developer link). 
  6. Once your application is made you’ll get to this page. Click on the Keys and Access Tokens tab. 
  7. Copy down your Consumer Key (API key) and  your Consumer Secret (API Secret). Click the create Access Token button and then copy your Access Token and Access token Secret. (Keep these numbers secret) 
  8. Open up a text editor and create a new ruby file. Using the docs for the Twitter gem we can make a basic test for our account using the code below. Make sure to replace the capitalized text with the keys you copied down from twitter.
  9. Then save and run the file from terminal with “ruby [filename]”. This should post a tweet saying “I’m tweeting!” to your twitter account! Check it out on your twitter profile. 
  10. From here you have a lot of options. You can post tweets, get tweets, follow other accounts and check your followers (check the twitter gem docs for examples). I’m going to set this account up to tweet about the weather in downtown Manhattan NYC. 

Adding the content from a 3rd part API

  1.  Make a Weather Underground account. Then Go to the Weather API  and select the free options. Locate the “Purchase key” button and click it to make a new project. 
  2. Once your signed up for the Api key copy down your key ID. (Again you might want to keep this secret)
  3. Go back to your Ruby document and add some functionality to get the info from Weather Underground. Review the code below. Were going to use the net/http gem to access the data from weather underground in JSON format (Http Gem setup). Enter your Key ID and set it equal to weather_key variable. Add your preferred zip code by setting it equal to the zip_code variable. The URL variable uses string interpolation to put our request together. Here I’m getting everything I can about the current conditions, but there are a lot of different return options that can be used (Weather Underground API docs). Make sure to add the “require “net/http”” at the top.
  4. If everything is working right your ruby code should puts out a bunch of JSON to the terminal with weather information in it. 
  5. Now were going to use a JSON gem to parse the results and print a string with some of the data we got. Make sure to add the “require “JSON”” up top and put the “get_weather(url)” method call into “JSON.parse” method like this “current_weather = JSON.parse(get_weather(url))”. (See: JSON parsing) 
  6. This should print something like this to your terminal:
  7. Now lets take that string and put it into our “client.update()” method to post it to twitter! Make sure you acknowledge your data sources in some way. 
  8. Check Twitter!

We’re done!

Now you have some ruby code that will get the current weather and post it to your twitter account. If you wanted to continue this project you could set it up on a small linux computer like the raspberry pi and use a “cron” job to run this script at regular intervals. There are thousands of APIs that will work similarly to the Weather Underground and can be made to interact with twitter (APIs). Other projects might take in mentions or other twitter feed data and use that to query APIs and return results in the form of tweets. There are lots of possibilities for interaction here. Another example using a very similar approach is my bot @plants_fast on twitter (Plants Fast) . It uses a camera, raspberry pi and the news API ( in addition to controlling lighting and watering of the plants it tracks. If you found this helpful let me know in the comments or find me on twitter @superjesseh.

Leave a Reply

Your email address will not be published. Required fields are marked *