| Tropo WebAPI Development Guide | Home | Frameset Home |
|
/samples sub folder of the project directory (the directory that also contains the lib and tests folders from the WebAPI library download; by default, this is named tropo-webapi-node, but it can be renamed however you want). You can download a complete copy of the script from our Github; we'll break it up into pieces in this tutorial to make it easier to follow.bodyParser so we can work with the session object (line 16):
/**
* Showing with the Express framework http://expressjs.com/
* Express must be installed for this sample to work
*/
require('../lib/tropo-webapi.js');
var express = require('express');
var app = express.createServer();
/**
* 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());
});
'/', this resource will be used when the base URL is passed to the server, making it essentially our start point. We then create a new instance of the TropoWebAPI object (line 3) and start into the logic:
app.post('/', function(req, res){
// Create a new instance of the TropoWebAPI object.
var tropo = new TropoWebAPI();
if(req.body['session']['from']['channel'] == "TEXT") {
tropo.say("This application is voice only. Please call in using a regular phone, SIP phone or via Skype.");
tropo.on("continue", null, null, true);
res.send(TropoJSON(tropo));
}
// Use the say method https://www.tropo.com/docs/webapi/say.htm
else {
tropo.say("Welcome to my Tropo Web API node demo.");
// Demonstrates how to use the base Tropo action classes.
var say = new Say("Please ree cord your message after the beep.");
var choices = new Choices(null, null, "#");
// Action classes can be passed as parameters to TropoWebAPI class methods.
// use the record method https://www.tropo.com/docs/webapi/record.htm
tropo.record(3, false, null, choices, audio/wav, 5, 60, null, null, "recording", null, say, 5, null, "http://example.com/tropo", null, null);
// use the on method https://www.tropo.com/docs/webapi/on.htm
tropo.on("continue", null, "/answer", true);
tropo.on("incomplete", null, "/timeout", true);
tropo.on("error", null, "/error", true);
res.send(TropoJSON(tropo));
}});
record object (line 12). In record, you'll see several parameters defined - these correspond to those defined for the record object in the tropo-webapi.js file we required at the beginning of the script. Here's the relevant portion, to make it easier to understand what values are associated with which parameters:TropoWebAPI.prototype.record = function(attempts, bargein, beep, choices, format, maxSilence, maxTime, method, minConfidence, name, required, say, timeout, transcription, url, password, username)record object will read out the prompt defined as the say variable (line 7 of Figure 9.2), beep to let the user know when to start talking, accept the # key as the terminator (defined in the choices variable on line 8 of Figure 9.2), then send the file to http://example.com/tropo as recordings.wav (the default format is wav, if you wanted to overwrite it, you could define the format as 'audio/mp3')./answer resource (defined by line 15 of Figure 9.2, shown in Figure 9.3):
app.post('/answer', function(req, res){
// Create a new instance of the TropoWebAPI object.
var tropo = new TropoWebAPI();
tropo.say("Recording successfully saved. Thank you!");
res.send(TropoJSON(tropo));
});
attempts parameter in record. On line 12 of Figure 9.2, the first value after record is 3, which corresponds to attempts), then the application will fire the incomplete event and move on to the /timeout resource (defined by line 17 of Figure 9.2, shown in Figure 9.4):
app.post('/timeout', function(req, res){
var tropo = new TropoWebAPI();
tropo.say("Sorry, I didn't hear anything. Please call back and try again.");
res.send(TropoJSON(tropo));
});
/error resource:
app.post('/error', function(req, res){
// Create a new instance of the TropoWebAPI object.
var tropo = new TropoWebAPI();
tropo.say("Recording failed. Please call back and try again.");
res.send(TropoJSON(tropo));
});
samples sub-directory of the tropo-webapi-node folder; with the file in the correct location, you would launch the application on your server by first opening your command prompt, then switch into the project directory, and then type:node samples/nodeScript.jsnodeScript.js, you can name it whatever you prefer.Server running on port:8000
app.listen(8000);
console.log('Server running on port :8000');
console.log, then the application is successfully running and can now be accessed by Tropo. Give it a call (or a text) and test it out!| ANNOTATIONS: EXISTING POSTS |
| login |
|