-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
202 lines (187 loc) · 7.03 KB
/
index.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
var sendRequest = require('request');
var iconv = require('iconv-lite');
var uuid = require('node-uuid');
var pkginfo = require('pkginfo')(module, 'version');
var _api = {
login: '',
password: '',
url : 'https://sms.voxbone.com:4443/sms/v1/'
};
var _version = module.exports.version;
var Voxbone = function(opts) {
_api.login = opts.apiLogin;
_api.password = opts.apiPassword;
if(typeof opts.url != 'undefined'){
_api.url = opts.url;
}
};
Voxbone.prototype = {
//Delivery Report constructor that passes parameters to the http sendSMSRequest request
sendSMS: function(to, from, msg, fragref, dr, successCB, failCB){
var encoding = detectEncoding(msg, fragref);
var fragLength = getFragLength(msg, encoding, fragref);
var fragments = [];
console.log('Encoding: '+encoding+' Frag Length: '+fragLength+' msg length: '+msg.length+' Frag Ref: '+fragref);
if (msg.length > fragLength){
while (msg.length > fragLength){
fragments.push(msg.substring(0,fragLength));
msg = msg.substring(fragLength);
if (msg.length < fragLength){
fragments.push(msg);
}
}
}else{
fragments.push(msg);
}
console.log(fragments);
if(fragments.length > 1){
for (var i = 0; i < fragments.length; ++i) {
var frag = {frag_ref: fragref, frag_total:fragments.length, frag_num: i+1};
var data ={from:from, msg:fragments[i], frag:frag, delivery_report:dr};
var callback = (function(error, response, body) {
if (error || response.statusCode >= 300) {
typeof failCB === "function" && failCB(error?error:response.statusCode,frag);
} else {
typeof successCB === "function" && successCB(body.transaction_id,frag);
}
});
request('POST',_api.url+to, data, callback);
}
}else{
var frag = null;
var data = {from:from, msg:msg, frag:frag, delivery_report:dr};
var callback = (function(error, response, body) {
if (error || response.statusCode >= 300) {
typeof failCB === "function" && failCB(error?error:response.statusCode,frag);
} else {
typeof successCB === "function" && successCB(body.transaction_id,frag);
}
});
request('POST',_api.url+to, data, callback);
}
},
//Generate a Frag Ref
//Frag ref should be a value within [0-65535]
createFragRef: function (){
return Math.floor(Math.random() * 65535 );
},
//Generate a transaction ID
createTransId: function (){
var transid = uuid.v4();
return transid;
},
//Delivery Report constructor that passes parameters to the http sendDeliveryRequest request
sendDeliveryReport: function(transid, orig_destination, orig_from, delivery_status, status_code, isSuccessCB){
var data = {orig_from:orig_from, delivery_status:delivery_status, status_code:status_code};
var callback = (function(error, response, body) {
if (isSuccessCB === "function") {
isSuccessCB(!error);
}
});
request('PUT',_api.url+orig_destination+'/report/'+transid, data, callback);
}
};
function request(method,url, data, callback){
var credentials = {
login: _api.login,
password: _api.password
};
var headers = {
'Content-Type': 'application/json',
'Accept': 'application/json',
'User-Agent': 'VoxSMS - NPM '+_version
};
var auth = {
'user': credentials.login,
'pass': credentials.password,
'sendImmediately': false
};
var options = {
auth: auth,
url: url,
body: data,
json: true,
headers: headers,
method: method.toUpperCase()
};
sendRequest(options,function (error, response, body) {
if (data.frag === null) {
response.body.final = true;
response.body.frag_total = 1;
response.body.frag_num = 1;
} else if (data.frag.frag_total === data.frag.frag_num) {
response.body.final = true;
response.body.frag_total = data.frag.frag_total;
response.body.frag_num = data.frag.frag_num;
} else {
response.body.final = false;
response.body.frag_total = data.frag.frag_total;
response.body.frag_num = data.frag.frag_num;
}
if (!error && (response.statusCode == 200 || response.statusCode == 202)) {
console.log('[DEBUG]',method,url,'succeeded with HTTP status', response.statusCode);
console.log('[DEBUG] response.body : ' + JSON.stringify(response.body));
}else if (!error){
console.log('[DEBUG]',method,url,'failed with HTTP status',response.statusCode);
console.log('[DEBUG] response.body : ' + JSON.stringify(response.body));
}else{
console.log('[DEBUG] An error occured while sending the request.');
console.log(error);
console.log(body);
console.log(response);
}
typeof callback === "function" && callback(error, response, body);
});
}
function detectEncoding(msg, fragref){
var encoding;
//Prepare encoding message
var buf_latin = iconv.encode(msg, 'ISO-8859-15');
var latin = iconv.decode(buf_latin, 'ISO-8859-15');
var buf_ucs2 = iconv.encode(msg, 'ucs-2');
var ucs2 = iconv.decode(buf_ucs2, 'ucs-2');
if(isGSMAlphabet(msg) == true){
encoding = 'gsma';
}else if(latin == msg){
encoding = 'latin1';
}else if(ucs2 == msg){
encoding = 'ucs-2';
} else{
console.log('No idea what encoding this is.');
}
function isGSMAlphabet(text) {
var regexp = new RegExp("^[A-Za-z0-9 \\r\\n@£$¥èéùìòÇØøÅå\u0394_\u03A6\u0393\u039B\u03A9\u03A0\u03A8\u03A3\u0398\u039EÆæßÉ!\"#$%&'()*+,\\-./:;<=>?¡ÄÖÑܧ¿äöñüà^{}\\\\\\[~\\]|\u20AC]*$");
return regexp.test(text);
}
return encoding;
}
function getFragLength(msg, encoding, fragref){
var maxchar;
if (encoding == 'gsma'){
if (msg.length <= 160){
maxchar = 160;
}else if (msg.length > 160 && fragref <= 255){
maxchar = 153;
}else if (msg.length > 160 && fragref > 255){
maxchar = 152;
}
}else if (encoding == 'latin1'){
if (msg.length <= 140){
maxchar = 140;
}else if (msg.length > 140 && fragref <= 255){
maxchar = 134;
}else if (msg.length > 140 && fragref > 255){
maxchar = 133;
}
}else if (encoding == 'ucs-2'){
if (msg.length <=70){
maxchar = 70;
}else if (msg.length > 70 && fragref <= 255){
maxchar = 65;
}else if (msg.length > 70 && fragref > 255){
maxchar = 64;
}
}
return maxchar;
}
module.exports = Voxbone;