-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
140 lines (130 loc) · 3.3 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
/**
* SMSFeedback message sender
* API doc: http://www.smsfeedback.ru/smsapi/
*/
var request = require('request');
var SERVER_URL = 'https://api.smsfeedback.ru/messages/v2';
var SEND_ENDPOINT = '/send';
var MESSAGE_STATUS_ENDPOINT = '/status';
var STATUS_QUEUE_ENDPOINT = '/statusQueue';
var BALANCE_ENDPOINT = '/balance';
var SENDERS_ENDPOINT = '/senders';
/**
* SMS Sender
*
* @param {string} username
* @param {string} password
* @param {string} queueName message queue name
*/
function Sender (username, password, queueName) {
this._username = username;
this._password = password;
this._queueName = queueName;
}
/**
* Send sms message
*
* @param {string} from from phone number
* @param {string} to to phone number
* @param {string} text message text
* @param {function} cb callback method
*/
Sender.prototype.send = function (from, to, text, cb) {
this._request(SEND_ENDPOINT, {
phone: to,
text: text,
sender: from,
statusQueueName: this._queueName
}, function (err, res, body) {
if (err) return cb(err);
var parts = body.split(';');
if (parts[0] !== 'accepted') return cb(parts[0]);
return cb(null, parts[1]);
})
}
/**
* Get message status
*
* @param {string} messageId message id
* @param {function} cb callback method
*/
Sender.prototype.getMessageStatus = function (messageId, cb) {
this._request(MESSAGE_STATUS_ENDPOINT, {id: messageId}, function (err, res, body) {
if (err) return cb(err);
var parts = body.split(';')
cb(null, parts[1]);
})
}
/**
* List messages in queue
*
* @param {number} limit [optional] limit messages
* @param {function} cb callback method
*/
Sender.prototype.listQueue = function (limit, cb) {
if (typeof limit === 'function') {
cb = limit;
limit = 0;
}
limit = limit || 100; // default limit
this._request(STATUS_QUEUE_ENDPOINT, {
statusQueueName: this._queueName,
limit: limit
}, function (err, res, body) {
if (err) return cb(err);
var results = [];
body.split('\n').forEach(function (line) {
var parts = line.split(';');
results.push({id: parts[0], status: parts[1]});
})
cb(null, results);
});
}
/**
* Get account balance
*
* @param {function) cb callback method
*/
Sender.prototype.getBalance = function (cb) {
this._request(BALANCE_ENDPOINT, {}, function (err, res, body) {
if (err) return cb(err);
var results = [];
body.split('\n').forEach(function (line) {
var parts = line.split(';');
results.push({type: parts[0], balance: parts[1], credit: parts[2]});
})
cb(null, results);
})
}
/**
* List of available senders
*
* @param {function} cb callback method
*/
Sender.prototype.listSenders = function (cb) {
this._request(SENDERS_ENDPOINT, {}, function (err, res, body) {
if (err) return cb(err);
var results = [];
body.split('\n').forEach(function (line) {
var parts = line.split(';');
results.push({name: parts[0], status: parts[1], note: parts[2]});
})
cb(null, results);
})
}
/**
* Service request helper
*
* @param {string} endpoint
*/
Sender.prototype._request = function (endpoint, query, cb) {
request.get({
uri: SERVER_URL + endpoint,
qs: query,
auth: {
username: this._username,
password: this._password
}
}, cb);
}
exports = module.exports = Sender;