-
Notifications
You must be signed in to change notification settings - Fork 1
/
twilio.js
150 lines (140 loc) · 4.85 KB
/
twilio.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
var Client = require('node-rest-client').Client;
var client = new Client();
var emitter = require('../core-integration-server-v2/javascripts/emitter');
var accountSid, authToken, baseUrl, fromPhone, textMessage;
var errMsg = '"Connection timeout error" in Twilio';
function run(node) {
try {
var msg;
var type = node.option.toLowerCase();
var url = "https://api.twilio.com/2010-04-01/Accounts/" + accountSid + "/";
var obj = node.reqData;
var postData = "";
var message, toPhone;
if(obj.hasOwnProperty("shippingAddress")) {
toPhone = obj.shippingAddress.phone;
} else {
toPhone = obj.defaultAddress.phone;
}
if(type == "phone") {
url += "Calls.json";
msg = 'Call has been sent successfully to ' + toPhone + ' from Twilio';
postData += 'Url=' + encodeURIComponent(baseUrl);
} else {
url += "Messages.json";
msg = 'Message has been sent successfully to ' + toPhone + ' from Twilio';
postData += "Body=" + encodeURIComponent(textMessage);
}
postData += "&From=" + encodeURIComponent(fromPhone) + "&To="+ encodeURIComponent(toPhone);
var args = {
data : postData,
headers : {
Authorization : "Basic " + b64EncodeUnicode(accountSid + ":" + authToken),
Accept : "application/json",
"Content-Type" : "application/x-www-form-urlencoded"
}
};
client.post(url, args, function (data, res) {
try {
var status = parseInt(res.statusCode/100) ;
if(status == 2) {
post(data, node, msg);
} else {
if(status == 5) {
emitter.emit('error', 'Server Error in Twilio','', url, node);
} else {
if(data.hasOwnProperty("detail")) {
errMsg = data.detail;
} else {
errMsg = data.message;
}
emitter.emit('error', errMsg, data, url, node);
}
}
} catch(e) {
emitter.emit('error', e.message, e.stack, url, node);
}
}).on('error',function(err) {
emitter.emit("error",errMsg, '', url, node);
});
} catch(e) {
emitter.emit('error', e.message, e.stack, "", node);
}
}
function b64EncodeUnicode(str) {
return new Buffer(str).toString('base64');
}
function post(response, node, message) {
node.resData = response;
emitter.emit("success",node,message);
}
function testApp(callback) {
try {
var url = "https://api.twilio.com/2010-04-01/Accounts/" + accountSid + "/Addresses.json";
var args = {
headers : {
Authorization : "Basic " + b64EncodeUnicode(accountSid + ":" + authToken),
Accept : "application/json"
}
};
var result;
client.get(url, args, function (data, res) {
try {
var statusCode = parseInt(res.statusCode/100);
if(statusCode == 2) {
result = {
status :'success',
response: data
};
} else {
var errMsg = 'Error in connecting Twilio';
if(data.hasOwnProperty("detail")) {
errMsg = data.detail;
} else {
errMsg = data.message;
}
result = {
status :'error',
response : errMsg
};
}
callback(result);
} catch(e) {
callback({status:"error", response:e.stack});
}
}).on('error',function(err) {
callback({status:"error", response:err});
});
} catch(e) {
callback({status:"error", response:e.stack});
}
}
function test(request, callback) {
try {
var credentials = request.credentials;
accountSid = credentials.accountSid;
authToken = credentials.authToken;
fromPhone = credentials.fromPhone;
testApp(callback);
} catch(e) {
callback({status:"error", response:e.stack});
}
}
function init(node) {
try {
var credentials = node.credentials;
accountSid = credentials.accountSid;
authToken = credentials.authToken;
fromPhone = credentials.fromPhone;
baseUrl = credentials.url;
textMessage = credentials.textMessage;
run(node);
} catch(e) {
emitter.emit('error', e.message, e.stack, "", node);
}
}
var Twilio = {
init : init,
test : test
};
module.exports = Twilio;