-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathintelliSMS.js
332 lines (293 loc) · 8.94 KB
/
intelliSMS.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
var http = require('http');
var https = require('https');
var querystring = require('querystring');
var fs = require('fs');
var FormData = require('form-data');
var path = require('path');
var _ = require('underscore');
var package = require('./package');
var IntelliSMS = function(username,password,options){
/* Setup Default Options */
this.options = _.extend({
username:username,
password:password,
host:['www.intellisoftware.co.uk','www.intellisoftware2.co.uk'],
timeout:5000,
useHost:0,
secure:false,
lineBreak:"\r\n",
sendsPerRequest:100
},options);
};
IntelliSMS.prototype.SendMessage = function(options,cb){
/* Set Scope */
var self = this;
/* Default Options */
var sendOptions = _.extend({
username : this.options.username,
password: this.options.password,
type:1
},options);
/* Validate Options */
if(sendOptions.to){
if(typeof(sendOptions.to)=='string') sendOptions.to = [sendOptions.to];
if(sendOptions.to instanceof Array === false) throw new Error('To numbers are an invalid type');
}else{
throw new Error('Enter a number to send to');
}
if([1,6,4].indexOf(sendOptions.type)>-1 && !sendOptions.text) throw new Error('Enter Enter Text to Send');
if(sendOptions.type==2 && !sendOptions.hex) throw new Error('Enter Enter Hex to Send');
if(sendOptions.type==3 && !sendOptions.ud) throw new Error('Enter Enter User Data to Send');
/* get maxconcat if not set and text is set */
if(!sendOptions.maxconcat && sendOptions.text) sendOptions.maxconcat = concatLength(sendOptions.text);
/* Get Send Batch */
var processNext = sendOptions.to.slice(this.options.sendsPerRequest)
sendOptions.to = sendOptions.to.slice(0,this.options.sendsPerRequest).join(',');
/* process Batch */
this.processRequest('/smsgateway/sendmsg.aspx',sendOptions,function(err,result){
if(!err && result.id) result = result.id;
cb(err,result);
});
/* process Next Batch if any */
if(processNext.length>0){
process.nextTick(function(){
sendOptions.to =processNext;
self.SendMessage(sendOptions,cb);
});
}
return this;
};
IntelliSMS.prototype.SendWapMessage = function(options,cb){
var sendOptions = _.extend({type : 4},options);
if(!sendOptions.href) throw new Error('Enter a URL to Send');
return this.SendMessage(sendOptions,cb);
};
IntelliSMS.prototype.SendUnicodeMessageHex = function(options,cb){
var sendOptions = _.extend({type : 2},options);
if(!sendOptions.hex) throw new Error('Enter a Hex Value');
return this.SendMessage(sendOptions,cb);
};
IntelliSMS.prototype.SendBinaryMessage = function(options,cb){
var sendOptions = _.extend({type : 3},options);
if(!sendOptions.ud) throw new Error('Enter a User Data');
return this.SendMessage(sendOptions,cb);
};
IntelliSMS.prototype.SendMessageWithUserContext = function(options,cb){
if(!options.usercontext) throw new Error('Enter user content');
return this.SendMessage(options,cb);
};
IntelliSMS.prototype.SendVoiceMessage = function(options,cb){
var sendOptions = _.extend({type : 6},options);
return this.SendMessage(options,cb);
};
IntelliSMS.prototype.SendMMSMessage = function(options,filepaths,cb){
if (filepaths instanceof Array ===false) filepaths = [filepaths];
/* Set Scope */
var self = this;
/* Default Options */
var sendOptions = _.extend({
username : this.options.username,
password: this.options.password,
type:5
},options);
/* Validate Options */
if(sendOptions.to){
if(typeof(sendOptions.to)=='string') sendOptions.to = [sendOptions.to];
if(sendOptions.to instanceof Array === false) throw new Error('To numbers are an invalid type');
}else{
throw new Error('Enter a number to send to');
}
if(!sendOptions.text) throw new Error('Enter some Data to Send');
/* Get Send Batch */
var processNext = sendOptions.to.slice(this.options.sendsPerRequest)
sendOptions.to = sendOptions.to.slice(0,this.options.sendsPerRequest).join(',');
/* Set Form Values */
var form = new FormData();
_.each(_.keys(sendOptions),function(key){
if(key!='maxconcat') form.append(key,sendOptions[key]);
});
/* Attach Files */
filepaths.forEach(function(filepath){
if(typeof(filepath)=='string'){
form.append(path.basename(filepath),fs.createReadStream(filepath));
}else if(filepath.on && filepath.path){
form.append(path.basename(filepath.path),filepath);
}else{
form.append('file',filepath);
}
});
/* Set URL */
var protocol = (this.options.secure)? 'https://':'http://';
var url = protocol+this.options.host[this.options.useHost]+'/smsgateway/default.aspx';
/* Callbacks */
form.on('error', function(e) {
if(self.options.useHost<(self.options.host.length-1)){
self.options.useHost++;
self.SendMMSMessage(sendOptions,filepaths,cb);
}else{
cb(e);
}
})
.on('socket', function (socket) {
socket.setTimeout(self.options.timeout);
socket.on('timeout', function() {
if(this.options.useHost<(this.options.host.length-1)){
self.options.useHost++;
self.SendMMSMessage(sendOptions,filepaths,cb);
}else{
cb(e);
}
});
})
.submit(url,function(err,res){
if(err){
cb(err);
}else{
var data = '';
res.setEncoding('UTF-8');
res.on('data',function(chunk){ data += chunk})
res.on('end',function(){
self.processResponse(data,function(err,result){
if(!err && result.id) result = result.id;
cb(err,result);
});
});
}
});
/* process Next Batch if any */
if(processNext.length>0){
process.nextTick(function(){
sendOptions.to =processNext;
self.SendMMSMessage(sendOptions,filepaths,cb);
});
}
return this;
};
IntelliSMS.prototype.GetBalance = function(cb){
this.processRequest('/smsgateway/getbalance.aspx',{
username : this.options.username,
password: this.options.password
},function(err,result){
if(!err && result.balance) result = result.balance;
cb(err,result);
});
return this;
};
IntelliSMS.prototype.UpdateSubscriptionList = function(sublistname,msisdn,action,cb){
var options = {
username : this.options.username,
password: this.options.password,
sublistname:sublistname,
msisdn:msisdn,
action:action
};
this.processRequest('/smsgateway/updatesubscriptionlist.aspx',options,function(err,result){
cb(err,result);
});
return this;
};
IntelliSMS.prototype.RetrieveMMSPart = function(msgid,msgpart,cb){
var options = {
username : this.options.username,
password: this.options.password,
msgid:sublistname,
msgpart:msgpart
};
this.processRequest('/smsgateway/retrievemms.aspx',options,function(err,result){
cb(err,result);
});
return this;
};
IntelliSMS.prototype.processRequest = function(endPoint,postData,cb){
var self = this;
if(typeof(postData)=='object') postData = querystring.stringify(postData);
var handler =(this.options.secure)? https:http;
var port =(this.options.secure)? 443:80;
var postOptions = {
host: this.options.host[this.options.useHost],
port: port,
path: endPoint,
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Content-Length': postData.length,
'user-agent': 'intelliSMS/'+package.version+' node.js'
}
};
var request = handler.request(postOptions, function(res) {
var data = '';
res.setEncoding('UTF-8');
res.on('data',function(chunk){ data += chunk})
res.on('end',function(){
self.processResponse(data,cb);
});
})
.on('error', function(e) {
if(self.options.useHost<(self.options.host.length-1)){
self.options.useHost++;
self.processRequest(endPoint,postData,cb);
}else{
cb(e);
}
})
.on('socket', function (socket) {
socket.setTimeout(self.options.timeout);
socket.on('timeout', function() {
if(self.options.useHost<(self.options.host.length-1)){
self.options.useHost++;
self.processRequest(endPoint,postData,cb);
}else{
cb(e);
}
});
});
request.write(postData);
request.end();
return this;
};
IntelliSMS.prototype.processResponse = function(responseBody,cb){
var lines = responseBody.split(this.options.lineBreak);
if(lines[lines.length-1].length==0) lines.pop();//Remove blank last line
if(lines.length>0){
for(var i=0;i<lines.length;i++){
var line = trim(lines[i]);
var number=null;
if(line.indexOf(',')>=0){
var lineParts = line.split(',');
number = lineParts.shift();
line = lineParts.shift();
}
if(line.indexOf(':')>=0){
var responseParts = line.split(':');
if(responseParts.length!=2){
cb(new Error('Invalid Data Response'));
}else{
if(responseParts[0]=='ERR'){
cb(new Error(responseParts[1]));
}else{
var callbackResponse = {};
callbackResponse[responseParts[0].toLowerCase()]=responseParts[1];
cb(null,callbackResponse,number);
}
}
}else{
cb(new Error('Invalid Data Response'));
}
}
}else{
cb(new Error('No Data Returned'));
}
return this;
};
/* Helper function to trim a string */
var trim = function(str) {
var str = str.replace(/^\s\s*/, ''),
ws = /\s/,
i = str.length;
while (ws.test(str.charAt(--i)));
return str.slice(0, i + 1);
};
var concatLength=function(text){
return Math.ceil(text.length/128);
};
module.exports = IntelliSMS;