-
Notifications
You must be signed in to change notification settings - Fork 1
/
twilio-call.js
56 lines (36 loc) · 1.5 KB
/
twilio-call.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
var _internals = {};
_internals.call = function (payload, creds, cb) {
var client = require('twilio')(creds.api_sid, creds.api_token);
//Place a phone call, and respond with TwiML instructions from the given URL
client.makeCall({
to: payload.to, // Any number Twilio can call
from: payload.from, // A number you bought from Twilio and can use for outbound communication
url: payload.twilio_url // A URL that produces an XML document (TwiML) which contains instructions for the call
}, function(err, responseData) {
cb(err, responseData)
});
};
module.exports = function(RED) {
'use strict';
function Node(n) {
RED.nodes.createNode(this,n);
var node = this;
this.on('input', function (msg) {
var creds = RED.nodes.getNode(n.creds);
var payload = typeof msg.payload === 'object' ? msg.payload : {};
var attrs = ['to', 'from', 'twilio_url'];
for (var attr of attrs) {
if (n[attr]) {
payload[attr] = n[attr];
}
}
_internals.call(payload, creds, function(err, result){
msg.payload = result;
node.log(JSON.stringify(err));
node.log(JSON.stringify(result));
node.send(msg);
});
});
}
RED.nodes.registerType('twilio-call', Node);
};