Skip to content

Commit 7939264

Browse files
committed
feat: Add send-sms node
1 parent ea0e611 commit 7939264

File tree

3 files changed

+106
-0
lines changed

3 files changed

+106
-0
lines changed

nodes/sms/send-sms.html

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<script type="text/javascript">
2+
RED.nodes.registerType('send-sms', {
3+
category: 'twilio sms',
4+
color: '#a6bbcf',
5+
defaults: {
6+
name: { value: '' },
7+
account: { value: '', type: 'account' },
8+
text: { value: '', required: true },
9+
from: { value: '', required: true, validate: RED.validators.typedInput('fromType') },
10+
fromType: { value: 'num' },
11+
to: { value: '', required: true, validate: RED.validators.typedInput('toType') },
12+
toType: { value: 'num' },
13+
},
14+
inputs: 1,
15+
outputs: 1,
16+
icon: 'file.png',
17+
label: function() {
18+
return this.name || 'send-sms';
19+
},
20+
oneditprepare: function() {
21+
$('#node-input-from').typedInput({ default: this.fromType || 'num', types: ['num', 'msg'] });
22+
$('#node-input-to').typedInput({ default: this.toType || 'num', types: ['num', 'msg'] });
23+
},
24+
oneditsave: function() {
25+
this.fromType = $('#node-input-from').typedInput('type');
26+
this.toType = $('#node-input-to').typedInput('type');
27+
},
28+
});
29+
</script>
30+
31+
<script type="text/x-red" data-template-name="send-sms">
32+
<div class="form-row">
33+
<label for="node-input-account"><i class="fa fa-user"></i> Account</label>
34+
<input type="text" id="node-input-account">
35+
</div>
36+
<div class="form-row">
37+
<label for="node-input-text"><i class="fa fa-align-left"></i> Text</label>
38+
<textarea rows="10" style="width: 70%;" id="node-input-text"/>
39+
</div>
40+
<div class="form-row">
41+
<label for="node-input-from"><i class="fa fa-tag"></i> From</label>
42+
<input type="text" id="node-input-from" style="width: 70%"/>
43+
</div>
44+
<div class="form-row">
45+
<label for="node-input-to"><i class="fa fa-tag"></i> To</label>
46+
<input type="text" id="node-input-to" style="width: 70%"/>
47+
</div>
48+
<div class="form-row">
49+
<label for="node-input-name"><i class="fa fa-tag"></i> Name</label>
50+
<input type="text" id="node-input-name" placeholder="Name">
51+
</div>
52+
</script>
53+
54+
<script type="text/x-red" data-help-name="send-sms">
55+
<p>Send an SMS.</p>
56+
</script>

nodes/sms/send-sms.js

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
module.exports = function(RED) {
2+
'use strict';
3+
var renderTemplate = require('../../utils/renderTemplate.js');
4+
5+
function SendSmsNode(config) {
6+
RED.nodes.createNode(this, config);
7+
this.account = RED.nodes.getNode(config.account);
8+
this.text = config.text;
9+
this.from = config.from;
10+
this.fromType = config.fromType;
11+
this.to = config.to;
12+
this.toType = config.toType;
13+
var node = this;
14+
15+
if (!node.account) {
16+
this.warn('missing account configuration');
17+
return;
18+
}
19+
20+
var client = require('twilio')(node.account.credentials.sid, node.account.credentials.token);
21+
client.api.baseUrl = node.account.credentials.apiUrl;
22+
23+
node.on('input', function(msg) {
24+
var fromNumber;
25+
var toNumber;
26+
if (node.fromType === 'msg') {
27+
fromNumber = RED.util.getMessageProperty(msg, node.from);
28+
} else {
29+
fromNumber = node.from;
30+
}
31+
if (node.toType === 'msg') {
32+
toNumber = RED.util.getMessageProperty(msg, node.to);
33+
} else {
34+
toNumber = node.to;
35+
}
36+
client.messages
37+
.create({
38+
body: renderTemplate(msg, node.text),
39+
from: fromNumber,
40+
to: toNumber,
41+
})
42+
.then(message => {
43+
msg.payload = message;
44+
node.send(msg);
45+
});
46+
});
47+
}
48+
RED.nodes.registerType('send-sms', SendSmsNode);
49+
};

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
"redirect": "nodes/sms/redirect.js",
2424
"reject": "nodes/voice/reject.js",
2525
"say": "nodes/voice/say.js",
26+
"send-sms": "nodes/sms/send-sms.js",
2627
"start-call": "nodes/voice/start-call.js",
2728
"webhook": "nodes/webhook.js"
2829
}

0 commit comments

Comments
 (0)