| Tropo WebAPI Development Guide | Home | Frameset Home |
|
index.json, your_age.json and age_fail.json, are defined in our Sinatra application through the use of post (line 7, line 27 and line 45 of Figure 2.0):
require 'rubygems'
require 'sinatra'
require 'tropo-webapi-ruby'
use Rack::Session::Pool
post '/index.json' do
v = Tropo::Generator.parse request.env["rack.input"].read
t = Tropo::Generator.new
if v[:session][:initial_text].to_i > 0
t.ask :name => 'initial_text', :choices => { :value => "[ANY]"}
session[:year] = v[:session][:initial_text]
else
t.ask :name => 'year', :bargein => true, :timeout => 60, :required => true, :attempts => 3,
:say => [{:event => "timeout", :value => "Sorry, I did not hear anything."},
{:event => "nomatch:1 nomatch:2", :value => "Don't think that was a year. "},
{:value => "What is your birth year?"}],
:choices => {:value => "[4 DIGITS]"}
end
t.on :event => 'continue', :next => '/your_age.json'
t.on :event => 'incomplete', :next => '/age_fail.json'
t.response
end
post '/your_age.json' do
t = Tropo::Generator.new
v = Tropo::Generator.parse request.env["rack.input"].read
session[:year] = v[:result][:actions][:year][:value].gsub(" ","") unless session[:year]
today = Date.today
agecalc = today.year - session[:year].to_i
lowdate = agecalc - 1
highdate = agecalc + 1
t.say "Your age is between #{lowdate} and #{highdate}", {:name => 'test'}
t.hangup
t.response
end
post '/age_fail.json' do
t = Tropo::Generator.new
v = Tropo::Generator.parse request.env["rack.input"].read
t.say "Gotta use numbers! Please try again."
t.hangup
t.response
end
index.json resource at your Sinatra application whenever someone calls, texts, or IMs your app (or launches it using an external event, such as a link on a website; more info on external events can be found in the Sessions & Tokens section of the Guide); you saw something similar in the How It Works section earlier. The script then converts this to a hash using this line of code in the Ruby application (line 9 of both Figure 2.0 and 2.1)v = Tropo::Generator.parse request.env["rack.input"].read
initial_text sent in a text message.index.json (line 7 of Figure 2.2), which we set in the URL earlier:
require 'rubygems'
require 'sinatra'
require 'tropo-webapi-ruby'
use Rack::Session::Pool
post '/index.json' do
v = Tropo::Generator.parse request.env["rack.input"].read
t = Tropo::Generator.new
if v[:session][:initial_text]
t.ask :name => 'initial_text', :choices => { :value => "[ANY]"}
session[:year] = v[:session][:initial_text]
else
t.ask :name => 'year', :bargein => true, :timeout => 60, :required => true, :attempts => 2,
:say => [{:event => "timeout", :value => "Sorry, I did not hear anything."},
{:event => "nomatch:1 nomatch:2", :value => "Don't think that was a year."},
{:value => "What is your birth year?"}],
:choices => {:value => "[4 DIGITS]"}
end
t.on :event => 'continue', :next => '/your_age.json'
t.response
end
initial_text (which won't exist if it's the voice channel). If initial_text exists, Tropo will attempt to use the value in the logic of the application immediately. By only accepting values that are greater than zero, this ensures Tropo doesn't try to use something like "Hello" as a valid number.your_age.json resource, as defined by t.on :event => 'continue', :next => '/your_age.json' in the previous resource (line 22 in Figure 2.1). If the caller didn't response with a valid choice after 3 attempts, Tropo will redirect to age_fail.json, defined by t.on :event => 'incomplete', :next => '/age_fail.json' (line 23 in Figure 2.1).
post '/your_age.json' do
t = Tropo::Generator.new
v = Tropo::Generator.parse request.env["rack.input"].read
session[:year] = v[:result][:actions][:year][:value].gsub(" ","") unless session[:year]
today = Date.today
agecalc = today.year - session[:year].to_i
lowdate = agecalc - 1
highdate = agecalc + 1
t.say "Your age is between #{lowdate} and #{highdate}", {:name => 'test'}
t.hangup
t.response
end
post '/age_fail.json' do
t = Tropo::Generator.new
v = Tropo::Generator.parse request.env["rack.input"].read
t.say "Gotta use numbers! Please try again."
t.hangup
t.response
end
| ANNOTATIONS: EXISTING POSTS |
| login |
|