Sunday, October 27, 2013

Mechanical Bird - Twilio-Rails and Figaro Gems

For my first project at GA I wanted to create a web app that would simply send a random poem by phone to whatever phone number was inputted by the user. I called it Mechanical Bird because my poet friend Denver Butson has used it in his poetry and to me it evoked a mix of Old Tyme message sending (which if you have ever watched an episode of Game of Thrones, you know involved birds as the high tech messenger device). I felt the Twilio API was a mechanical messenger bird of sorts and I wanted to be able to tap into that feel and collaborate with Denver on a mechanical poetry project.

The Twilio Docs can be difficult to parse but they are super helpful over there and answered my questions right away. My first step was to get an .rb file to send me a poem. Denver has recordings of himself reading his poetry up on Soundcloud so I wanted to use Twilio API to connect to the recording directly. No Go. Soundcloud uses .m4a format and Twilio cannot send that format so I simply downloaded the file, then reformatted it to .mp3 using the free services of Media.io.

Once I had the mp3, I uploaded it to my server and the Twilio API was able to find it there and send it by phone. Seriously, I almost cried the first time I heard the poem being recited over my phone. It was so exciting. I used the Twilio-Rails gem to facilitate and integrate the API into my Rails app. The main brain to the app lives in my numbers_controller.rb file in a method I created called send_poem:
def send_poem(number)    
   # super-secret Twilio authorization keys    
   account_sid = ENV["account_sid"]    
   auth_token = ENV["auth_token"]
     
   # set up a client to talk to the Twilio REST API    
   client = Twilio::REST::Client.new account_sid, auth_token
   client.account.calls.create(      
      :from => '+13479605724',   # From your Twilio number      
      :to => number,     # To any number      
      # Fetch instructions from this URL when the call connects      
      :url => 'http://myserver.com/recording1.mp3'    
  )
end

The other crucial gem I used was Figaro because I needed to hide my Twilio authorization keys. Once you have Figaro in your Rails app run :
rails generate figaro:install 
and it creates a special file where you put your keys. This file is ignored when you push to Github. It was very easy to use, just don't forget to run :
rake figaro:heroku 
when you push to Heroku.

As of this writing, the user:
1. Signs In.
2. Is taken to a page where they can input  number and send a poem.
3. Can see a list of all the numbers they have sent a poem to.

I would like to:
1. Have the app send a random poem from many in my server.
2. Have a name associated with the number so the user can have a telephone book of sorts and see who     she/he has sent poems to and re-send as well.
3. I have many, many more ideas that will be added when I have time...and oh yeah, style it decently.

Now that the app works, Denver will be writing/re-recording poems specifically for the app. I want it to be more of a collaboration and have the poetry speak to the intimacy of being read into your ear as well as the practical constraints of it, in terms of a poem arriving to you as a phone call (how long should the poem be and how much will it cost my Twilio account :) .


No comments:

Post a Comment