Twilio: Cloud Communications
diagram

twilio makes it easy for developers to add voice, SMS, and VoIP functionality into web and native mobile apps.

Your app probably has one of these:

http://myapp.com

twilio gives it one of these:

(612) 867-5309

With twilio you can:

  • Build a custom phone support system
  • Send daily deals via SMS to customers
  • Instantly connect customers on your website to sales reps with a voice call
  • Let mobile users chat in a private conference call while using your app

Every human enterprise is based on communication.

INTRODUCTIONS

Talk is cheap!

(let's make some phones ring...)

HOLY BISCUITS

(told you it was easy)

Now it's your turn!

DOers

Doing is for DOers

  • At twilio, we celebrate the DOers
  • This workshop is for DOers
  • Code > Slides
  • Results > Theory

Challenge #1

Dial "A" for "Awesome"

pizza guy
  • Use Case: Simple Call Forwarding
  • Requirements:
    • User dials in to a corporate number
    • User selects a person or department by pressing a button
    • System connects caller to that person or department

Stuff you'll need to know:

  • Webhooks
  • TwiML
  • Where to look when things break

Webhooks

Webhooks are user-defined callbacks that allow event-driven interaction between two applications over HTTP.

  • Assume a Service and a Consumer of that Service.
  • The Consumer wants to be notified when Some Event happens inside the Service.
  • The Consumer defines a URL to be requested by the Service over HTTP when Some Event happens.

twilio provides webhooks for voice calls and text messages

  • You purchase a phone number from twilio
  • For each phone number, you specify webhooks for incoming voice calls and text messages
  • When twilio receives a phone call or SMS to your number, the URLs you specified are requested over HTTP

What does twilio tell you about incoming calls?

Your URL is sent POST or GET parameters that contain information like:

  • The number calling/texting
  • a unique ID for the call/text
  • the body of a text message
  • location information for the caller (if available)

How do you tell twilio how to handle incoming calls and texts?

TwiML

Your webhook URL should return an XML document that contains instructions for how you want to handle the call or text. The set of XML tags twilio understands is called TwiML.


<?xml version="1.0" encoding="ISO-8859-1"?>
<Response>
    <Say voice="woman">Hello, world!</Say>
</Response>
                    

What can TwiML tell twilio to do?

A small sample...

<Say> Say a string of text, using our built in text-to-speech engine
<Gather> Gather input from the user via DTMF tones (generated by pressing buttons on a standard phone).
<Dial> Dial a specific number or VoIP client, connecting the caller with that number/client.
<Sms> Send an SMS text message to the incoming caller/text-er or another number.
fail

Things don't always work out like you planned

If there is an error in your TwiML or somewhere in telecom land, check the logs. There's usually detailed information there.

Challenge #1

Dial "A" for "Awesome"

operator
  • Use Case: Simple Call Forwarding
  • Requirements:
    • User dials in to a corporate number
    • User selects a person or department by pressing a button
    • System connects caller to that person or department

Plan of Attack

  • Buy a Twilio number
  • Write a Ruby server which will handle an HTTP callback from Twilio
  • Put your service on the internet using localtunnel or heroku
  • Call and test your number

Resources

Challenge #2

Kicking @$$ and Taking Orders

pizza guy
  • Use Case: Pizza Orders via SMS
  • Requirements:
    • Take (and acknowledge) order via SMS
    • Display list of orders on a web page
    • Send an SMS notification when a pizza order is on the way

Stuff you'll need to know:

  • Twilio REST API
  • Twilio Helper Libraries

The twilio REST API

  • Make phone calls
  • Send text messages
  • Get usage records
  • Buy phone numbers
  • Everything twilio can do (just about)
  • Full Docs

Security

  • HTTP Basic or Digest Authentication (not oAuth - w00t!)
  • Username: Account SID
  • Password: Auth Token
  • More details

A cURL Request


$ curl -XPOST https://api.twilio.com/2010-04-01/Accounts/AC308ac9a994b1bd8b73ca0166442236c7/Calls.json \
    -d "Url=http://demo.twilio.com/docs/voice.xml" \
    -d "To=+14155551212" \
    -d "From=+14158675309" \
    -u 'AC308ac9a994b1bd8b73ca0166442236c7:{AuthToken}'
                    

Available Helper Libraries

Same Request, With Ruby Helper


require 'rubygems'
# Download twilio-ruby from twilio.com/docs/libraries
require 'twilio-ruby'

# Get your Account Sid and Auth Token from twilio.com/user/account
account_sid = 'AC308ac9a994b1bd8b73ca0166442236c7'
auth_token = '{{ auth_token }}'
@client = Twilio::REST::Client.new account_sid, auth_token

call = @client.account.calls.create(
    :url => "http://demo.twilio.com/docs/voice.xml",
    :to => "+14155551212",
    :from => "+14158675309"
)
puts call.sid
                    

Challenge #2

Kicking @$$ and Taking Orders

pizza guy
  • Use Case: Pizza Orders via SMS
  • Requirements:
    • Take (and acknowledge) order via SMS
    • Display list of orders on a web page
    • Send an SMS notification when a pizza order is on the way

Plan of Attack

  • Buy a Twilio number (or use one you already have)
  • Write a Ruby server which will handle an HTTP callback from Twilio
  • Display a web page with all orders received
  • Click an order to indicate it has been finished
  • Use the Twilio REST API (via a helper library) to send an SMS to the orderer

Resources

Challenge #3

Making It Rain

salesperson
  • Use Case: Click To Call
  • Requirements:
    • A customer arrives on your website
    • Customer clicks a button to talk to sales
    • The customer is connected in their browser to a sales rep on the phone

Stuff you'll need to know:

  • TwiML Applications
  • Twilio Client SDKs
  • Capability Tokens

TwiML Applications

  • Re-use TwiML webhook configuration across multiple numbers. Manage TwiML apps here.
  • Also used to instruct VoIP clients on what to do when a connection is made.

Twilio Client SDKs

  • Enable VoIP communications with soft and traditional phones
  • Available for iOS, Android, and the browser
  • Browser version uses WebRTC if available, Flash if not
  • Get Started with iOS
  • Get Started with Android
  • Get Started with JavaScript (browser)

Capability Tokens

Twilio Client relies on "capability tokens" to sign communications from devices to Twilio. These tokens are a secure way of setting up your device to access various features of Twilio.

These tokens must be generated on a server, then sent to the browser or mobile device to authorize a client.

Outgoing Calls

If a client needs to make outgoing calls, it needs the "allowClientOutgoing" permission:


require 'rubygems'
require 'twilio-ruby'

# put your own account credentials here:
account_sid = 'AC043dcf9844e13758bc3a36a84c29761'
auth_token = '{auth token}'

# set up
capability = Twilio::Util::Capability.new account_sid, auth_token

# allow outgoing calls to this TwiML application
capability.allow_client_outgoing 'AP89a0180a1a4ddf1da954efca349b7a20'

# generate the token string
@token = capability.generate
                    

Incoming Calls

If a client needs to accept incoming calls, it needs the "allowClientIncoming" permission:


require 'rubygems'
require 'twilio-ruby'

# put your own account credentials here:
account_sid = 'AC043dcf9844e13758bc3a36a84c29761'
auth_token = '{auth token}'

# set up
capability = Twilio::Util::Capability.new account_sid, auth_token

# allow incoming calls to this client name:
capability.allow_client_incoming 'andrew' #could be anything

# generate the token string
@token = capability.generate
                    

Browser Configuration

The capability token is used to initialize the client in the browser like so:


Twilio.Device.setup(token);
// Make an outgoing call using the configured TwiML application
Twilio.Device.connect();
                    

Challenge #3

Making It Rain

salesperson
  • Use Case: Click To Call
  • Requirements:
    • A customer arrives on your website
    • Customer clicks a button to talk to sales
    • The customer is connected in their browser to a sales rep on the phone

Plan of Attack

  • Create a TwiML application with a Voice URL set to a server you control
  • Write a Ruby endpoint which will return a TwiML document with a Dial command (your sales rep)
  • Display a web page with a "click to call" button, using a server-generated capability token
  • Initiate a phone call with Twilio Client to your "sales rep"

Resources

Wrapping Up

  • twilio wants to help you change communications forever
  • Every application has an opportunity to communicate with users better
  • Phone, voice, and SMS interactions can be every bit as valuable as interactions on a screen

We're Here To Help!

Thank You!

@kevinwhinnery

kwhinnery@twilio.com