Tropo WebAPI Development Guide Home  |  Frameset Home

  Asking a Question  |  TOC  |  5 Steps to Ask Complete Questions  

Asking for Digits


Tropo supports a number of simple ways to specify typical choices. For example, if you want to collect a single digit input from a user, you could do this (like the other examples for ask, this one requires a library; note that if you use PHP, the following example includes the Limonade framework, so you would need to define the end of the start URL in Tropo to include the resource name, like http://www.example.com/test.php?uri=start):

require 'tropo-webapi-ruby'
require 'sinatra'

post '/index.json' do
  
  t = Tropo::Generator.new
  
  t.ask :name => 'digit', 
        :timeout => 60, 
        :say => {:value => "Pick a number from 0 to 9"},
        :choices => {:value => "0,1,2,3,4,5,6,7,8,9"}
  
  t.on :event => 'continue', :next => '/continue.json'
  
  t.response
  
end

post '/continue.json' do
  
  v = Tropo::Generator.parse request.env["rack.input"].read
  
  t = Tropo::Generator.new
  
  answer = v[:result][:actions][:digit][:value]
  
  t.say(:value => "You said " + answer)
  
  t.response
  
end

var http = require('http');
var express = require('express');
var app = express.createServer();
var tropo_webapi = require('tropo-webapi');

// Required to process the HTTP body.
// req.body has the Object while req.rawBody has the JSON string.

app.configure(function(){
	app.use(express.bodyParser());
});

app.post('/', function(req, res){
	
    var tropo = new TropoWebAPI();
    
    var say = new Say("Pick a number from 0 to 9");
    var choices = new Choices("0,1,2,3,4,5,6,7,8,9");

    // (choices, attempts, bargein, minConfidence, name, recognizer, required, say, timeout, voice);
	
    tropo.ask(choices, null, null, null, "digit", null, null, say, 60, null);
	
    tropo.on("continue", null, "/continue", true);
	
    res.send(TropoJSON(tropo));
	
});

app.post('/continue', function(req, res){
	
    var tropo = new TropoWebAPI();

    var answer = req.body['result']['actions']['value'];
	
    tropo.say("You said " + answer);
		
   	res.send(TropoJSON(tropo));
 });

app.listen(8000);
console.log('Server running on port :8000');
<?php

require 'tropo.class.php';
require 'lib/limonade.php';

dispatch_post('/start', 'app_start');
function app_start() {

	$tropo = new Tropo();

	$options = array("choices" => "0,1,2,3,4,5,6,7,8,9", "name" => "digit", "timeout" => 60);
 
	$tropo->ask("Pick a number from 0 to 9", $options);

	$tropo->on(array("event" => "continue", "next" => "hello_world.php?uri=continue"));

	$tropo->RenderJson();
}

dispatch_post('/continue', 'app_continue');
function app_continue() {

	$tropo = new Tropo();
	$result = new Result();
	
	$answer = $result->getValue();
	
	$tropo->say("You said " . $answer);
	
	$tropo->RenderJson();

}

run();

?>
from itty import *
from tropo import Tropo, Result

@post('/index.json')
def index(request):

	t = Tropo()

	t.ask(choices = "0,1,2,3,4,5,6,7,8,9", timeout=60, name="digit", say = "Pick a number from 0 to 9")	
	
	t.on(event = "continue", next ="/continue")
	
	return t.RenderJson()
	
@post("/continue")
def index(request):
	
	r = Result(request.body)
	t = Tropo()
	
	answer = r.getValue()
	
	t.say("You said " + answer)
	
	return t.RenderJson()
	
run_itty(server='wsgiref', host='0.0.0.0', port=8888)

That would only work on the text channel though (SMS/IM); in order to work on the voice channel, "0,1,2,3,4,5,6,7,8,9" would need to be rewritten as "zero,one,two,three,four,five,six,seven,eight,nine". There's an easier and simpler way to work on both channels - you can just replace the numbered list with the [DIGITS] grammar:

require 'tropo-webapi-ruby'
require 'sinatra'

post '/index.json' do
  
  t = Tropo::Generator.new
  
  t.ask :name => 'digit', 
        :timeout => 60, 
        :say => {:value => "Pick a number from 0 to 9"},
        :choices => {:value => "[1 DIGIT]"}
  
  t.on :event => 'continue', :next => '/continue.json'
  
  t.response
  
end

post '/continue.json' do
  
  v = Tropo::Generator.parse request.env["rack.input"].read
  
  t = Tropo::Generator.new
  
  answer = v[:result][:actions][:digit][:value]
  
  t.say(:value => "You said " + answer)
  
  t.response
  
end

var http = require('http');
var express = require('express');
var app = express.createServer();
var tropo_webapi = require('tropo-webapi');

// Required to process the HTTP body.
// req.body has the Object while req.rawBody has the JSON string.

app.configure(function(){
	app.use(express.bodyParser());
});

app.post('/', function(req, res){
	
    var tropo = new TropoWebAPI();
    
    var say = new Say("Pick a number from 0 to 9");
    var choices = new Choices("[1 DIGIT]");

    // (choices, attempts, bargein, minConfidence, name, recognizer, required, say, timeout, voice);
	
    tropo.ask(choices, null, null, null, "digit", null, null, say, 60, null);
	
    tropo.on("continue", null, "/continue", true);
	
    res.send(TropoJSON(tropo));
	
});

app.post('/continue', function(req, res){
	
    var tropo = new TropoWebAPI();

    var answer = req.body['result']['actions']['value'];
	
    tropo.say("You said " + answer);
		
   	res.send(TropoJSON(tropo));
 });

app.listen(8000);
console.log('Server running on port :8000');
<?php

require 'tropo.class.php';
require 'lib/limonade.php';

dispatch_post('/start', 'app_start');
function app_start() {

	$tropo = new Tropo();

	$options = array("choices" => "[1 DIGIT]", "name" => "digit", "timeout" => 60);
 
	$tropo->ask("Pick a number from 0 to 9", $options);

	$tropo->on(array("event" => "continue", "next" => "hello_world.php?uri=continue"));

	$tropo->RenderJson();
}

dispatch_post('/continue', 'app_continue');
function app_continue() {

	$tropo = new Tropo();
	@$result = new Result();
	
	$answer = $result->getValue();
	
	$tropo->say("You said " . $answer);
	
	$tropo->RenderJson();

}

run();

?>
from itty import *
from tropo import Tropo, Result

@post('/index.json')
def index(request):

	t = Tropo()

	t.ask(choices = "[1 DIGIT]", timeout=60, name="digit", say = "Pick a number from 0 to 9")	
	
	t.on(event = "continue", next ="/continue")

	return t.RenderJson()
	
@post("/continue")
def index(request):
	
	r = Result(request.body)
	t = Tropo()
	
	answer = r.getValue()
	
	t.say("You said " + answer)
	
	return t.RenderJson()
	
run_itty(server='wsgiref', host='0.0.0.0', port=8888)

Or if you want the user to enter their four digit pin code, just replace the ask prompt with

“Please enter your four digit pin”

and change the [1 DIGIT] grammar to:


The simple syntax Tropo uses to allow you to specify possible user inputs is called, simply put, “simple grammar”. Simple grammar doesn't necessarily mean basic, however, as it can be fairly complex and comprehensive.

Next Step: 5 Steps To Ask Complete Questions



Additional Reading

To learn more about simple grammar, visit Working with Simple Grammar.


  ANNOTATIONS: EXISTING POSTS
ScottAlexander
10/7/2011 11:30 PM (EDT)
I'm trying to run this and in my browser it always says:

Cannot GET /

VoxeoGarret
10/8/2011 12:02 AM (EDT)
Hello there,

Could you let me see the code you are trying to use and/or a set of logs regarding a session where this had occurred? Once I have a little more detail I can further advise on a way to resolve this issue. Standing by.

Regards,
Garrett King

Customer Support Engineer
Voxeo Corporation

login
  Asking a Question  |  TOC  |  5 Steps to Ask Complete Questions  

© 2012 Voxeo Corporation  |  Voxeo IVR  |  VoiceXML & CCXML IVR Developer Site