-
Notifications
You must be signed in to change notification settings - Fork 24
/
index.js
179 lines (155 loc) · 5.12 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
"use strict";
let request = require('request');
class SendOtp {
/**
* Creates a new SendOtp instance
* @param {string} authKey Authentication key
* @param {string, optional} messageTemplate
*/
constructor(authKey, messageTemplate) {
this.authKey = authKey;
if(messageTemplate){
this.messageTemplate = messageTemplate;
}else{
this.messageTemplate = "Your otp is {{otp}}. Please do not share it with anybody";
}
this.otp_expiry = 1440; //1 Day =1440 minutes
}
/**
* Returns the base URL for MSG91 api call
* @returns {string} Base URL for MSG91 api call
*/
static getBaseURL() {
return "https://control.msg91.com/api/";
}
/**
* Set the OTP expiry minutes for MSG91 api call
*/
setOtpExpiry(otp_expiry) {
this.otp_expiry=otp_expiry;
return;
}
/**
* Returns the 4 digit otp
* @returns {integer} 4 digit otp
*/
static generateOtp() {
return Math.floor(1000 + Math.random() * 9000);
}
/**
* Send Otp to given mobile number
* @param {string} contactNumber receiver's mobile number along with country code
* @param {string} senderId
* @param {string, optional} otp
* Return promise if no callback is passed and promises available
*/
send(contactNumber, senderId, otp, callback) {
if (typeof otp === 'function') {
callback = otp;
otp = SendOtp.generateOtp()
}
let args = {
authkey: this.authKey,
mobile: contactNumber,
sender: senderId,
message: this.messageTemplate.replace('{{otp}}', otp),
otp: otp,
otp_expiry: this.otp_expiry
};
return SendOtp.doRequest('get', "sendotp.php", args, callback);
}
/**
* Retry Otp to given mobile number
* @param {string} contactNumber receiver's mobile number along with country code
* @param {boolean} retryVoice, false to retry otp via text call, default true
* Return promise if no callback is passed and promises available
*/
retry(contactNumber, retryVoice, callback) {
let retryType = 'voice';
if (!retryVoice) {
retryType = 'text'
}
let args = {
authkey: this.authKey,
mobile: contactNumber,
retrytype: retryType
};
return SendOtp.doRequest('get', "retryotp.php", args, callback);
}
/**
* Verify Otp to given mobile number
* @param {string} contactNumber receiver's mobile number along with country code
* @param {string} otp otp to verify
* Return promise if no callback is passed and promises available
*/
verify(contactNumber, otp, callback) {
let args = {
authkey: this.authKey,
mobile: contactNumber,
otp: otp
};
return SendOtp.doRequest('get', "verifyRequestOTP.php", args, callback);
}
static doRequest (method, path, params, callback) {
if (typeof params === 'function') {
callback = params;
params = {};
}
// Return promise if no callback is passed and promises available
else if (callback === undefined && this.allow_promise) {
promise = true;
}
let options = {
method: method,
url: SendOtp.getBaseURL() + "" + path
};
if (method === 'get') {
options.qs = params;
}
// Pass form data if post
if (method === 'post') {
let formKey = 'form';
if (typeof params.media !== 'undefined') {
formKey = 'formData';
}
options[formKey] = params;
}
request(options, function(error, response, data) {
// request error
if (error) {
return callback(error, data);
}
// JSON parse error or empty strings
try {
// An empty string is a valid response
if (data === '') {
data = {};
}
else {
data = JSON.parse(data);
}
}
catch(parseError) {
return callback(
new Error('JSON parseError with HTTP Status: ' + response.statusCode + ' ' + response.statusMessage),
data
);
}
// response object errors
// This should return an error object not an array of errors
if (data.errors !== undefined) {
return callback(data.errors, data);
}
// status code errors
if(response.statusCode < 200 || response.statusCode > 299) {
return callback(
new Error('HTTP Error: ' + response.statusCode + ' ' + response.statusMessage),
data
);
}
// no errors
callback(null, data);
});
};
}
module.exports = SendOtp;