Skip to content

Commit 336ad78

Browse files
committed
Merge pull request #8 from thiseldo/master
twilio node first commit
2 parents 3f2433e + 20aa487 commit 336ad78

File tree

3 files changed

+122
-0
lines changed

3 files changed

+122
-0
lines changed

social/twilio/56-twilio.html

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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+
<script type="text/x-red" data-template-name="twilio">
19+
<div class="form-row">
20+
<label for="node-input-title"><i class="icon-flag"></i> Title</label>
21+
<input type="text" id="node-input-title" placeholder="Node-RED">
22+
</div>
23+
<div class="form-row">
24+
<label for="node-input-name"><i class="icon-tag"></i> Name</label>
25+
<input type="text" id="node-input-name" placeholder="Name">
26+
</div>
27+
</script>
28+
29+
<script type="text/x-red" data-help-name="twilio out">
30+
<p>Uses Twilio to send the <b>msg.payload</b> as a SMS to the configured number.</p>
31+
<p>Uses <b>msg.topic</b> to set the phone number, if not already set in the properties.</p>
32+
<p>You MUST configure both your Account SID and the Auth Token. Either into settings.js like this</p>
33+
<p><pre>twilio: { account:'My-ACCOUNT-SID', authtoken:'TWILIO-TOKEN', from:'FROM-NUMBER' },</pre></p>
34+
<p>Or as a twiliokey.js file in the directory <b>above</b> node-red.<p>
35+
<p><pre>module.exports = { account:'My-ACCOUNT-SID', authtoken:'TWILIO-TOKEN',from:'FROM-NUMBER' }</pre></p>
36+
</script>
37+
38+
<script type="text/javascript">
39+
RED.nodes.registerType('twilio out',{
40+
category: 'output',
41+
defaults: {
42+
title: {value:""},
43+
name: {value:""}
44+
},
45+
color:"#ed1c24",
46+
inputs:1,
47+
outputs:0,
48+
icon: "twilio.png",
49+
align: "right",
50+
label: function() {
51+
return this.name||this.title||"twilio out";
52+
},
53+
labelStyle: function() {
54+
return this.name?"node_label_italic":"";
55+
}
56+
});
57+
58+
</script>

social/twilio/56-twilio.js

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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);

social/twilio/icons/twilio.png

717 Bytes
Loading

0 commit comments

Comments
 (0)