-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initial commit of module files and samples.
- Loading branch information
0 parents
commit 057b058
Showing
7 changed files
with
407 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
A Node.js module for the Tropo WebAPI (https://www.tropo.com/docs/webapi/). | ||
|
||
Very early yet, still lots of work to do. | ||
|
||
Sample useage: | ||
|
||
<pre> | ||
require('../lib/tropo-webapi'); | ||
var sys = require('sys'); | ||
|
||
var tropo = new TropoWebAPI(); | ||
|
||
tropo.say("Hello, World."); | ||
|
||
sys.puts(TropoJSON(tropo)); | ||
</pre> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,192 @@ | ||
/** | ||
* Tropo action classes. | ||
* | ||
*/ | ||
Ask = function(choices, attempts, bargein, minConfidence, name, recognizer, required, say, timeout, voice) { | ||
this.choices = serializeProperty(choices); | ||
this.attempts = attempts; | ||
this.bargein = bargein; | ||
this.minConfidence = minConfidence; | ||
this.name = name; | ||
this.recognizer = recognizer; | ||
this.required = required; | ||
this.say = serializeProperty(say); | ||
this.timeout = timeout; | ||
this.voice = voice; | ||
}; | ||
|
||
Call = function(to, answerOnMedia, channel, from, headers, name, network, recording, required, timeout) { | ||
this.to = to; | ||
this.answerOnMedia = answerOnMedia; | ||
this.channel = channel; | ||
this.from = from; | ||
this.headers = serializeProperty(headers); | ||
this.name = name; | ||
this.network = network; | ||
this.recording = (typeof(recording) == 'Object') ? serializeProperty(recording) : recording; | ||
this.required = required; | ||
this.timeout = timeout; | ||
}; | ||
|
||
Choices = function(value, mode, terminator) { | ||
this.value = value; | ||
this.mode = mode; | ||
this.terminator = terminator; | ||
}; | ||
|
||
Conference = function(id, mute, name, playTones, required, terminator) { | ||
this.id = id; | ||
this.mute = mute; | ||
this.name = name; | ||
this.playTones = playTones; | ||
this.required = required; | ||
this.terminator = terminator; | ||
}; | ||
|
||
Hangup = function() {}; | ||
|
||
Message = function(say, to, answerOnMedia, channel, from, name, network, required, timeout, voice) { | ||
this.say = serializeProperty(say); | ||
this.to = to; | ||
this.answerOnMedia = answerOnMedia; | ||
this.channel = channel; | ||
this.from = from; | ||
this.name = name; | ||
this.network = network; | ||
this.required = required; | ||
this.timeout = timeout; | ||
this.voice = voice; | ||
}; | ||
|
||
On = function(event, name, next, required, say) { | ||
this.event = event; | ||
this.name = name; | ||
this.next = next; | ||
this.required = required; | ||
this.say = serializeProperty(say); | ||
}; | ||
|
||
Record = function(attempts, bargein, beep, choices, format, maxSilence, maxTime, method, minConfidence, name, required, say, timeout, transcription, url, password, username) { | ||
this.attempts = attempts; | ||
this.bargein = bargein; | ||
this.beep = beep; | ||
this.choices = serializeProperty(choices); | ||
this.format = format; | ||
this.maxSilence = maxSilence; | ||
this.maxTime = maxTime; | ||
this.method = method; | ||
this.minConfidence = minConfidence; | ||
this.name = name; | ||
this.required = required; | ||
this.say = serializeProperty(say); | ||
this.timeout = timeout; | ||
this.transcription = (typeof(transcription) == 'Object') ? serializeProperty(transcription) : recording; | ||
this.url = url; | ||
this.password = password; | ||
this.username = username; | ||
}; | ||
|
||
Redirect = function(to, name, required) { | ||
this.to = to; | ||
this.name = name; | ||
this.required = required; | ||
}; | ||
|
||
Reject = function() {}; | ||
|
||
Result = function(json) { | ||
var result = JSON.parse(json); | ||
this.sessionId = result.result.sessionId; | ||
this.state = result.result.state; | ||
this.sessionDuration = result.result.sessionDuration; | ||
this.sequence = result.result.sequence; | ||
this.complete = result.result.complete; | ||
this.error = result.result.error; | ||
this.actions = result.result.actions; | ||
this.name = result.result.actions.name; | ||
this.attempts = result.result.actions.attempts; | ||
this.disposition = result.result.actions.disposition; | ||
this.confidence = result.result.actions.confidence; | ||
this.interpretation = result.result.actions.interpretation; | ||
this.utterance = result.result.actions.utterance; | ||
this.value = result.result.actions.value; | ||
this.concept = result.result.concept.value; | ||
|
||
return this; | ||
}; | ||
|
||
Say = function(value, as, name, required, voice) { | ||
this.value = value; | ||
this.as = as; | ||
this.name = name; | ||
this.required = required; | ||
this.voice = voice; | ||
}; | ||
|
||
//TODO: Complete Session object. | ||
Session = function(json) { | ||
var session = JSON.parse(json); | ||
this.id = session.session.id; | ||
this.accountId = session.session.accountId; | ||
this.timestamp = session.session.timestamp; | ||
this.userType = session.session.userType; | ||
this.initialText = session.session.initialText; | ||
this.to; | ||
this.from; | ||
this.headers; | ||
this.parameters; | ||
|
||
return this; | ||
}; | ||
|
||
startRecording = function(format, method, url, username, password) { | ||
this.format = format; | ||
this.method = method; | ||
this.url = url; | ||
this.username = username ; | ||
this.password = password; | ||
}; | ||
|
||
stopRecording = function() {}; | ||
|
||
Transfer = function(to, answerOnMedia, choices, from, name, required, terminator, timeout) { | ||
this.to = to; | ||
this.answerOnMedia = answerOnMedia; | ||
this.choices = serializeProperty(choices); | ||
this.from = from; | ||
this.name = name; | ||
this.required = required; | ||
this.terminator = terminator; | ||
this.timeout = timeout; | ||
}; | ||
|
||
// Helper method that serializes properties of Tropo action objects.. | ||
function serializeProperty(object) { | ||
return JSON.stringify(object, replaceNull); | ||
} | ||
|
||
// Helper method to remove null values from rendered JSON. | ||
function replaceNull (key, value) { | ||
if (value === null || typeof(value) == 'undefined' || value == '') { | ||
return undefined; | ||
} | ||
return value; | ||
}; | ||
|
||
exports.Ask = Ask; | ||
exports.Call = Call; | ||
exports.Choices = Choices; | ||
exports.Conference = Conference; | ||
exports.Hangup = Hangup; | ||
exports.Message = Message; | ||
exports.On = On; | ||
exports.Record = Record; | ||
exports.Redirect = Redirect; | ||
exports.Reject = Reject; | ||
exports.Result = Result; | ||
exports.Say = Say; | ||
exports.Session = Session; | ||
exports.startRecording = startRecording; | ||
exports.stopRecording = stopRecording; | ||
exports.Transfer = Transfer; | ||
exports.replaceNull = replaceNull; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
var base = require('./base'); | ||
|
||
/** | ||
* Primary Tropo WebAPI class and prototype methods. | ||
* | ||
*/ | ||
TropoWebAPI = function() { | ||
this.tropo = new Array(); | ||
}; | ||
|
||
TropoWebAPI.prototype.ask = function(choices, attempts, bargein, minConfidence, name, recognizer, required, say, timeout, voice) { | ||
var ask = new base.Ask(choices, attempts, bargein, minConfidence, name, recognizer, required, say, timeout, voice); | ||
this.tropo.push(serializeObject("ask", ask)); | ||
}; | ||
|
||
TropoWebAPI.prototype.call = function(to, answerOnMedia, channel, from, headers, name, network, recording, required, timeout) { | ||
var call = new base.Call(to, answerOnMedia, channel, from, headers, name, network, recording, required, timeout); | ||
this.tropo.push(serializeObject("call", call)); | ||
}; | ||
|
||
TropoWebAPI.prototype.conference = function(id, mute, name, playTones, required, terminator) { | ||
var conference = new base.Conference(id, mute, name, playTones, required, terminator); | ||
this.tropo.push(serializeObject("conference", conference)); | ||
}; | ||
|
||
TropoWebAPI.prototype.hangup = function() { | ||
var hangup = new base.Hangup(); | ||
this.tropo.push(serializeObject("hangup", hangup)); | ||
}; | ||
|
||
TropoWebAPI.prototype.message = function(say, to, answerOnMedia, channel, from, name, network, required, timeout, voice) { | ||
var message = new base.Message(say, to, answerOnMedia, channel, from, name, network, required, timeout, voice); | ||
this.tropo.push(serializeObject("message", message)); | ||
}; | ||
|
||
TropoWebAPI.prototype.on = function(event, name, next, required, say) { | ||
var on = new base.On(event, name, next, required, say); | ||
this.tropo.push(serializeObject("on", on)); | ||
}; | ||
|
||
TropoWebAPI.prototype.record = function(attempts, bargein, beep, choices, format, maxSilence, maxTime, method, minConfidence, name, required, say, timeout, transcription, url, password, username) { | ||
var record = new base.Record(attempts, bargein, beep, choices, format, maxSilence, maxTime, method, minConfidence, name, required, say, timeout, transcription, url, password, username); | ||
this.tropo.push(serializeObject("record", record)); | ||
}; | ||
|
||
TropoWebAPI.prototype.redirect = function(to, name, required) { | ||
var redirect = new base.Redirect(to, name, required); | ||
this.tropo.push(serializeObject("redirect", redirect)); | ||
}; | ||
|
||
TropoWebAPI.prototype.reject = function() { | ||
var reject = new base.Reject(); | ||
this.tropo.push(serializeObject("reject", reject)); | ||
}; | ||
|
||
TropoWebAPI.prototype.say = function(value, as, name, required, voice) { | ||
var say = new base.Say(value, as, name, required, voice); | ||
this.tropo.push(serializeObject("say", say)); | ||
}; | ||
|
||
TropoWebAPI.prototype.startRecording = function(format, method, url, username, password) { | ||
var startRecording = new base.StartRecording(format, method, url, username, password); | ||
this.tropo.push(serializeObject("startRecording", startRecording)); | ||
}; | ||
|
||
TropoWebAPI.prototype.stopRecording = function() { | ||
var stopRecording = new base.StopRecording(); | ||
this.tropo.push(serializeObject("stopRecording", stopRecording)); | ||
}; | ||
|
||
TropoWebAPI.prototype.transfer = function(to, answerOnMedia, choices, from, name, required, terminator, timeout) { | ||
var transfer = new base.Transfer(to, answerOnMedia, choices, from, name, required, terminator, timeout); | ||
this.tropo.push(serializeObject("transfer", transfer)); | ||
}; | ||
|
||
exports.TropoWebAPI = TropoWebAPI; | ||
|
||
/** | ||
* Helper class to render JSON for Tropo WebAPI. | ||
* | ||
*/ | ||
TropoJSON = function(object) { | ||
return JSON.stringify(object).replace(/\\/g, "").replace(/"{/g, "{").replace(/}"/g, "}"); | ||
}; | ||
exports.TropoJSON = TropoJSON; | ||
|
||
//TODO: Complete header method. | ||
Headers = function() { | ||
|
||
}; | ||
exports.Headers = Headers; | ||
|
||
// Helper method that serializes objects that are properties of the primary Tropo object. | ||
function serializeObject(name, object) { | ||
return "{ \"" + name + "\":" + JSON.stringify(object, base.replaceNull) + "}"; | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
/** | ||
* A simple example demonstrating how to produce JSON for the | ||
* Tropo WebAPI. Execute by doing: | ||
* ~$ node path/to/tropo-webapi-node/samples/sample-1.js | ||
*/ | ||
|
||
require('../lib/tropo-webapi'); | ||
var sys = require('sys'); | ||
|
||
// Create a new instance of the TropoWebAPI object. | ||
var tropo = new TropoWebAPI(); | ||
|
||
// Say something and then hangup. (Note, null values are excluded from rendered JSON.) | ||
tropo.say("Hello, World.", null, null, true, "carmen"); | ||
tropo.hangup(); | ||
|
||
// Write out the rendered JSON. | ||
sys.puts(TropoJSON(tropo)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
/** | ||
* A very simple node web server that will respond to requests | ||
* with the Tropo WebAPI JSON version of "Hello, World!" | ||
*/ | ||
|
||
var http = require('http'); | ||
require('../lib/tropo-webapi'); | ||
|
||
var server = http.createServer(function (request, response) { | ||
|
||
// Create a new instance of the TropoWebAPI object. | ||
var tropo = new TropoWebAPI(); | ||
tropo.say("Hello, World!"); | ||
|
||
// Render out the JSON for Tropo to consume. | ||
response.writeHead(200, {'Content-Type': 'application/json'}); | ||
response.end(TropoJSON(tropo)); | ||
|
||
}).listen(8000); // Listen on port 8000 for requests. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/** | ||
* A very simple node web server that will respond to requests | ||
* with a request to enter a 5 digit zip code. | ||
*/ | ||
|
||
var http = require('http'); | ||
require('../lib/tropo-webapi'); | ||
|
||
var server = http.createServer(function (request, response) { | ||
|
||
// Create a new instance of the TropoWebAPI object. | ||
var tropo = new TropoWebAPI(); | ||
tropo.say("Welcome to my Tropo Web API node demo."); | ||
|
||
// Demonstrates how to use the base Tropo action classes. | ||
var say = new Say("Please enter your 5 digit zip code."); | ||
var choices = new Choices("[5 DIGITS]"); | ||
|
||
// Action classes can be passes as parameters to TropoWebAPI class methods. | ||
tropo.ask(choices, 3, false, null, "foo", null, true, say, 5, null); | ||
tropo.on(new On("continue", null, "http://somefakehost.com:8000/", true)); | ||
|
||
// Render out the JSON for Tropo to consume. | ||
response.writeHead(200, {'Content-Type': 'application/json'}); | ||
response.end(TropoJSON(tropo)); | ||
|
||
}).listen(8000); // Listen on port 8000 for requests. |
Oops, something went wrong.