PageRenderTime 46ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/node_modules/tropo-webapi/samples/use-result.js

https://bitbucket.org/mpuckett/iostudio-whiteboard
JavaScript | 60 lines | 32 code | 16 blank | 12 comment | 4 complexity | 5533f89a32d3797d0517ef47540af435 MD5 | raw file
Possible License(s): Apache-2.0, BSD-3-Clause, MIT
  1. /**
  2. * A very simple node web server that demonstrates how to use
  3. * the Tropo Result object.
  4. */
  5. var http = require('http');
  6. var tropowebapi = require('tropo-webapi');
  7. var server = http.createServer(function (request, response) {
  8. // Get the pathname for the current request.
  9. var pathname = require('url').parse(request.url).pathname;
  10. // Add a listener for the data event (incoming data from the HTTP request)
  11. request.addListener('data', function(data){
  12. json = data.toString();
  13. });
  14. // Add a listener for the EOF event on the incoming stream.
  15. request.addListener('end', function() {
  16. try {
  17. // Create a new instance of the TropoWebAPI object.
  18. var tropo = new tropowebapi.TropoWebAPI();
  19. // The path for the first step in the application flow (ask the caller for input).
  20. if(pathname == '/') {
  21. var say = new Say("Please enter a number one through 5.");
  22. var choices = new Choices("1,2,3,4,5");
  23. tropo.ask(choices, 3, false, null, "foo", null, true, say, 5, null);
  24. tropo.on("continue", null, '/selection', true);
  25. response.writeHead(200, {'Content-Type': 'application/json'});
  26. response.end(tropowebapi.TropoJSON(tropo));
  27. }
  28. // The second step in the application flow - input is submitted via Result JSON delivered from Tropo.
  29. if(pathname == '/selection') {
  30. // Create a new instance of the Result object and give it the JSON delivered from Tropo.
  31. var result = Result(json);
  32. tropo.say("Your selection was, " + result.interpretation + ". Goodbye.");
  33. tropo.hangup();
  34. response.writeHead(200, {'Content-Type': 'application/json'});
  35. response.end(tropowebapi.TropoJSON(tropo));
  36. }
  37. }
  38. catch(e) {
  39. response.writeHead(501, {'Content-Type': "text/plain"});
  40. //sys.log(e.message);
  41. response.end(e.message + "");
  42. }
  43. })
  44. }).listen(8000); // Listen on port 8000 for requests.