| Tropo WebAPI Development Guide | Home | Frameset Home |
|
require portion of the application won't work (effectively killing the entire app).channel they come in on - either VOICE or TEXT. It assumes if the user is texting the app, they were provided instructions telling them what to do - possibly on a website or a flyer. In this case, the options are four different movie trilogies; the user is asked to select their favorite using an assigned keypad number. We need to define what to do when the user texts the app vs what to do when the user calls the app, which is done right at the start of the script.
<?php
require 'tropo.class.php';
require 'lib/limonade.php';
// This is a helper method, used when the caller initially sends in a valid input over the text channel.
function valid_text(&$tropo, $initial_text) {
// Create a new instance of the Session object, and get the channel information.
$session = new Session();
$from_info = $session->getFrom();
$network = $from_info['channel'];
// Welcome prompt.
$tropo->ask("Welcome to the Tropo PHP example for $network");
// Provide a prompt based on the the initial_text value
if ($initial_text == "1")
{$tropo->say("You picked Lord of the Rings. Did you know Gandalf is also Magneto? Weird.");
}
if ($initial_text == "2")
{$tropo->say("You picked the original Star Wars. I hear Leonard Nimoy was awesome in those.");
}
if ($initial_text == "3")
{$tropo->say("You picked the Star Wars prequels. Stop calling this number, Mr. Lucas, we know it's you.");
}
if ($initial_text == "4")
{$tropo->say("You picked the Matrix. Dude, whoa.");
}
// Tell Tropo what to do next. This redirects to the instructions under dispatch_post('/hangup', 'app_hangup').
$tropo->on(array("event" => "continue", "next" => "favorite-movie-webapi.php?uri=hangup"));
// Tell Tropo what to do if there's an error. This redirects to the instructions under dispatch_post('/incomplete', 'app_incomplete').
$tropo->on(array("event" => "incomplete", "next" => "favorite-movie-webapi.php?uri=incomplete"));
}
hangup resource (line 32) unless an error occurs server-side, in which case it would move on to the error resource (line 35).
dispatch_post('/start', 'app_start');
function app_start() {
// Create a new instance of the Session object, and get the channel information.
$session = new Session();
$from_info = $session->getFrom();
$network = $from_info['channel'];
// Create a new instance of the Tropo object.
$tropo = new Tropo();
// See if any text was sent with session start.
$initial_text = $session->getInitialText();
// If the initial text is a choice, skip the input collection and go right to results
if(strlen($initial_text) == 1 && is_numeric($initial_text)) {
valid_text($tropo, $initial_text);
}
else {
// Welcome prompt.
$tropo->say("Welcome to the Tropo PHP example for $network");
// Set up options for input.
$options = array("attempts" => 3, "bargein" => true, "choices" => "1,2,3,4", "mode" => "dtmf", "name" => "movie", "timeout" => 30);
// Ask the caller for input, pass in options.
$tropo->ask("Which of these trilogies do you like the best? Press 1 to vote for Lord of the Rings, press 2 for the original Star Wars, 3 for the Star Wars prequels, or press 4 for the Matrix", $options);
// Tell Tropo what to do when the user has entered input, or if there's an error. This redirects to the instructions under dispatch_post('/choice', 'app_choice') or dispatch_post('/incomplete', 'app_incomplete').
$tropo->on(array("event" => "continue", "next" => "favorite-movie-webapi.php?uri=choice", "say" => "Please hold."));
$tropo->on(array("event" => "incomplete", "next" => "favorite-movie-webapi.php?uri=incomplete"));
}
// Render the JSON for the Tropo WebAPI to consume.
return $tropo->RenderJson();
}
initial_text, the app came across the TEXT channel. It then checks to see if the value was a valid one - a single, numeric digit (lines 16). If that matches, it then redirects to the valid_text function (line 17) - the one we defined in the very beginning (Figure 7.0).attempts earlier in the code (line 26 of Figure 7.1). If the user makes a valid selection, it moves on to the choice resource (line 32 of Figure 7.1), which is shown in Figure 7.2:
dispatch_post('/choice', 'app_choice');
function app_choice() {
// Accessing the result object
$result = new Result();
$choice = $result->getValue();
// Create a new instance of the Tropo object.
$tropo = new Tropo();
// Provide a prompt based on the value
if ($choice == "1")
{$tropo->say("You picked Lord of the Rings. Did you know Gandalf is also Mag knee toe? Weird.");
}
if ($choice == "2")
{$tropo->say("You picked the original Star Wars. I hear Leonard Nimoy was awe some in those.");
}
if ($choice == "3")
{$tropo->say("You picked the Star Wars prequels. Stop calling this number, Mr. Lucas, we know it's you.");
}
if ($choice == "4")
{$tropo->say("You picked the Matrix. Dude, woe.");
}
// Tell Tropo what to do next. This redirects to the instructions under dispatch_post('/hangup', 'app_hangup').
$tropo->on(array("event" => "continue", "next" => "favorite-movie-webapi.php?uri=hangup"));
// Tell Tropo what to do if there's an error. This redirects to the instructions under dispatch_post('/incomplete', 'app_incomplete').
$tropo->on(array("event" => "incomplete", "next" => "favorite-movie-webapi.php?uri=incomplete"));
// Render the JSON for the Tropo WebAPI to consume.
return $tropo->RenderJson();
}
choice resource will look much like the valid_text "helper" method. It retrieves the user's choice from the result object sent back by Tropo (lines 5 and 6 of Figure 7.2) and reads off the appropriate prompt based on the value (lines 12 through 23). Once it's finished with the say, it moves on to hangup (line 26), shown in Figure 7.3:
dispatch_post('/hangup', 'app_hangup');
function app_hangup() {
$tropo = new Tropo();
$tropo->say("Thanks for voting!");
$tropo->hangup();
return $tropo->RenderJson();
}
error:
dispatch_post('/error', 'app_error');
function app_error() {
$tropo = new Tropo();
$tropo->say("Something has gone wrong, please call back.");
$tropo->hangup();
return $tropo->RenderJson();
}
run();
| ANNOTATIONS: EXISTING POSTS |
| login |
|