Skip to content
Browse files

Tropo code moved to proper place

  • Loading branch information...
1 parent 4ae1670 commit 9bf30d0cf6efa2c3a58d565d6292da0e20c70b8d @1602 1602 committed Jun 8, 2012
Showing with 64 additions and 63 deletions.
  1. +1 −63 config/initializers/tropo.js
  2. +63 −0 lib/tropo.js
View
64 config/initializers/tropo.js
@@ -1,63 +1 @@
-var tropoSessions = {};
-var tropoSessionsIndex = {};
-var https = require('https');
-var API = require('tropo-webapi');
-var tropo = new TropoWebAPI();
-var token = '124cdb4120bef148a47b0e84a7eabe335c19828a326fcb47c6aa4c0565948b2692d5570d64f7a8f5dd047e90';
-
-app.startTropoSession = function (message, callback) {
- tropoSessions[message.id] = {
- message: message,
- callback: callback
- };
-
- var req = https.request({
- method: 'GET',
- host: 'api.tropo.com',
- port: 443,
- path: '/1.0/sessions?action=create&token=' + token + '&messageId=' + message.id
- }, function (res) {
- // res.on('data', console.log);
- });
- req.on('error', console.log);
- req.end();
-};
-
-var callbacks = ['incomplete', 'hangup', 'error', 'continue'];
-
-app.post('/tropo/text', function (req, res) {
-
- var messageId = req.body.session.parameters.messageId;
- var sessionId = req.body.session.id;
- var session = tropoSessions[messageId];
- var message = session.message;
- tropoSessionsIndex[sessionId] = messageId;
-
- // tropo.call(to, null, null, null, null, null, "SMS", null, null, null);
- tropo.call(message.phone);
- tropo.say(message.text);
- callbacks.forEach(function (callback) {
- tropo.on(callback, null, '/tropo/callback/' + callback, true);
- });
-
- res.send(TropoJSON(tropo));
-
-});
-
-callbacks.forEach(function (callback) {
- app.post('/tropo/callback/' + callback, callbackHandler(callback));
-});
-
-function callbackHandler(callback) {
- return function (req, res) {
- var sessionId = req.body.result.sessionId;
- var messageId = tropoSessionsIndex[sessionId];
- var session = tropoSessions[messageId];
- req.body.result.callback = callback;
- session.callback(req.body.result);
- delete tropoSessions[messageId];
- delete tropoSessionsIndex[sessionId];
- res.send(200);
- };
-}
-
+require(app.root + '/lib/tropo');
View
63 lib/tropo.js
@@ -0,0 +1,63 @@
+var tropoSessions = {};
+var tropoSessionsIndex = {};
+var https = require('https');
+var API = require('tropo-webapi');
+var tropo = new TropoWebAPI();
+var token = '124cdb4120bef148a47b0e84a7eabe335c19828a326fcb47c6aa4c0565948b2692d5570d64f7a8f5dd047e90';
+
+app.startTropoSession = function (message, callback) {
+ tropoSessions[message.id] = {
+ message: message,
+ callback: callback
+ };
+
+ var req = https.request({
+ method: 'GET',
+ host: 'api.tropo.com',
+ port: 443,
+ path: '/1.0/sessions?action=create&token=' + token + '&messageId=' + message.id
+ }, function (res) {
+ // res.on('data', console.log);
+ });
+ req.on('error', console.log);
+ req.end();
+};
+
+var callbacks = ['incomplete', 'hangup', 'error', 'continue'];
+
+app.post('/tropo/text', function (req, res) {
+
+ var messageId = req.body.session.parameters.messageId;
+ var sessionId = req.body.session.id;
+ var session = tropoSessions[messageId];
+ var message = session.message;
+ tropoSessionsIndex[sessionId] = messageId;
+
+ // tropo.call(to, null, null, null, null, null, "SMS", null, null, null);
+ tropo.call(message.phone);
+ tropo.say(message.text);
+ callbacks.forEach(function (callback) {
+ tropo.on(callback, null, '/tropo/callback/' + callback, true);
+ });
+
+ res.send(TropoJSON(tropo));
+
+});
+
+callbacks.forEach(function (callback) {
+ app.post('/tropo/callback/' + callback, callbackHandler(callback));
+});
+
+function callbackHandler(callback) {
+ return function (req, res) {
+ var sessionId = req.body.result.sessionId;
+ var messageId = tropoSessionsIndex[sessionId];
+ var session = tropoSessions[messageId];
+ req.body.result.callback = callback;
+ session.callback(req.body.result);
+ delete tropoSessions[messageId];
+ delete tropoSessionsIndex[sessionId];
+ res.send(200);
+ };
+}
+

0 comments on commit 9bf30d0

Please sign in to comment.
Something went wrong with that request. Please try again.