Tropo WebAPI Development Guide Home  |  Frameset Home

  Passing in Parameters  |  TOC  |  Receiving Text Messages  

Receiving Text Responses


Want to send a text message and wait for a response? Below utilizes on events with theask, which allows you to ask a question with choices, wait for a reply, then send back a tailored response based on the choice the user made.

JSON alone cannot be used to pass parameters, so all the following examples include 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.call(:to => "+14075550100", :network => "SMS")
  t.ask :name => 'reminder', 
        :timeout => 60, 
        :say => {:value => "Hey, did you remember to take your pills?"},
        :choices => {:value => "yes(yes,y,1), no(no,n,2)"}
  
  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][:reminder][:value]
  
  if answer == "yes"
    t.say(:value => "Ok, just checkin.")
  else
    t.say(:value => "What are you waiting for?")
  end

  t.response
  
end

post '/incomplete.json' do

  t = Tropo::Generator.new
  
  t.say(:value => "Sorry, that wasn't on 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();
    
    //to, answerOnMedia, channel, from, headers, name, network, recording, required, timeout
    tropo.call("14075550100", null, null, null, null, null, "SMS", null, null, null);

    var say = new Say("Hey, did you remember to take your pills?");
    var choices = new Choices("yes(yes,y,1), no(no,n,2)");

    // (choices, attempts, bargein, minConfidence, name, recognizer, required, say, timeout, voice);
	
    tropo.ask(choices, null, null, null, "reminder", null, null, say, 60, 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'];
	
    if(answer == "yes") {
    	tropo.say("Ok, just checkin.");
		
    	res.send(TropoJSON(tropo));
    }
    else {
    	tropo.say("What are you waiting for?");
		
    	res.send(TropoJSON(tropo));
    }
});

app.post('/incomplete', function(req, res){
	
    var tropo = new TropoWebAPI();
	
    tropo.say("Sorry, that wasn't on 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();

	$tropo->call("+14155550100", array('network'=>'SMS'));

	// Set up options for input.
	$options = array("choices" => "yes(yes,y,1), no(no,n,2)", "name" => "reminder", "timeout" => 60);
 
        // Ask the caller for input, pass in options.
	$tropo->ask("Hey, did you remember to take your pills?", $options);

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

	$tropo->RenderJson();
}

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

	$tropo = new Tropo();
	@$result = new Result();
	
	$choice = $result->getValue();
	
	if ($choice == "yes") 
		{
		$tropo->say("Ok, just checkin.");
		}
	else
		{
		$tropo->say("What are you waiting for?");
		}
	
	$tropo->RenderJson();

}

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

}

run();

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

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

	t = Tropo()

	t.call(to="+14155550100", network = "SMS")
	t.ask(choices = "yes(yes,y,1), no(no,n,2)", timeout=60, name="reminder", say = "Hey, did you remember to take your pills?")	
	
	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()
	
	if answer == "yes" :
		t.say("Ok, just checkin.")
	else :
		t.say("What are you waiting for?")
		
	return t.RenderJson()
	
@post("/incomplete")
def index(request):
	
	t = Tropo()
	
	t.say("Sorry, that wasn't on of the options.")
	
	return t.RenderJson()

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

Now that you're sending text messages, time to learn how to work with inbound text messages.

Next Step: Receiving Text Messages



Additional Reading

Find out more about the ask method in the Asking a Question QuickStart; you can also find out more about using choices in the Working with Simple Grammar and Concepts & Vocabulary sections.


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

login
  Passing in Parameters  |  TOC  |  Receiving Text Messages  

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