Tropo WebAPI Development Guide Home  |  Frameset Home

  Concepts & Vocabulary  |  TOC  |  Tutorials  

Free Form Text


Grammars are a great tool for limiting user input and getting the precise meaning behind a caller’s response. But there are times when collecting text input where we simply want to know what the user typed word for word. Tropo’s simple grammar notation has built-in support for this via its [ANY] grammar. The following example receives a text message, asks for a status update and logs the result. Note there are two ask methods in the example; one is blank. Because we're using the [ANY] grammar, any text message sent will satisfy the grammar's requirements. However, since the first text message is actually the activator for the application - the catalyst to get it going - rather than an answer to our question, we need to ignore it. The blank ask handles that for us.

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

post '/index.json' do
  
  t = Tropo::Generator.new
  
  t.ask :name => 'catch', 
        :say => {:value => ""},
        :timeout => 60,
        :choices => {:value => "[ANY]"}
        
  t.on :event => 'continue', :next => '/continue.json'  
    
  t.response
  
end

post '/continue.json' do
  
  t = Tropo::Generator.new
    
  t.ask :name => 'status', 
        :say => {:value => "What's your status?"},
        :timeout => 60,
        :choices => {:value => "[ANY]"}        
  
  t.response
  
  t.on :event => 'continue', :next => '/finish.json' 
  
end

post '/finish.json' do
  
  v = Tropo::Generator.parse request.env["rack.input"].read
  
  t = Tropo::Generator.new
  
  value = v[:result][:actions][:status][:value]
  
  t.say(:value => "Status returned: " + 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("");
	var choices = new Choices(null, null, "[ANY]");

	var say2 = new Say("What's your status?");
	var choices2 = new Choices(null, null, "[ANY]");

	// (choices, attempts, bargein, minConfidence, name, recognizer, required, say, timeout, voice);
	tropo.ask(choices, null, null, null, "the_ask", null, null, say, 60, null);
	tropo.ask(choices2, null, null, null, "the_ask2", null, null, say2, 60, null);

	tropo.on("continue", null, "/continue", true);

	res.send(TropoJSON(tropo));

});

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

    var value = req.body['result']['actions'][1]['value'];
	
    tropo.say("Status returned: " + 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();

	// Set up options for input.
	$options = array("choices" => "[ANY]", "name" => "catch", "timeout" => 60);
	$tropo->ask("", $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();
	
	$options2 = array("choices" => "[ANY]", "name" => "status", "timeout" => 60);
	$tropo->ask("What's your status?", $options2);
	
	$tropo->on(array("event" => "continue", "next" => "hello_world.php?uri=finish"));

	$tropo->RenderJson();
}

dispatch_post('/finish', 'app_finish');
function app_finish() {

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

run();

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

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

	t = Tropo()

	t.ask(choices = "[ANY]", timeout = 60, name = "catch", say = "")

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

	return t.RenderJson()

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

	t = Tropo()

	t.ask(choices = "[ANY]", timeout = 60, name = "status", say = "What's your status?")
	t.on(event = "continue", next ="/finish")

	return t.RenderJson()

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

	r = Result(request.body)        

	t = Tropo()

	value = r.getValue()

	t.say("Status returned: " + value)

	return t.RenderJson()

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

To be clear, the [ANY] grammar will definitely not work with a voice application - it can only work across the text channel.

Note that if you use PHP, the previous 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:

Covered all the QuickStarts? Ready for some real world applications?

Next Step: Tutorials



Additional Reading

More information on the ask method can be found here.


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

login
  Concepts & Vocabulary  |  TOC  |  Tutorials  

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