forked from hirenrojasara/Msg91
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
142 lines (104 loc) · 3.35 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
/**
*
* @param authKey
* @param senderId
* @param route : Value can be 1 for Promotional Router or 4 for Transactional Route or 106 for SendOTP
* @param DLT_TE_ID : Issued DLT template from TRAI
*/
module.exports = function (authKey, senderId, route) {
if (authKey == null || authKey == "") {
throw new Error("MSG91 Authorization Key not provided.");
}
if (senderId == null || senderId == "") {
throw new Error("MSG91 Sender Id is not provided.");
}
if (route == null || route == "") {
throw new Error("MSG91 router Id is not provided.");
}
this.send = function (mobileNos, message, DLT_TE_ID, callback) {
callback = modifyCallbackIfNull(callback);
mobileNos = validateMobileNos(mobileNos);
message = validateMessage(message);
var isUnicode = isUnicodeString(message);
// Adding support for DLT template to accommodate changes by TRAI
var postData = "authkey=" + authKey + "&sender=" + senderId + "&mobiles=" + mobileNos + "&message=" + message + "&route=" + route + "&DLT_TE_ID=" + DLT_TE_ID;
if(isUnicode){
postData += "&unicode=1";
}
var options = {
hostname: 'control.msg91.com',
port: 80,
path: '/api/sendhttp.php',
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Content-Length': postData.length
}
};
makeHttpRequest(options, postData, function(err, data){
callback(err, data);
});
};
this.getBalance = function(customRoute, callback) {
if(arguments.length == 1) {
callback = customRoute;
customRoute = null;
}
callback = modifyCallbackIfNull(callback);
var currentRoute = customRoute || route;
var options = {
hostname: 'control.msg91.com',
port: 80,
path: '/api/balance.php?authkey=' + authKey + '&type=' + currentRoute,
method: 'GET'
};
makeHttpRequest(options, null, function(err, data){
callback(err, data);
});
}
return this;
};
function validateMobileNos(mobileNos){
if (mobileNos == null || mobileNos == "") {
throw new Error("MSG91 : Mobile No is not provided.");
}
if(mobileNos instanceof Array){
mobileNos = mobileNos.join(",");
}
return mobileNos
}
function validateMessage(message){
if (message == null || message == "") {
throw new Error("MSG91 : message is not provided.");
}
return message;
}
function modifyCallbackIfNull(callback){
return callback || function(){};
}
function isUnicodeString(str) {
for (var i = 0, n = str.length; i < n; i++) {
if (str.charCodeAt( i ) > 255) { return true; }
}
return false;
}
function makeHttpRequest(options, postData, callback) {
var http = require("http");
var data = "";
var req = http.request(options, function (res) {
res.setEncoding('utf8');
res.on('data', function (chunk) {
data += chunk;
});
res.on('end', function () {
callback(null, data);
})
});
req.on('error', function (e) {
callback(e);
});
if(postData!=null){
req.write(postData);
}
req.end();
}