This repository was archived by the owner on Jan 19, 2023. It is now read-only.
forked from slackapi/hubot-slack
-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathriddler_when_build_night.js
More file actions
62 lines (59 loc) · 2.18 KB
/
riddler_when_build_night.js
File metadata and controls
62 lines (59 loc) · 2.18 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
_ = require('lodash/fp');
var eventsUrl = process.env.EVENTS_URL;
const buildNightChannels = ['chat','buildnight']
module.exports = function (robot) {
var now;
var titleOf = function (e) { return e.title; };
var dateOf = function (e) { return e.date; };
var locationOf = function (e) { return e.venue_name || "(location not set)"; };
var dateSorted = _.sortBy(dateOf);
var dateFilter = _.filter(function (event) { return (event.date !== null); });
var findBuildNight = _.find(function (e) {
return e.title === 'Build Night' && new Date(e.date) >= now;
});
var ensureFindBuildNight = function (e) {
if (findBuildNight(e) === undefined) {
return "Build Night has not been scheduled.";
} else {
return concatBuildNight(findBuildNight(e));
}
};
var regexDate = function (date) { return /(\d+).(\d+).(\d+).(\d+).(\d+)/.exec(date); };
var formatDate = function (date) {
if ((date !== false) && (date !== "(date not set)")) {
var regexed = regexDate(date);
return regexed[2] + "/" + regexed[3] + "/" + regexed[1] + " at " + formatTime(regexed[4], regexed[5]);
} else {
return "(date not set)";
}
};
var formatTime = function (hour, minute) {
var parsedHour = parseInt(hour);
if (parsedHour > 12) {
parsedHour -= 12;
return parsedHour.toString(10) + ":" + minute + "pm";
} else {
return hour + ":" + minute + "am";
};
};
var concatBuildNight = function (e) { return "Build Night is on " + formatDate(dateOf(e)) + ", " + locationOf(e); };
var nextBuildNight = _.flow(dateFilter, dateSorted, ensureFindBuildNight);
return robot.hear(/(when|where)\b(.*?)\b(build night)/i, function (msg) { // returns when/where of build night
if(buildNightChannels.indexOf(msg.channel) === -1) {
return;
}
if (!eventsUrl) {
msg.send('Please set the EVENTS_URL environment variable.');
return;
}
return msg.http(eventsUrl).get()(function (err, res, body) {
if (err) {
msg.send('Sorry, there was an error getting the event list.');
return;
}
now = new Date();
var json = JSON.parse(body);
return msg.send(nextBuildNight(json));
});
});
};