diff --git a/social/twilio/56-twilio.html b/social/twilio/56-twilio.html index ec9f84ef..c696aaa5 100644 --- a/social/twilio/56-twilio.html +++ b/social/twilio/56-twilio.html @@ -17,22 +17,22 @@ @@ -73,13 +73,13 @@ (function() { - + var hasGlobal = false; $.getJSON('twilio-api/global',function(data) { hasGlobal = data.hasToken; }); - - + + RED.nodes.registerType('twilio-api',{ category: 'config', defaults: { @@ -98,7 +98,7 @@ } else { $('#node-config-input-token').val(''); } - + }); }, oneditsave: function() { @@ -122,7 +122,7 @@ }); } }); - + RED.nodes.registerType('twilio out',{ category: 'output', defaults: { diff --git a/social/twilio/56-twilio.js b/social/twilio/56-twilio.js index 5db40726..0212fb09 100644 --- a/social/twilio/56-twilio.js +++ b/social/twilio/56-twilio.js @@ -15,100 +15,101 @@ * limitations under the License. **/ -var RED = require(process.env.NODE_RED_HOME+"/red/red"); -var util = require('util'); -var twilio = require('twilio'); +module.exports = function(RED) { + "use strict"; + var twilio = require('twilio'); -try { - var twiliokey = RED.settings.twilio || require(process.env.NODE_RED_HOME+"/../twiliokey.js"); -} -catch(err) { -} - -var querystring = require('querystring'); - -RED.httpAdmin.get('/twilio-api/global',function(req,res) { - res.send(JSON.stringify({hasToken:!(twiliokey && twiliokey.account && twiliokey.authtoken)})); -}); -RED.httpAdmin.get('/twilio-api/:id',function(req,res) { - var credentials = RED.nodes.getCredentials(req.params.id); - if (credentials) { - res.send(JSON.stringify({hasToken:(credentials.token&&credentials.token!="")})); - } else { - res.send(JSON.stringify({})); + try { + var twiliokey = RED.settings.twilio || require(process.env.NODE_RED_HOME+"/../twiliokey.js"); + } + catch(err) { } -}); -RED.httpAdmin.delete('/twilio-api/:id',function(req,res) { - RED.nodes.deleteCredentials(req.params.id); - res.send(200); -}); + var querystring = require('querystring'); -RED.httpAdmin.post('/twilio-api/:id',function(req,res) { - var body = ""; - req.on('data', function(chunk) { - body+=chunk; + RED.httpAdmin.get('/twilio-api/global',function(req,res) { + res.send(JSON.stringify({hasToken:!(twiliokey && twiliokey.account && twiliokey.authtoken)})); }); - req.on('end', function(){ - var newCreds = querystring.parse(body); - var credentials = RED.nodes.getCredentials(req.params.id)||{}; - if (newCreds.token == "") { - delete credentials.token; + RED.httpAdmin.get('/twilio-api/:id',function(req,res) { + var credentials = RED.nodes.getCredentials(req.params.id); + if (credentials) { + res.send(JSON.stringify({hasToken:(credentials.token&&credentials.token!=="")})); } else { - credentials.token = newCreds.token; + res.send(JSON.stringify({})); } - RED.nodes.addCredentials(req.params.id,credentials); + }); + + RED.httpAdmin.delete('/twilio-api/:id',function(req,res) { + RED.nodes.deleteCredentials(req.params.id); res.send(200); }); -}); -function TwilioAPINode(n) { - RED.nodes.createNode(this,n); - this.sid = n.sid; - this.from = n.from; - this.name = n.name; - var credentials = RED.nodes.getCredentials(n.id); - if (credentials) { - this.token = credentials.token; - } -} -RED.nodes.registerType("twilio-api",TwilioAPINode); - - -function TwilioOutNode(n) { - RED.nodes.createNode(this,n); - this.number = n.number; - - this.api = RED.nodes.getNode(n.twilio); - - if (this.api) { - this.twilioClient = twilio(this.api.sid,this.api.token); - this.fromNumber = this.api.from; - } else if (twiliokey) { - this.twilioClient = twilio(twiliokey.account, twiliokey.authtoken); - this.fromNumber = twiliokey.from; - } else { - this.error("missing twilio credentials"); - return; - } - - var node = this; - this.on("input",function(msg) { - if (typeof(msg.payload) == 'object') { - msg.payload = JSON.stringify(msg.payload); - } - try { - // Send SMS - var tonum = node.number || msg.topic; - node.twilioClient.sendMessage( {to: tonum, from: node.fromNumber, body: msg.payload}, function(err, response) { - if (err) { - node.error(err); - } - //console.log(response); - }); - } catch (err) { - node.error(err); - } + RED.httpAdmin.post('/twilio-api/:id',function(req,res) { + var body = ""; + req.on('data', function(chunk) { + body+=chunk; + }); + req.on('end', function(){ + var newCreds = querystring.parse(body); + var credentials = RED.nodes.getCredentials(req.params.id)||{}; + if (newCreds.token == "") { + delete credentials.token; + } else { + credentials.token = newCreds.token; + } + RED.nodes.addCredentials(req.params.id,credentials); + res.send(200); + }); }); + + function TwilioAPINode(n) { + RED.nodes.createNode(this,n); + this.sid = n.sid; + this.from = n.from; + this.name = n.name; + var credentials = RED.nodes.getCredentials(n.id); + if (credentials) { + this.token = credentials.token; + } + } + RED.nodes.registerType("twilio-api",TwilioAPINode); + + + function TwilioOutNode(n) { + RED.nodes.createNode(this,n); + this.number = n.number; + + this.api = RED.nodes.getNode(n.twilio); + + if (this.api) { + this.twilioClient = twilio(this.api.sid,this.api.token); + this.fromNumber = this.api.from; + } else if (twiliokey) { + this.twilioClient = twilio(twiliokey.account, twiliokey.authtoken); + this.fromNumber = twiliokey.from; + } else { + this.error("missing twilio credentials"); + return; + } + + var node = this; + this.on("input",function(msg) { + if (typeof(msg.payload) == 'object') { + msg.payload = JSON.stringify(msg.payload); + } + try { + // Send SMS + var tonum = node.number || msg.topic; + node.twilioClient.sendMessage( {to: tonum, from: node.fromNumber, body: msg.payload}, function(err, response) { + if (err) { + node.error(err); + } + //console.log(response); + }); + } catch (err) { + node.error(err); + } + }); + } + RED.nodes.registerType("twilio out",TwilioOutNode); } -RED.nodes.registerType("twilio out",TwilioOutNode); diff --git a/social/twilio/README.md b/social/twilio/README.md index e0e5b146..83c2db12 100644 --- a/social/twilio/README.md +++ b/social/twilio/README.md @@ -1,5 +1,5 @@ -node-red-node-pushbullet -======================== +node-red-node-twilio +==================== A Node-RED node to send SMS messages via the Twilio service. diff --git a/social/twilio/package.json b/social/twilio/package.json index c9ab8e37..ab4b7bd7 100644 --- a/social/twilio/package.json +++ b/social/twilio/package.json @@ -1,6 +1,6 @@ { "name" : "node-red-node-twilio", - "version" : "0.0.3", + "version" : "0.0.4", "description" : "A Node-RED node to send SMS messages via the Twilio service.", "dependencies" : { "twilio" : "1.6.0"