This repository has been archived by the owner on Mar 16, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
156 lines (124 loc) · 3.78 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
'use strict';
var API_KEY = undefined, URL_BASE = 'http://mainsms.ru/api/mainsms',
crypto = require('crypto'),
http = require('http');
// tools, mainsms.ru/home/api
function getUrlParams (options) {
var signature = '', params = '',
keys = Object.keys(options).sort(),
i, imax = keys.length, key, value;
for (i = 0; i < imax; i++) {
key = keys[i];
value = options[key];
if (value || value === '' || value === 0) {
signature += value + ';';
if (key == 'message') {
params += key + '=' + encodeURIComponent(value) + '&';
} else {
params += key + '=' + value + '&';
}
}
}
signature += API_KEY;
signature = crypto.createHash('sha1').update(signature).digest('hex');
signature = crypto.createHash('md5').update(signature).digest('hex');
params += 'sign=' + signature;
return params;
}
function sendRequest (url_base, options, callback) {
var url = url_base + '?' + getUrlParams(options);
http
.get(url, function (res) {
var body = '';
// http error
if (res.statusCode !== 200)
callback({
code: res.statusCode,
message: 'HTTP ERROR: bad response code'
});
// http ok
else {
res.on('data', function (chunk) { body += chunk; });
res.on('end', function () {
// parse response
body = JSON.parse(body);
// mainsms ok
if (body.status === 'success') {
delete body.status;
callback(null, body);
}
// mainsms error
else if (body.status === 'error')
callback({
code: body.error,
message: body.message
});
// mainsms unknown error
else callback({
code: 0,
message: 'MAINSMS ERROR: unknown error'
});
});
}
})
// request error
.on('error', function (err) {
callback({
code: -1,
message: 'REQUEST ERROR: ' + err.message
});
});
}
// message, mainsms.ru/home/mainapi
var message = (function () {
var URL_GROUP = URL_BASE + '/message';
return {
send: function (options, callback) {
// allow array of recipients
if (options.recipients instanceof Array) options.recipients = options.recipients.join(',');
//
sendRequest(URL_GROUP + '/send', options, callback);
},
status: function (options, callback) {
// allow array of messages_id
if (options.messages_id instanceof Array) options.messages_id = options.messages_id.join(',');
//
sendRequest(URL_GROUP + '/status', options, callback);
},
price: function (options, callback) {
// allow array of recipients
if (options.recipients instanceof Array) options.recipients = options.recipients.join(',');
//
sendRequest(URL_GROUP + '/price', options, callback);
},
balance: function (options, callback) {
sendRequest(URL_GROUP + '/balance', options, callback);
},
info: function (options, callback) {
// allow array of phones
if (options.phones instanceof Array) options.phones = options.phones.join(',');
//
sendRequest(URL_GROUP + '/info', options, callback);
}
};
})();
// export
module.exports = function (key) {
// fail
if (!key) {
console.log('node-mainsmsru: api key required');
return null;
}
// ok
else {
API_KEY = key;
return {
message: message,
sending: null,
batch: null,
group: null,
contact: null,
sender: null
};
}
};