Tropo is part of CiscoLearn More

Handling No Input or Bad Input

What if the user never responds to your ask, responds with something other than the possible choices you’ve specified or responds with something Tropo doesn’t understand? You’d probably like to tell the user they made a bad choice, and possibly provide more information so they can respond properly. To do this, just include an on event for incomplete. As with the other ask examples, 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 => 'color', 
        :timeout => 10,
        :attempts => 3, 
        :say => {:value => "What's your favorite color?  Choose from red, blue or green."},
        :choices => {:value => "red, blue, green"}
  
  t.on :event => 'continue', :next => '/continue.json'
  t.on :event => 'incomplete', :next => '/incomplete.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][:color][:value]
  
  t.say(:value => "You said " + answer)
  
  t.response
  
end

post '/incomplete.json' do

  t = Tropo::Generator.new
  
  t.say(:value => "Sorry, that wasn't one of the options.")
  
  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("What's your favorite color?  Choose from red, blue or green.");
    var choices = new Choices("red, blue, green");

    // (choices, attempts, bargein, minConfidence, name, recognizer, required, say, timeout, voice);
	
    tropo.ask(choices, 3, null, null, "color", null, null, say, 10, null);
	
    tropo.on("continue", null, "/continue", true);
    tropo.on("incomplete", null, "/incomplete", 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.post('/incomplete', function(req, res){
	
    var tropo = new TropoWebAPI();
	
    tropo.say("Sorry, that wasn't one of the options.");
	
    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" => "red, blue, green", "name" => "color", "attempts" => 3, "timeout" => 60);
 
	$tropo->ask("What's your favorite color?  Choose from red, blue or green.", $options);

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

	$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();

}

dispatch_post('/incomplete', 'app_incomplete');
function app_incomplete() {
	
	$tropo = new Tropo();
	
	$tropo->say("Sorry, that wasn't one of the options.");
	
	$tropo->RenderJson();

}

run();

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

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

	t = Tropo()

	t.ask(choices = "red, blue, green", attempts=3, timeout=60, name="color", say = "What's your favorite color?  Choose from red, blue or green.")	
	
	t.on(event = "continue", next ="/continue")
	t.on(event = "incomplete", next ="/incomplete")
	
	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()
	
@post("/incomplete")
def index(request):
	
	t = Tropo()
	
	t.say("Sorry, that wasn't one of the options.")
	
	return t.RenderJson()

run_itty(server='wsgiref', host='0.0.0.0', port=8888)

There are many more uses and options for ask, but this covers the basics. Next, we'll get into mixing text and voice in a single application.

Additional Reading

Check out our API reference for detailed, nuts and bolts info on ask.