Created
February 17, 2011 23:52
-
-
Save kyleburton/833002 to your computer and use it in GitHub Desktop.
Better IVR Protoype: using twilio
This file contains hidden or 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
class TwillioController < ApplicationController | |
def digits | |
params['Digits'] | |
end | |
def twaction m | |
"http://snapclean.me:3000/twillio/#{m.to_s}" | |
end | |
def send_back &block | |
t = TWML.new do | |
instance_eval &block | |
end | |
render :text => t.twml | |
end | |
# (415) 599-2671 PIN 3847-9755 | |
def start | |
c = self | |
send_back do | |
say "Thankyou for calling the Card Issuer Prepaid activation center." | |
say "Mobilized by Relay Network." | |
gather(:action => c.twaction(:ismobilep), :digits => 1) do | |
say "Press 1 if you are calling from your mobile phone." | |
say "Otherwise press 2." | |
end | |
end | |
end | |
def ismobilep | |
c = self | |
send_back do | |
say "Thank You." | |
gather(:action => c.twaction(:provide_cardnumber), :digits => 16) do | |
say "Please enter your 16 digit card number." | |
end | |
end | |
end | |
def provide_cardnumber | |
c = self | |
if !digits.empty? && digits.size == 16 | |
send_back do | |
say "Thank you, your card has been activated. Good bye." | |
hangup | |
end | |
else | |
send_back do | |
say "We're sorry, but that is not a valid card number." | |
gather(:action => c.twaction(:provide_cardnumber), :digits => 16) do | |
say "Please enter your 16 digit card number." | |
end | |
end | |
end | |
end | |
end | |
class TWML | |
attr_accessor :twml | |
def self.response &block | |
self.new do | |
instance_eval &block | |
end | |
end | |
def initialize &block | |
@twml = "<Response>\n" | |
instance_eval &block if block_given? | |
@twml += "</Response>\n" | |
end | |
def say phrase | |
@twml += "#{@indent}<Say>#{phrase}</Say>\n" | |
end | |
def gather opts={}, &block | |
@twml += "<Gather action=\"#{opts[:action]}\" numDigits=\"#{opts[:digits]}\">\n" | |
instance_eval &block if block_given? | |
@twml += "</Gather>\n" | |
end | |
def hangup | |
@twml += "<Hangup/>\n" | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment