Skip to content

Instantly share code, notes, and snippets.

@mheadd
Created August 18, 2011 20:00
Show Gist options
  • Select an option

  • Save mheadd/1155022 to your computer and use it in GitHub Desktop.

Select an option

Save mheadd/1155022 to your computer and use it in GitHub Desktop.
Sample Tropo app written in Node.js with the Express Framework.
/**
* Sample Tropo app written in Node.js with the Express Framework.
*/
var tropo = require('tropo-webapi');
var express = require('express');
var port = process.ARGV[2] || 8000;
var app = express.createServer();
// Required to process the body of HTTP responses from Tropo.
app.configure(function(){
app.use(express.bodyDecoder());
});
// Base route, plays welcome message.
app.post('/', function(req, res){
var tropo = new TropoWebAPI();
tropo.say("Welcome to the Tropo Web API node demo.");
tropo.on("continue", null, "/start", true);
res.send(TropoJSON(tropo));
});
// Route to start asking caller for selection.
app.post('/start', function(req, res){
var tropo = new TropoWebAPI();
// Set up options for question to ask caller.
var choices = new Choices("Node JS, PHP, Ruby, Python, Scala");
var attempts = 3;
var bargein = false;
var minConfidence = null; // Use the platform default.
var name = "test";
var recognizer = "en-us";
var required = true;
var say = new Say("What is your favorite programming language?");
var timeout = 5;
var voice = "Allison";
tropo.ask(choices, attempts, bargein, minConfidence, name, recognizer, required, say, timeout, voice);
tropo.on("continue", null, "/answer", true);
tropo.on("error", null, "/error", true);
res.send(TropoJSON(tropo));
});
// Route to handle valid answers.
app.post('/answer', function(req, res){
var tropo = new TropoWebAPI();
var selection = req.body['result']['actions']['value'];
tropo.say('You chose, ' + selection + '. Thanks for playing.');
tropo.hangup();
res.send(TropoJSON(tropo));
});
// Route to handle errors or invalid responses.
app.post('/error', function(req, res){
var tropo = new TropoWebAPI();
tropo.say("Whoops, something bad happened. Please try again later.");
res.send(TropoJSON(tropo));
});
app.listen(port);
console.log('Tropo demo running on port: ' + port);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment