-
Notifications
You must be signed in to change notification settings - Fork 2
/
robin.js
81 lines (73 loc) · 2.63 KB
/
robin.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
// Description:
// A hubot robinpowered integration for getting room occupancy and other data
// about your office
//
// Configuration:
// ROBIN_TOKEN
// ROBIN_ORGANIZATION
//
// Commands:
// hubot rooms - get all free rooms in all offices/locations
// hubot rooms <location> - get all free rooms at the giving location
//
// Notes:
// This integration assumes your organization has less than 100 rooms (spaces) in it
//
// Author:
// Santiago Suarez Ordoñez <santiycr@gmail.com>
// Gavin Mogan <gavin@gavinmogan.com>
const once = require('lodash.once');
const Robin = require('robin-js-sdk');
const fuzzy = require('fuzzy');
const robinToken = process.env.ROBIN_TOKEN;
const robinOrg = process.env.ROBIN_ORGANIZATION;
module.exports = function(robot) {
let robin = new Robin(robinToken);
const getLocations = once(function getLocations() {
return robin.api.organizations.locations.get(robinOrg).then(function(response) {
return response.getData();
});
});
function filter(locations, filter) {
if (!filter) { return locations; }
const options = {
extract: l => l.name
};
return fuzzy.filter(filter, locations, options).map(i => i.original).slice(0);
}
function handler(res) {
robin = new Robin(robinToken);
return getLocations()
.then(locations => filter(locations, res.match[1]))
.then(locations => {
return Promise.all(locations.map(l => robin.api.locations.spaces.get(l.id)))
.then(locations => {
let final_response = '';
locations.forEach(location => {
for (let space of location.getData()) {
let { name } = space;
if (!space.current_event) {
let time_notice = '';
if (space.next_event) {
var next_event_date = new Date(space.next_event.started_at);
}
let time_diff = next_event_date ? (next_event_date.getTime() - Date.now()) / 1000 : 0;
if (time_diff && time_diff < (60 * 60)) { // 1 hour
time_notice = ` for ${Math.round(time_diff / 60)} minutes`;
}
final_response += `\n${name} is free${time_notice}`;
}
}
});
return final_response;
});
}).then(final_response => {
return res.reply(final_response ? final_response : 'no rooms available :(');
}).catch(function(err) {
console.error('robin err', err); // eslint-disable-line no-console
throw err;
});
}
robot.respond(/rooms$/, handler);
robot.respond(/rooms (.*)$/, handler);
};