|
| 1 | +/** |
| 2 | + * Copyright 2013 Andrew D Lindsay @AndrewDLindsay |
| 3 | + * http://blog.thiseldo.co.uk |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + **/ |
| 17 | + |
| 18 | +var RED = require(process.env.NODE_RED_HOME+"/red/red"); |
| 19 | +var util = require('util'); |
| 20 | + |
| 21 | +// Either add a line like this to settings.js |
| 22 | +// twilio: { account:'My-ACCOUNT-SID', authtoken:'TWILIO-TOKEN',from:'FROM-NUMBER' }, |
| 23 | +// Or as a twiliokey.js file in the directory ABOVE node-red. |
| 24 | +// module.exports = { account:'My-ACCOUNT-SID', authtoken:'TWILIO-TOKEN',from:'FROM-NUMBER' } |
| 25 | + |
| 26 | +try { |
| 27 | + var twiliokey = RED.settings.twilio || require(process.env.NODE_RED_HOME+"/../twiliokey.js"); |
| 28 | +} |
| 29 | +catch(err) { |
| 30 | + util.log("[56-twilio.js] Error: Failed to load Twilio credentials"); |
| 31 | +} |
| 32 | + |
| 33 | +if (twiliokey) { |
| 34 | + var twilioClient = require('twilio')(twiliokey.account, twiliokey.authtoken); |
| 35 | + var fromNumber = twiliokey.from; |
| 36 | +} |
| 37 | + |
| 38 | +function TwilioOutNode(n) { |
| 39 | + RED.nodes.createNode(this,n); |
| 40 | + this.title = n.title; |
| 41 | + var node = this; |
| 42 | + this.on("input",function(msg) { |
| 43 | + if (typeof(msg.payload) == 'object') { |
| 44 | + msg.payload = JSON.stringify(msg.payload); |
| 45 | + } |
| 46 | + if (twiliokey) { |
| 47 | + try { |
| 48 | + // Send SMS |
| 49 | + twilioClient.sendMessage( {to: msg.topic, from: fromNumber, body: msg.payload}, function(err, response) { |
| 50 | + if (err) node.error(err); |
| 51 | + //console.log(response); |
| 52 | + }); |
| 53 | + } |
| 54 | + catch (err) { |
| 55 | + node.error(err); |
| 56 | + } |
| 57 | + } |
| 58 | + else { |
| 59 | + node.warn("Twilio credentials not set/found. See node info."); |
| 60 | + } |
| 61 | + }); |
| 62 | +} |
| 63 | + |
| 64 | +RED.nodes.registerType("twilio out",TwilioOutNode); |
0 commit comments