-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
102 lines (88 loc) · 2.54 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
// smsbip - Infobip SMS API wrapper for NodeJS
// Created by Surya Handika Putratama <ubunteroz@gmail.com>
// Licensed under GNU Lesser General Public License v3.0
// (see LICENSE or https://www.gnu.org/licenses/lgpl-3.0.en.html)
const _ = require('underscore');
const Promise = require('bluebird');
const request = Promise.promisifyAll(require('request'));
class SMSBip {
constructor(username, password) {
this.urls = {
base: 'http://api.infobip.com',
balance: '/account/1/balance',
numberContext: '/number/1/query',
preview: '/sms/1/preview',
reports: '/sms/1/reports',
sendOne: '/sms/1/text/single',
sendMulti: '/sms/1/text/multi',
sendAdvanced: '/sms/1/text/advanced'
}
this.props = {
headers: {
'Authorization': 'Basic ' + (new Buffer(username + ':' + password)).toString('base64'),
'Accept': 'application/json'
}
}
}
_get(props) {
return request.getAsync(_.extend(this.props, props));
}
_post(props) {
return request.postAsync(_.extend(this.props, props));
}
getBalance() {
return this._get({
url: this.urls.base + this.urls.balance
});
}
getPreview(_text) {
return this._post({
url: this.urls.base + this.urls.preview,
json: {
text: _text
}
});
}
sendOne(_from, _to, _text) {
return this._post({
url: this.urls.base + this.urls.sendOne,
json: {
from: _from,
to: _to,
text: _text
}
});
}
sendMulti(_messages) {
return this._post({
url: this.urls.base + this.urls.sendMulti,
json: _messages
});
}
sendAdvanced(_messages) {
return this._post({
url: this.urls.base + this.urls.sendAdvanced,
json: _messages
});
}
getReports(_bulkId, _msgId, _limit) {
if (!_limit) _limit = 50;
return this._get({
url: this.urls.base + this.urls.reports,
json: {
bulkId: _bulkId,
messageId: _msgId,
limit: _limit
}
})
}
getNumberContext(_numbers) {
return this._post({
url: this.urls.base + this.urls.numberContext,
json: {
to: _numbers
}
})
}
}
module.exports = SMSBip;