Skip to content

Commit

Permalink
Added several Express framework examples.
Browse files Browse the repository at this point in the history
  • Loading branch information
jsgoecke committed Sep 29, 2010
1 parent 631b4ea commit ee0ec6e
Show file tree
Hide file tree
Showing 4 changed files with 140 additions and 0 deletions.
22 changes: 22 additions & 0 deletions samples/express/hello-world.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/**
* Showing with the Express framwork http://expressjs.com/
* Express must be installed for this sample to work
*/

require('../../lib/tropo-webapi');
var express = require('express');

var app = express.createServer();

app.post('/', function(req, res){
// Create a new instance of the TropoWebAPI object.
var tropo = new TropoWebAPI();
// Use the say method https://www.tropo.com/docs/webapi/say.htm
tropo.say("Hello World!");

res.send(TropoJSON(tropo));
});


app.listen(8000);
console.log('Server running on http://0.0.0.0:8000/')
40 changes: 40 additions & 0 deletions samples/express/resource-routing.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/**
* Showing with the Express framwork http://expressjs.com/
* Express must be installed for this sample to work
*/

require('../../lib/tropo-webapi');
var express = require('express');
var app = express.createServer();

app.post('/', function(req, res){
// Create a new instance of the TropoWebAPI object.
var tropo = new TropoWebAPI();
// Use the say method https://www.tropo.com/docs/webapi/say.htm
tropo.say("Welcome to my Tropo Web API node demo.");
// Use the on method https://www.tropo.com/docs/webapi/on.htm
tropo.on("continue", null, "/one", true);

res.send(TropoJSON(tropo));
});

app.post('/one', function(req, res){
// Create a new instance of the TropoWebAPI object.
var tropo = new TropoWebAPI();
tropo.say("Hello from resource one!");
tropo.on("continue", null, "/two", true);

res.send(TropoJSON(tropo));
});

app.post('/two', function(req, res){
// Create a new instance of the TropoWebAPI object.
var tropo = new TropoWebAPI();
tropo.say("Hello from resource two!");
tropo.say("Well, enough of that. Goodbye.");

res.send(TropoJSON(tropo));
});

app.listen(8000);
console.log('Server running on http://0.0.0.0:8000/')
46 changes: 46 additions & 0 deletions samples/express/say-ask.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/**
* Showing with the Express framwork http://expressjs.com/
* Express must be installed for this sample to work
*/

require('../../lib/tropo-webapi');
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.bodyDecoder());
});

app.post('/', function(req, res){
// Create a new instance of the TropoWebAPI object.
var tropo = new TropoWebAPI();
// Use the say method https://www.tropo.com/docs/webapi/say.htm
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.
// use the ask method https://www.tropo.com/docs/webapi/ask.htm
tropo.ask(choices, 3, false, null, "foo", null, true, say, 5, null);
// use the on method https://www.tropo.com/docs/webapi/on.htm
tropo.on("continue", null, "/answer", true);

res.send(TropoJSON(tropo));
});

app.post('/answer', function(req, res){
// Create a new instance of the TropoWebAPI object.
var tropo = new TropoWebAPI();
tropo.say("Your zip code is " + req.body['result']['actions']['interpretation']);

res.send(TropoJSON(tropo));
});

app.listen(8000);
console.log('Server running on http://0.0.0.0:8000/')
32 changes: 32 additions & 0 deletions samples/express/use-session.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/**
* Showing with the Express framwork http://expressjs.com/
* Express must be installed for this sample to work
*/

require('../../lib/tropo-webapi');
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.bodyDecoder());
});

app.post('/', function(req, res){
console.log(req.body)

// Create a new instance of the TropoWebAPI object.
var tropo = new TropoWebAPI();
// Use the say method https://www.tropo.com/docs/webapi/say.htm
tropo.say("You are a " + req.body['session']['userType']);
tropo.say("Did you not know that already? Goodbye.")

res.send(TropoJSON(tropo));
});


app.listen(8000);
console.log('Server running on http://0.0.0.0:8000/')

0 comments on commit ee0ec6e

Please sign in to comment.