-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
135 lines (129 loc) · 3.87 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
const express = require('express');
const bodyParser = require('body-parser');
const logger = require('morgan');
const emojiRegex = require('emoji-regex');
const nodeEmoji = require('node-emoji');
const slack = require('slack');
const moment = require('moment');
const app = express();
const port = process.env.PORT || 5000;
app.use(logger('dev'));
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
const router = express.Router();
app.post('/', (req, res, next) => {
// check for secret token
if (!req.body.token || req.body.token !== process.env.SECRET_TOKEN) {
next();
return;
}
// store token
const token = process.env.SLACK_TOKEN;
// log some stuff for dev
console.log(req.body);
// grab status and emojis and clean it up
let status = req.body.title;
let statusEmoji = ':moo:';
const statusHasEmoji = emojiRegex().exec(status);
if (statusHasEmoji) {
statusEmoji = nodeEmoji.unemojify(statusHasEmoji[0]);
console.log(`CUSTOM EMOJI! ${statusEmoji}`);
status = nodeEmoji.strip(status);
}
// additional tokens
const dndToken = '[DND]';
const awayToken = '[AWAY]';
const aways = [awayToken, 'AFK', 'LMAFK'];
// parse event start/stop time
const dateFormat = 'MMM D, YYYY [at] hh:mmA';
const start = moment(req.body.start, dateFormat);
const end = moment(req.body.end, dateFormat);
const startUtc = moment(req.body.start, dateFormat).subtract(8, 'hours');
const endUtc = moment(req.body.end, dateFormat).subtract(8, 'hours');
// check for DND
if (status.includes(dndToken)) {
slack.dnd.setSnooze({
token,
num_minutes: end.diff(start, 'minutes')
});
status = status.replace(dndToken, '').trim();
}
// check for AWAY
slack.users.setPresence({
token,
presence: aways.some(e => status.includes(e)) ? 'away' : 'auto'
});
if (status.includes(awayToken)) {
status = status.replace(awayToken, '').trim();
}
// Common Statuses
if ( status.match('Pocket Casts') ) {
statusEmoji = ':party-casts:';
} else if ( status.match('Woo Chat Reserve') ) {
statusEmoji = ':happychat:';
} else if ( status.match('Woo Chat') ) {
statusEmoji = ':chat-green:';
} else if ( status.match('Woo Tickets') ) {
statusEmoji = ':zendesk2:';
} else if ( status.match('Woo Forums') ) {
statusEmoji = ':wpicon-black-light:';
} else if ( status.match('Weekend') ) {
statusEmoji = ':weekend-2:';
} else if ( status.match('Flex') ) {
statusEmoji = ':flexday:';
} else if ( status.match('LMAFK') ) {
statusEmoji = ':lmafk:';
} else if ( status.match('AFK') ) {
statusEmoji = ':afk:';
} else {
statusEmoji = ':moo:';
}
// set status
status = `${status} from ${startUtc.format('h:mm')} to ${endUtc.format('h:mm a')} ${process.env.TIME_ZONE}`;
let profile = JSON.stringify({
"status_text": status,
"status_emoji": statusEmoji,
"status_expiration": end.unix()
});
console.log(profile);
slack.users.profile.set({ token, profile });
console.log(`Status set as "${status}" and will expire at ${end.format('h:mm a')}`);
res.status(200);
res.send('🤘');
});
app.get('/', (req, res, next) => {
// welcome message
res.send(`
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Welcome!</title>
<style>
pre {
background-color: #DDD;
padding: 1em;
display: inline-block;
}
</style>
</head>
<body>
<h1>Your Heroku server is running!</h1>
<p>You'll need the following information for your IFTTT recipe:</p>
<h3>Body</h3>
<pre>{
"title":"<<<{{Title}}>>>",
"start":"{{Starts}}",
"end":"{{Ends}}",
"token": "${process.env.SECRET_TOKEN}"
}</pre>
</body>
</html>
`);
});
app.use((req, res, next) => {
res.status(404);
res.send('Not found');
});
app.listen(port);
console.log(`Server running on port ${port}`);