forked from tiny-bot/default-slack-api.exe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
54 lines (38 loc) · 1.31 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
const ts = require('tinyspeck'),
onboarding = require('./onboarding.json'),
{PORT, TOKEN} = process.env,
users = {};
// setting defaults for all Slack API calls
let slack = ts.instance({ token: TOKEN });
// build the user's current onboarding message
function getStatusMessage(user) {
return Object.assign({ channel: user }, onboarding.welcome, users[user]);
}
// watch for onboarding slash commands
slack.on('/onboarding', payload => {
let {user_id, response_url} = payload;
let message = getStatusMessage(user_id);
// send current onboarding status privately
slack.send(response_url, message);
});
// event handler
slack.on('star_added', 'pin_added', 'reaction_added', payload => {
let {type, user, item} = payload.event;
// get the user's current onboarding message
let message = getStatusMessage(user);
// modify completed step
message.attachments.forEach(step => {
if (step.event === type && !step.completed) {
step.title += " :white_check_mark:";
step.color = "#2ab27b";
step.completed = true;
}
});
// save the message and update the timestamp
slack.send(message).then(res => {
let {ts, channel} = res.data;
users[user] = Object.assign({}, message, { ts: ts, channel: channel });
});
});
// incoming http requests
slack.listen(PORT);