forked from nluo/pushwoosh-node-client
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
185 lines (152 loc) · 4.77 KB
/
index.js
File metadata and controls
185 lines (152 loc) · 4.77 KB
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
180
181
182
183
184
185
var request = require('request'),
extend = require('util')._extend,
errors = require('./lib/errors'),
host = 'https://cp.pushwoosh.com/json',
apiVersion = '1.3';
function PushwooshClient(appCode, authToken, options) {
if (!appCode || !authToken) {
throw new Error('Application/ApplicationsGroup ID and Authentication Token from Pushwoosh must be provided');
}
this.appCode = appCode;
this.authToken = authToken;
// parse the options
options = options || {};
this.host = options.host || host;
this.apiVersion = options.apiVersion || apiVersion;
this.useApplicationsGroup = options.useApplicationsGroup;
};
PushwooshClient.prototype.sendMessage = function (msg, device, options, callback) {
var client = this;
if (!msg) {
return callback(new Error('Message has to be provided'));
}
if (!options) {
callback = device;
options = {};
device = null;
}
if (!callback) {
callback = options;
if (typeof device === 'object') {
options = device;
device = null;
}
if (typeof device === 'string') {
options = {};
}
}
var devices = [];
if (device && typeof device === 'string') {
devices.push(device);
}
if (device && device instanceof Array) {
devices = device;
}
var defaultOptions;
if(devices && devices.length>0){
defaultOptions = {
send_date: 'now',
ignore_user_timezone: true,
content: msg,
devices: devices
};
}else{
defaultOptions = {
send_date: 'now',
ignore_user_timezone: true,
content: msg
};
}
var notification = extend(defaultOptions, options);
var body = {
request: {
auth: client.authToken,
notifications: [notification]
}
};
if (client.useApplicationsGroup) {
body.request.applications_group = client.appCode;
} else {
body.request.application = client.appCode;
}
client.sendRequest('createMessage', body, function (error, response, body) {
if (error) {
return callback(error);
}
client.parseResponse(response, body, callback);
});
};
PushwooshClient.prototype.deleteMessage = function (msgCode, callback) {
var client = this;
if (typeof msgCode !== 'string') {
return callback(new Error('Message code must be provided'));
}
var body = {
request: {
auth: client.authToken,
message: msgCode
}
};
client.sendRequest('deleteMessage', body, function(error, response, body){
if (error) {
return callback(error);
}
client.parseResponse(response, body, callback);
})
};
PushwooshClient.prototype.registerDevice = function (options, callback) {
var client = this;
if (typeof options.push_token !== 'string') {
return callback(new Error('Device push token must be provided (string)'));
}
if (typeof options.hwid !== 'string') {
return callback(new Error('Device hwid must be provided (string)'));
}
if (typeof options.device_type !== 'number') {
return callback(new Error('Device type must be provided (number)'));
}
var body = {
request: {
application: client.appCode,
push_token: options.push_token,
hwid: options.hwid,
device_type: options.device_type
}
};
if (options.timezone) {
body.request.timezone = options.timezone;
}
if (options.language) {
body.request.language = options.language;
}
client.sendRequest('registerDevice', body, function(error, response, body){
if (error) {
return callback(error);
}
client.parseResponse(response, body, callback);
});
};
PushwooshClient.prototype.sendRequest = function (method, data, callback) {
request({
method: 'POST',
uri: this.host + '/' + this.apiVersion + '/' + method,
json: true,
body: data
}, callback);
};
PushwooshClient.prototype.parseResponse = function(response, body, callback) {
if (response.statusCode === 200 && body.status_code === 200) {
return callback(null, body.response);
}
if (response.statusCode === 200 && body.status_code === 210) {
return callback(null, {description: 'Argument error', detail: body.status_message, code: 210});
}
if (response.statusCode === 500) {
return callback(new errors.Internal());
}
if (response.statusCode === 400) {
return callback(new errors.Malformed());
}
callback(new Error('Unknown response code / error'));
};
module.exports = PushwooshClient;