-
Notifications
You must be signed in to change notification settings - Fork 4
/
sms.js
156 lines (146 loc) · 3.72 KB
/
sms.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
module.exports = function (RED) {
"use strict"
var twilio = require('twilio')
function twilioRequest(from, to, text) {
return {
from: from,
to: to,
body: text
}
}
function twilioResponse(node, msg, err, message) {
node.sent++;
if (err) {
node.failed++;
console.log(err)
msg.payload = err;
node.error(msg)
} else {
if (message.from && message.from.indexOf('1500555000') >= 0) {
message.sid += '-TEST'
}
node.delivered++;
node.log(message.sid + ' | ' + message.to + ' | ' + message.body)
msg.payload = {
sid: message.sid,
to: message.to,
body: message.body
};
node.send(msg)
}
if (node.buffer.length === 0) {
node.status({
fill: "yellow",
shape: "dot",
text: node.sent + ', success: ' + node.delivered + ', error: ' + node.failed
})
}
}
function getMsgProps(msg, props) {
return props.split(".").reduce(function (obj, i) {
return obj[i]
}, msg)
}
function makeNumberMessagePairs(node, msg) {
var message = node.message;
var numbers = node.numbers;
if (!message) {
message = getMsgProps(msg, 'payload')
}
if (!numbers) {
numbers = getMsgProps(msg, 'topic').split(",")
} else {
numbers = numbers.split(",")
}
return numbers.map(function (elem) {
return {
to: elem,
text: message
}
})
}
function twilioSMS(node, to, text, msg) {
if (node.twilioClient && node.twilio.credentials.from) {
node.twilioClient.messages.create(twilioRequest(node.twilio.credentials.from, to, text), twilioResponse.bind(undefined, node, msg))
} else {
twilioResponse(node, msg, null, {
'sid': 'SIMULATED',
'to': to,
'body': text
})
}
}
function throtlleSMS(node, msg) {
Array.prototype.push.apply(node.buffer, makeNumberMessagePairs(node, msg))
// if timer already running there is nothing to do
if (node.intervalID !== -1) {
return;
}
node.intervalID = setInterval(function () {
if (node.buffer.length === 0) {
clearInterval(node.intervalID)
node.intervalID = -1
} else {
var elem = node.buffer.shift();
twilioSMS(node, elem.to, elem.text, msg);
if (node.buffer.length > 0) {
node.status({
text: node.buffer.length + ' pending',
fill: 'grey',
shape: 'dot'
})
}
}
}, node.throttle)
}
function SmsNode(config) {
RED.nodes.createNode(this, config)
var node = this
this.delivered = 0
this.failed = 0
this.sent = 0
this.twilio = RED.nodes.getNode(config.twilio)
if (this.twilio) {
node.status({
shape: "ring",
fill: "blue",
text: this.twilio.name
})
this.twilioClient = twilio(this.twilio.credentials.sid, this.twilio.credentials.token)
} else {
node.status({
shape: "ring",
fill: "red",
text: "SIMULATED"
})
}
this.message = config.message
this.numbers = config.numbers
this.throttle = config.throttle || 0
this.buffer = [];
this.intervalID = -1;
this.on("input", throtlleSMS.bind(undefined, this))
this.on("close", function () {
clearInterval(this.intervalID)
this.buffer = []
})
}
function TwilioConfigNode(config) {
RED.nodes.createNode(this, config)
this.name = config.name
}
RED.nodes.registerType("sms", SmsNode)
RED.nodes.registerType("twilioConfig", TwilioConfigNode, {
credentials: {
sid: {
type: "text"
},
token: {
type: "text"
},
from: {
type: "text"
}
}
})
}