-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
152 lines (135 loc) · 2.99 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
/**
* Load Module Dependencies
*/
const request = require('request');
const {merge} = require('merge');
const WEB_SERVICE_URL = 'http://infopi.io/text/index.php';
/**
* Send SMS
*
* Params:
* - mandatory: to, msg, u, h
* - optional: type, unicode, from, footer, nofooter, format
*/
exports.sendSMS = function sendSMS(data, cb) {
data = merge(data, { op: 'pv' });
return sendRequest(data, cb);
};
/**
* Outgoing SMS and Delivery status
*
* Params:
* - mandatory: u, h
* - optional: queue, src, dst, dt, smslog_id, c, last, format
*/
exports.outgoingSMS = function outgoingSMS(data, cb) {
data = merge(data, { op: 'ds' });
return sendRequest(data, cb);
};
/**
* List Incoming SMS
*
* Params:
* - mandatory: u, h
* - optional: queue, src, dst, dt, smslog_id, c, last, format
*/
exports.incomingSMS = function incomingSMS(data, cb) {
data = merge(data, { op: 'in' });
return sendRequest(data, cb);
};
/**
* List SMS on user's inbox
*
* Params:
* - mandatory: u, h
* - optional: queue, src, dst, dt, smslog_id, c, last, format
*/
exports.showInbox = function showInbox(data, cb) {
data = merge(data, { op: 'ix' });
return sendRequest(data, cb);
};
/**
* Get User's Credit information
*
* Params:
* - mandatory: u, h
* - optional: format
*/
exports.getUserCredit = function getUserCredit(data, cb) {
data = merge(data, { op: 'cr' });
return sendRequest(data, cb);
};
/**
* Get User's webservice token.
* This can be used as a login mechanism
*
* Params:
* - mandatory: u, p
* - optional: format
*/
exports.getToken = function getToken(data, cb) {
data = merge(data, { op: 'get_token' });
return sendRequest(data, cb);
};
/**
* Set User's webservice token.
* This can be used as a password change mechanism.
*
* Params:
* - mandatory: u, h
* - optional: format
*/
exports.setToken = function setToken(data, cb) {
data = merge(data, { op: 'set_token' });
return sendRequest(data, cb);
};
/**
* Get Contact List by name or code
*
* Params:
* - mandatory: u, h, kwd
* - optional: format, c
*/
exports.getContactList = function getContactList(data, cb) {
data = merge(data, { op: 'get_contact_group' });
return sendRequest(data, cb);
};
/**
* Query Server for useful info.
*
* Params:
* - mandatory: u, h
* - optional: format
*/
exports.serverInfo = function serverInfo(data, cb) {
data = merge(data, { op: 'query' });
return sendRequest(data, cb);
};
/**
* Wrapper for request handler
*
* @private
*/
function sendRequest(payload, cb) {
var info;
var qsParams = { app: 'ws', u: '', h: '' };
var qs = merge(qsParams, payload);
info = {
uri: WEB_SERVICE_URL,
qs: qs
};
request(info, function (err, res, body) {
if(err) return cb(err);
try {
body = JSON.parse(body);
} catch(ex) {
return cb(ex);
}
if(body.error_string) {
err = new Error(body.error_string);
err.code = body.error;
return cb(err);
}
cb(null, body);
});
}