Post image for Twilio API and Rails 3 example — Archived thumbnail, 66×66. Full-size article image was not recovered.
I recently created a simple Rails app based on the Twilio API (which is awesome) and the Twilio “appointment reminder” demo. Twilio allows developers to incorporate telephony into their web apps. You can use it to allow users to make phone calls or send SMS to your app or have your app call or SMS users, plus features such as audio playback and recording. Twilio has a Ruby on Rails specific tutorial on their site but it is not written for Rails 3 and so there were some small changes that had to be made. One of the changes is that the url that Twilio calls for the TwiML file needs a .xml extension:
Historical source code and transcript text. Live Gist files may differ from the original embedded revisions; no archived code is executed.
callme_controller.rb
Live source companion; exact historical-revision equivalence is unverified. Code was preserved, not executed.
# your Twilio authentication credentials
ACCOUNT_SID = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXX'
ACCOUNT_TOKEN = 'YYYYYYYYYYYYYYYYYYYYYYYYYYYY'
# version of the Twilio REST API to use
API_VERSION = '2010-04-01'
# base URL of this application
BASE_URL = "localhost:3000" #production ex: "http://appname.heroku.com/callme"
# Outgoing Caller ID you have previously validated with Twilio
CALLER_ID = 'XXX-XXX-XXXX'
class CallmeController < ApplicationController
def index
end
# Use the Twilio REST API to initiate an outgoing call
def makecall
if !params['number']
redirect_to({ :action => '.', 'msg' => 'Invalid phone number' })
return
end
# parameters sent to Twilio REST API
d = {
'From' => CALLER_ID,
'To' => params['number'],
'Url' => BASE_URL + '/hellomoto.xml',
}
begin
account = Twilio::RestAccount.new(ACCOUNT_SID, ACCOUNT_TOKEN)
resp = account.request(
"/#{API_VERSION}/Accounts/#{ACCOUNT_SID}/Calls",
'POST', d)
resp.error! unless resp.kind_of? Net::HTTPSuccess
rescue StandardError => bang
redirect_to({ :action => '.', 'msg' => "Error #{ bang }" })
return
end
redirect_to({ :action => '', 'msg' => "Calling #{ params['number'] }..." })
end
# TwiML response that says the hellomoto to the caller and presents a
# short menu: 1. repeat the msg, 2. directions, 3. good bye
def hellomoto
@postto = BASE_URL + '/directions.xml'
respond_to do |format|
format.xml { @postto }
end
end
# TwiML response that inspects the caller's menu choice:
# - says good bye and hangs up if the caller pressed 3
# - repeats the menu if caller pressed any other digit besides 2 or 3
# - says the directions if they pressed 2 and redirect back to menu
def directions
if params['Digits'] == '3'
redirect_to BASE_URL + "/goodbye.xml"
return
end
if !params['Digits'] or params['Digits'] != '2'
redirect_to BASE_URL + "/hellomoto.xml"
return
end
@redirectto = BASE_URL + '/hellomoto.xml',
respond_to do |format|
format.xml { @redirectto }
end
end
# TwiML response saying the goodbye message. Twilio will detect no
# further commands after the Say and hangup
def goodbye
respond_to do |format|
format.xml
end
end
end