Tropo WebAPI Development Guide Home  |  Frameset Home

  Working with Simple Grammar  |  TOC  |  Free Form Text  

Concepts and Vocabulary


The following script works via the phone, text or IM. It’s a company directory that starts off by welcoming the user, then asks the user who they’re trying to reach; this can be the name of a person or department. Depending on the type selected (person or department), one of two web services is called to play back the contact information. As with the other ask examples, this one will again only work with 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 => 'directory', 
        :timeout => 15, 
        :say => {:value => "Welcome to the Tropo company directory. Who are you trying to reach?"},
        :choices => {:value => "department(support, engineering, sales), person(jose, jason, adam)"}        
  
  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][:directory][:interpretation]
  value = v[:result][:actions][:directory][:value]
  
  t.say(:value => "You said " + answer + ", which is a " + value)
  
  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("Welcome to the Tropo company directory. Who are you trying to reach?");

    // (value, mode, terminator)
    var choices = new Choices("department(support, engineering, sales), person(jose, jason, adam)");

    // (choices, attempts, bargein, minConfidence, name, recognizer, required, say, timeout, voice);
    tropo.ask(choices, null, null, null, "directory", null, null, say, 15, 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']['interpretation'];
    var value = req.body['result']['actions']['value'];

    tropo.say("You said " + answer + ", which is a " + value);

    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" => "department(support, engineering, sales), person(jose, jason, adam)", "name" => "directory", "timeout" => 15);
 
	$tropo->ask("Welcome to the Tropo company directory. Who are you trying to reach?", $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->getInterpretation();
	$value = $result->getValue();
	
	$tropo->say("You said " . $answer . ", which is a " . $value);
	
	$tropo->RenderJson();

}

run();

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


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

	t = Tropo()

	t.ask(choices = "department(support, engineering, sales), person(jose, jason, adam)", timeout = 15, name = "directory", say = "Welcome to the Tropo company directory. Who are you trying to reach?")

	t.on(event = "continue", next ="/continue")

	return t.RenderJson()

@post("/continue")
def index(request):

	r = Result(request.body)        

	t = Tropo()

	answer = r.getInterpretation()
	value = r.getValue()

	t.say("You said " + answer + ", which is a " + value)

	return t.RenderJson()

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

There’s a lot going on in this short example so let’s break it down.

In the ask, we have a few parameters - the prompt asking the user who they're trying to reach, the timeout, a name used to identify the results, and then the choices. The choices parameter instructs Tropo to listen for a set of words; in this case, department and people names. The key thing here is the notation used to define the grammar:


The words outside the parentheses are called concepts. Concepts provide a context when handling the user’s response. The company directory grammar defines two concepts: department and person. When the caller says one of the items inside the parentheses (such as support, jose, etc.), an event is triggered that gives us access to both the spoken word and the concept to which it belongs.

Want to accept any text input as valid input?

Next Step: Free Form Text



Additional Reading

More information about the ask method can be found here and here.


  ANNOTATIONS: EXISTING POSTS
0 posts - click the button below to add a note to this page

login
  Working with Simple Grammar  |  TOC  |  Free Form Text  

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