-
Notifications
You must be signed in to change notification settings - Fork 32
/
hipchatter.js
429 lines (386 loc) · 16.3 KB
/
hipchatter.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
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
// Dependencies
var needle = require('needle');
var async = require('async');
// Hipchatter constructor
var Hipchatter = function(token, api_root) {
this.token = token;
this.api_root = api_root || 'https://api.hipchat.com/v2/';
}
// Turns logging on for debugging
// var DEBUG = true;
var DEBUG = false;
// Hipchatter functions
Hipchatter.prototype = {
// Get capabilities
// https://www.hipchat.com/docs/apiv2/method/get_capabilities
capabilities: function(callback) {
// Make a request without an auth token, since only unauthorized users are allowed to call this resource
this.request('get', 'capabilities', {'token': ''}, callback);
},
// Updates a room
// https://www.hipchat.com/docs/apiv2/method/update_room
update_room: function(params, callback) {
this.request('put', 'room/'+params.name, params, callback)
},
// Create a new room
// https://www.hipchat.com/docs/apiv2/method/create_room
create_room: function(params, callback){
this.request('post', 'room', params, callback);
},
// Delete a room
// https://www.hipchat.com/docs/apiv2/method/delete_room
delete_room: function(room, callback){
this.request('delete', 'room/'+room, callback);
},
// Get all rooms
// https://www.hipchat.com/docs/apiv2/method/get_all_rooms
rooms: function(callback){
this.request('get', 'room', function(err, results){
if (err) callback(err);
else callback(err, results.items);
});
},
// Get room
// https://www.hipchat.com/docs/apiv2/method/get_room
get_room: function(room, callback){
this.request('get', 'room/'+room, callback);
},
// Add a member to a room
// https://www.hipchat.com/docs/apiv2/method/add_member
add_member: function(params, callback) {
this.request('put', 'room/'+params.room_name+'/member/'+params.user_email, callback);
},
// Delete a member from a room
// https://www.hipchat.com/docs/apiv2/method/remove_member
delete_member: function(params, callback) {
this.request('delete', 'room/'+params.room_name+'/member/'+params.user_email, callback);
},
// Invite a user to a room.
// https://www.hipchat.com/docs/apiv2/method/invite_member
invite_member: function(params, reason, callback) {
this.request('post', 'room/'+params.room_name+'/invite/'+params.user_email, reason, callback);
},
// Get history from room
// Takes either a room id or room name as a parameter
// https://www.hipchat.com/docs/apiv2/method/view_history
history: function(room, callback){
this.request('get', 'room/'+room+'/history', callback);
},
// Get all users
// https://www.hipchat.com/docs/apiv2/method/get_all_users
users: function(params, callback) {
if (this.isFunction(params)) { // No payload
callback = params;
params = {};
}
this.request('get', 'user', params, function(err, results) {
if(err) callback(err);
else callback(err, results.items);
});
},
// View user details
// https://www.hipchat.com/docs/apiv2/method/view_user
view_user: function(user, callback) {
this.request('get', 'user/'+user, callback);
},
// Update a user
// https://www.hipchat.com/docs/apiv2/method/update_user
update_user: function(params, callback) {
this.request('put', 'user/'+params.email, params, callback)
},
// Creates a user
// https://www.hipchat.com/docs/apiv2/method/create_user
create_user: function(params, callback) {
this.request('post', 'user', params, callback);
},
// Deletes a user
// https://www.hipchat.com/docs/apiv2/method/delete_user
delete_user: function(user, callback) {
this.request('delete', 'user/'+user, callback);
},
// Send a private message to a user
// https://www.hipchat.com/docs/apiv2/method/private_message_user
send_private_message: function(user, message, callback) {
this.request('post', 'user/'+user+'/message', message, callback);
},
// Get emoticons
//
// https://www.hipchat.com/docs/apiv2/method/get_all_emoticons
// params: {
// 'start-index': 0,
// 'max-results': 100,
// 'type': 'all'
// }
//
// or
//
// Get emoticon by shortcut or id
// https://www.hipchat.com/docs/apiv2/method/get_emoticon
//
// params = 34;
// params = 'fonzie';
//
emoticons: function(params, callback){
if (arguments.length === 1) { // only supplied a callback function
callback = params;
this.request('get', 'emoticon', function(err, results){
if (err) callback(err);
else callback(err, results.items);
});
} else if (arguments.length === 2) { // contains some type of param
if (typeof params === 'number' || typeof params === 'string') {
// get emoticon by id or shortcut
return this.get_emoticon(params, callback);
} else if (typeof params === 'object') {
// get all emoticons based on specified params
// resort to default params if input param doesn't exist or is incorrectly typed
var query = {
'start-index': 'start-index' in params ? params['start-index'] : 0,
'max-results': 'max-results' in params ? params['max-results'] : 100,
'type': 'type' in params ? params['type'] : 'all',
};
this.request('get', 'emoticon', query, function(err, results){
if (err) callback(err);
else callback(err, results.items);
});
}
}
},
// Get emoticon by id or shortcut
// https://www.hipchat.com/docs/apiv2/method/get_emoticon
//
// param = 34; // id
// param = 'fonzie'; // shortcut
//
get_emoticon: function(param, callback) {
this.request('get', 'emoticon/' + param, callback);
},
// Uses the simple "Room notification" token
// https://www.hipchat.com/docs/apiv2/method/send_room_notification
// notify: function(room, message, token, callback){
// var data = {
// color: 'green',
// message: message
// }
// needle.post(this.url('room/'+room+'/message', token), data, {json:true}, function(error, res, body){
// if (!error && res.statusCode == 204) { callback(null, body); }
// else callback(error, 'API connection error.');
// });
// },
notify: function(room, options, callback){
// convenience function notify(room, message, token, callback)
if (typeof options == 'string') {
var message = arguments[1];
var token = arguments[2];
var callback = arguments[3];
this.request('post', 'room/'+room+'/notification', {message: message, token: token}, callback);
}
else if (typeof options != 'object' && typeof options == 'function') {
options(new Error('Must supply an options object to the notify function containing at least the message. See https://www.hipchat.com/docs/apiv2/method/send_room_notification'));
}
else if (!options.hasOwnProperty('message') ) {
callback(new Error('Message is required.'));
}
else this.request('post', 'room/'+room+'/notification', options, callback);
},
send_room_message: function(room, message, callback){
//https://www.hipchat.com/docs/apiv2/method/send_message
if (typeof message == "string") {
message = {message: message};
}
this.request('post', 'room/'+room+'/message', message, callback);
},
create_webhook: function(room, options, callback){
if (typeof options != 'object' && typeof options == 'function') {
options(new Error('Must supply an options object to the notify function containing at least the message and the room notification token. See https://www.hipchat.com/docs/apiv2/method/send_room_notification'));
}
else if (!options.hasOwnProperty('url') || (!options.hasOwnProperty('event'))) {
callback(new Error('URL and Event are required.'));
}
else this.request('post', 'room/'+room+'/webhook', options, callback);
},
get_webhook: function(room, id, callback){
this.request('get', 'room/'+room+'/webhook/'+id, callback);
},
webhooks: function(room, callback){
this.request('get', 'room/'+room+'/webhook', callback);
},
delete_webhook: function(room, id, callback){
needle.delete(this.url('room/'+room+'/webhook/'+id), null, function (error, response, body) {
// Connection error
if (!!error) callback(new Error('HipChat API Error.'));
// HipChat returned an error or no HTTP Success status code
else if (body.hasOwnProperty('error') || response.statusCode < 200 || response.statusCode >= 300){
try { callback(new Error(body.error.message)); }
catch (e) {callback(new Error(body)); }
}
// Everything worked
else {
callback(null, body);
}
});
},
delete_all_webhooks: function(room, callback){
var self = this;
this.webhooks(room, function(err, response){
if (err) return callback(new Error(response));
var hooks = response.items;
var hookCalls = [];
for (var i=0; i<hooks.length; i++){
// wrapper function to preserve context of hookId
(function(hookId){
hookCalls[i] = function(done){
self.delete_webhook(room, hookId, done);
}
})(hooks[i].id);
}
async.parallel(hookCalls, callback);
});
},
set_topic: function(room, topic, callback){
this.request('put', 'room/'+room+'/topic', {topic: topic}, callback);
},
/** EXTRAS **/
// functions that are not part of the official HipChat API
// Check if a room exists
room_exists: function(room, callback){
this.get_room(room, function(err, response){
if(err === null){
return callback(null, true);
}
else if (err.message.match(/Room .* not found/)){
return callback(null, false);
}
else {
console.log(err)
return callback(err)
}
});
},
/** HELPERS **/
// Generator API url
url: function(rest_path, query, alternate_token){
// inner helpers
var BASE_URL = this.api_root + rest_path + '?auth_token=';
var queryString = function(query) {
var query_string = '';
for (var key in query) {
query_string += ('&' + key + '=' + query[key]);
}
return query_string;
};
if (arguments.length === 1) { // only contains path
var url = BASE_URL + this.token;
if (DEBUG) console.log('URL REQUEST: ', url);
return url;
} else if (arguments.length === 2) { // contains query or alt token
if (typeof query === 'object') { // query {}
var url = BASE_URL + this.token + queryString(query);
if (DEBUG) console.log('URL REQUEST: ', url);
return url;
} else { // alt token
alternate_token = query;
var token = (alternate_token == undefined) ? this.token : alternate_token;
var url = BASE_URL + token;
if (DEBUG) console.log('URL REQUEST: ', url);
return url;
}
} else if (arguments.length === 3) {
var token = (alternate_token == undefined)? this.token : alternate_token;
var url = BASE_URL + token + queryString(query);
if (DEBUG) console.log('URL REQUEST: ', url);
return url;
}
},
// Make a request
// type: required - type of REST request ('get' or 'post' currently)
// path: required -
// payload: optional - query string data for 'get', ''
// callback: required -
request: function(type, path, payload, callback){
self = this;
if (this.isFunction(payload)) { // No payload
callback = payload;
payload = {};
}
var requestCallback = function (error, response, body) {
if (DEBUG) {console.log('RESPONSE: ', error, response, body);}
// Connection error
if (!!error) callback(new Error('HipChat API Error.'));
// Rate limit error
else if (response.statusCode == 429) {
var apitimeout, floodtimeout, reset, timeout;
error = body.error.message || body.error.toString();
//check if we hit the api rate limit
reset = response.headers["x-ratelimit-reset"] * 1000;
if (reset > 0) {
//assume that the reset value will be the unix timestamp
//when the limiter will reset
apitimeout = reset - (new Date());
//if out timeout period is negative, that is probably
//because instead of a unix timestamp, the reset value
//is the number of seconds until the counter will reset.
apitimeout = (apitimeout < 0 ? reset : apitimeout);
}
//do the same for flood control
reset = response.headers["x-floodcontrol-reset"] * 1000;
if (reset > 0) {
floodtimeout = reset - (new Date());
floodtimeout = (floodtimeout<0 ? reset : floodtimeout);
}
//choose which ever timeout is bigger.
//If neither are valid, just set it to 5 minutes
timeout =
Math.max((+apitimeout || 0), (+floodtimeout || 0)) ||
300000;
//wait until it is safe to resend the request
setTimeout(
self.request.bind(self, type, path, payload, callback),
timeout
);
if(DEBUG){
console.log(
"Rate limit reached. Waiting " + timeout +
" ms before resubmitting."
);
}
}
// HipChat returned an error or no HTTP Success status code
else if (body.hasOwnProperty('error') || response.statusCode < 200 || response.statusCode >= 300){
try { callback(new Error(body.error.message)); }
catch (e) {callback(new Error(body)); }
}
// Everything worked
else {
if (self.isFunction(callback)) {
callback(null, body, response.statusCode);
}
}
};
// GET request
if (type.toLowerCase() === 'get') {
var url = payload.hasOwnProperty('token') ? this.url(path, payload, payload.token) : this.url(path, payload);
needle.get(url, requestCallback);
// POST request
} else if (type.toLowerCase() === 'post') {
var url = payload.hasOwnProperty('token') ? this.url(path, payload.token) : this.url(path);
needle.post(url, payload, {json: true, headers:{'Content-Type': 'application/json; charset=utf-8'}}, requestCallback);
// PUT request
} else if (type.toLowerCase() === 'put') {
needle.put(this.url(path), payload, {json: true, headers:{'Content-Type': 'application/json; charset=utf-8'}}, requestCallback);
// DELETE request
} else if (type.toLowerCase() === 'delete') {
needle.delete(this.url(path), {}, requestCallback);
// otherwise, something went wrong
} else {
if (self.isFunction(callback)) {
callback(new Error('Invalid use of the hipchatter.request function.'));
}
}
},
isFunction: function(obj) {
return !!(obj && obj.constructor && obj.call && obj.apply);
}
/** END HELPERS **/
}
module.exports = Hipchatter;