-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
99 lines (77 loc) · 2.11 KB
/
index.js
File metadata and controls
99 lines (77 loc) · 2.11 KB
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
var request = require('request');
var qs = require('qs');
var util = require('util');
var helpers = require('./string-helpers');
function OneWaySMS(config) {
if (!(this instanceof OneWaySMS)) {
return new OneWaySMS(config);
}
// Set the headers
var headers = {
'User-Agent' : 'OneWaySMS Gateway/1.0.0.'
}
this.headers = headers;
this.apiusername = config.apiusername;
this.apipassword = config.apipassword;
this.endpoint = config.endpoint;
};
OneWaySMS.prototype.send = function send(sms, callback) {
var numbers = util.isArray(sms.to) ? sms.to.join(',') : sms.to;
var data = {
apiusername: this.apiusername,
apipassword: this.apipassword,
senderid: encodeURIComponent(sms.from),
mobileno: numbers,
languagetype: sms.ascii ? 1 : 2,
message: sms.ascii ? encodeURIComponent(sms.message) : sms.message.toUnicodeSMS()
}
var query = toQueryParam(data);
var options = {
url: this.endpoint + '/api.aspx?' + query,
method: 'GET',
headers: this.headers
};
//console.log(options.url);
sendRequest(options, callback);
};
OneWaySMS.prototype.status = function status(transactionId, callback) {
var data = {
mtid: transactionId
}
var query = toQueryParam(data);
var options = {
url: this.endpoint + '/bulktrx.aspx?' + query,
method: 'GET'
}
sendRequest(options, callback);
};
OneWaySMS.prototype.balance = function balance(callback) {
var data = {
apiusername: this.apiusername,
apipassword: this.apipassword
}
var query = toQueryParam(data);
var options = {
url: this.endpoint + '/bulkcredit.aspx?' + query,
method: 'GET'
}
sendRequest(options, callback);
};
function toQueryParam(jsonData) {
var query = '';
for(key in jsonData) {
query += key + '=' + jsonData[key] + '&';
}
query = query.slice(0, query.length - 1);
return query;
}
function sendRequest(options, callback) {
function handleResponse(error, response, body) {
if (!error && response.statusCode == 200) {
return callback(null, body);
}
return callback(error);
}
request(options, handleResponse);
}
module.exports = OneWaySMS;