Last active
December 20, 2015 01:59
-
-
Save xmjw/6053357 to your computer and use it in GitHub Desktop.
Sample code for Twilio IVR Blog. Demonstrates how to create a run a simple IVR System.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
get '/build' do | |
Step.create_tree | |
"DONE" | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# We can use this to setup the database, | |
configure do | |
#Finalize and store our models. | |
DataMapper.finalize.auto_upgrade! | |
#Create some steps... | |
if Step.all.count == 0 | |
Step.create_tree | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
DataMapper.setup(:default, ENV['DATABASE_URL']) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
post '/fin' do | |
Metric.create(cid: params[:CallSid], state: params[:CallStatus]) | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
get '/metric' do | |
@metrics = Metric.all | |
#erb :metric, :layout => :main_layout | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'sinatra' | |
require 'data_mapper' | |
require 'twilio-ruby' | |
require './step' | |
require './metric' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Simple Page to show all the steps. | |
get '/' do | |
@steps = Step.all | |
erb :index, :layout => :main_layout | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
post '/step' do | |
step = Step.first(:root => true) | |
Metric.create(cid: params[:CallSid], state: params[:CallStatus], step: step) | |
step.to_twiml | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Get and render a step... | |
post '/step/:id' do | |
step = Step.get(params[:id]) | |
#If we are responding to a DTMF sequence, we can simply swap out the current step, and proceed as before. | |
if params[:Digits] != nil | |
#We swap the step if the user has entered a keypress. | |
step = step.children.find {|option| option[:gather] == params[:Digits] }.action | |
end | |
Metric.create(cid: params[:CallSid], state: params[:CallStatus], step: step) | |
content_type 'text/xml' | |
step.to_twiml | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Deletes everything to give a nice clean database... | |
get '/tank' do | |
Step.all.each{|step| step.destroy} | |
Metric.all.each{|metric| metric.destroy} | |
"DONE" | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require './call_handler' | |
run Sinatra::Application |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
source 'http://rubygems.org' | |
ruby '1.9.3' | |
gem 'sinatra' | |
gem 'thin' | |
gem 'twilio-ruby' | |
gem 'pg' | |
gem 'data_mapper' | |
gem 'dm-postgres-adapter' | |
gem 'dm-timestamps' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<table> | |
<tr> | |
<th>ID</th> | |
<th>Say</th> | |
<th>Parent</th> | |
<th>Sequence</th> | |
<th>Gather</th> | |
<th>Action</th> | |
</tr> | |
<% @steps.each do |step| %> | |
<tr> | |
<td><%= step.id %></td> | |
<td><%= step.say %></td> | |
<td><%= step.parent.say if step.parent != nil %></td> | |
<td><%= step.sequence %></td> | |
<td><%= step.gather %></td> | |
<td><%= step.action.say if step.action != nil%></td> | |
<tr> | |
<% end %> | |
</table> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!doctype html> | |
<html> | |
<head> | |
<title>Twilio Demo App</title> | |
</head> | |
<body> | |
<%= yield %> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<p>Total Calls: <%= @metrics.uniq{|metric| metric.cid}.count %></p> | |
<p>Total Completed Calls: <%= @metrics.select{|metric| metric.step != nil && metric.step.goal}.uniq{|metric| metric.cid}.count %></p> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Just in case we reuse this elsewhere. | |
require 'data_mapper' | |
require './step' | |
#A model to hold the call tree. | |
class Metric | |
include DataMapper::Resource | |
property :id, Serial | |
property :cid, String | |
property :state, String | |
belongs_to :step, :required => false | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Just in case we reuse this elsewhere. | |
require 'data_mapper' | |
require 'twilio-ruby' | |
class Step | |
include DataMapper::Resource | |
property :id, Serial | |
property :say, String | |
property :sequence, Integer | |
property :gather, String | |
property :root, Boolean, :default => false | |
property :goal, Boolean, :default => true | |
belongs_to :parent, :model => Step, :required => false | |
has n, :children, :model => Step, :child_key => [ :parent_id ] | |
belongs_to :action, :model => Step, :required => false | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def self.create_tree | |
# Create the first varaint of steps... | |
topStep = Step.create(say: "Thank you for calling Twil'lio Owl Sanctuary.", sequence: 0, gather: nil, root: true) | |
owl_count = Step.create(say: "Thank you. We have 3 Owls. Three.", sequence: 0, gather: nil, goal: true) | |
operator = Step.create(say: "This is a demo, we don't really have an operator.", sequence: 0, gather: nil, goal: true) | |
Step.create(say: "To hear how many Owls we have, press 1.", parent: topStep, sequence: 0, gather: "1", action: owl_count) | |
Step.create(say: "To speak to an operator, press 2.", parent: topStep, sequence: 1, gather: "2", action: operator,) | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def to_twiml | |
#If an error occurs here, the exception will cause Twilio to read out the 'An Application Error has Occured' message. | |
twiml = Twilio::TwiML::Response.new do |r| | |
r.Say say, :language => "en-gb", :voice => "alice" | |
if children.count > 0 | |
r.Gather action: "/step/#{id}" do |gather| | |
children.each do |options| | |
gather.Say options.say, :language => "en-gb", :voice => "alice" | |
end | |
end | |
else | |
r.Say "Goodbye.", :language => "en-gb", :voice => "alice" | |
r.Hangup | |
end | |
end | |
twiml.text | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment