Build the future of communications.
Start building for free

Calling Rails 5: Getting started with Twilio on Rails

I’m sad I missed RailsConf 2016 in Kansas City but it got me thinking about Rails again. Rails 5 RaceCar 1 was just released and there is a release candidate around the corner, so it’s about time to see how it plays with Twilio.

Let’s build a quick Twilio application with Rails 5. You’ll need Ruby installed, a Twilio account and a Twilio number. If you don’t already have one, you can sign up for a free Twilio account now. If you want to test this locally, you’ll need ngrok too. I’m a big fan of ngrok and I’m sure you will be too.

A brand new set of tracks

We’ll start by installing Rails 5. Open up your terminal and get yourself the latest and greatest.

$ gem install rails --version 5.0.0.rc1

Let’s create a new Rails application.

$ rails new calling-rails
$ cd calling-rails

We can check out a new feature of Rails 5 already at this point by running the tests with the new, all encompassing rails command.

$ rails test

Sure, we have no tests and if you’ve been using Rails 4 it looks the same as the old rake test command, but the difference is that in Rails 5 all commands will live under the rails command instead of some under rails and some under rake. We can’t see it yet, but the test runner has actually picked up some very nice upgrades along the way as well.

We can run our Rails server at this point too. Just enter:

$ rails server

on the command line and open up http://localhost:3000.

The successful Rails install page. It says 'Yay! You're on Rails!'.

Yay! Check out the new branding and illustration for the successful install page.

Adding gems

Certain things haven’t changed with this latest version of Rails. To add the twilio-ruby gem, stop the server then open up the Gemfile, add the line:

gem "twilio-ruby"

and install the gem using Bundler.

$ bundle install

Taking control

To get our Twilio app off the ground, we’re going to need a controller.

$ rails generate controller twilio

Open up app/controllers/twilio_controller.rb and let’s build a voice endpoint for our Twilio number. The idea is that when a call is made to our Twilio number, Twilio will send an HTTP request to our application to ask what to do with it. This action will respond to that request with the instructions in TwiML, which is a set of XML tags and attributes that Twilio understands.

First, we need to disable verifying the Rails authenticity token. This is intended to stop CSRF attacks, but that doesn’t apply when we’re expecting to receive a webhook from an external service. We also have our own way of verifying a request came from Twilio using Rack middleware.

class TwilioController < ApplicationController
  skip_before_action :verify_authenticity_token

Now we can write an action to respond to an incoming phone call. We’re going to build up some TwiML to speak a response to us down the phone, send us a congratulatory SMS message and play us out with a favourite tune.

class TwilioController < ApplicationController
  skip_before_action :verify_authenticity_token

  def voice
    response = do |r|
      r.Say "Yay! You're on Rails!", voice: "alice"
      r.Sms "Well done building your first Twilio on Rails 5 app!"
      r.Play ""
    render :xml => response.to_xml

We’re using the Twilio Ruby library’s TwiML builder (which is actually based on the builder gem) to generate our XML which we then render with the correct content type using the Rails render method.

Routing requests

In order to serve our request from Twilio, we’ll need a route too. Open up config/routes.rb and enter:

Rails.application.routes.draw do
  post 'twilio/voice' => 'twilio#voice'

Now a POST request to our application at the path /twilio/voice will route through to the action we just wrote. Start the server and we’ll get Twilio POSTing those requests.

$ rails server

Tunnelling with ngrok

We need to make our local application available to the internet so that Twilio can get through to it. I like using ngrok to do this, so I just open a new terminal window and type:

$ ngrok http 3000

When ngrok starts, it looks like this. Grab the URL you can see, we’re going to need that right away.

The ngrok live console shows a URL that is forwarding to your application on localhost. You will need to copy the HTTP URL to use in the Twilio console.

We need the URL, along with the route we created earlier, so that we can tell Twilio what URL to call when we phone our number. Jump into your Twilio console, find or buy a number that you want to use for this (make sure it can receive phone calls) and edit the number.

Find the Voice section, make sure you have Webhooks/TwiML selected as your configure option. Then, for when a call comes in, enter the full URL to your voice action.

Fill in the ngrok URL combined with the path to the action that we created to the field in the number editing page under Voice : 'a call comes in'.

Save the settings, whip out your phone and dial up your number. Soon enough you’ll be hearing the celebratory tones of a successful Twilio call.

Rocking on Rails 5

Now we’ve built our first Rails 5 and Twilio app there’s much more to be done. You could read up on all the other changes between Rails 4 and 5, particularly the brand new, real time ActionCable. It seems like a useful candidate for making a snappy user interface for two factor authentication with Authy OneTouch.

If you want to talk more about Ruby, Rails and communications then SIGNAL is the place to be. Not only are there over a hundred sessions on topics from real time communications to security and even bots, but we have two members of the Rails Committers team speaking. Eileen Uchitelle will be covering common software vulnerabilities and Sean Griffin will be diving into the hidden semantics of the code we write. I’ll be there too, and while I haven’t managed to get a commit into Rails yet, I do have a promo code that will get you 20% off to join Eileen and Sean in just two short weeks. Register now and use the code PNASH20 at checkout.

I hope to see you at SIGNAL. If you need me before, just drop me a comment below, hit me up on Twitter or grab me over email

Sign up and start building
Not ready yet? Talk to an expert.