-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
84 lines (63 loc) · 1.96 KB
/
Copy pathindex.js
File metadata and controls
84 lines (63 loc) · 1.96 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
const { App } = require("@slack/bolt");
const token = process.env.token;
const signingSecret = process.env.signingSecret;
const dropdownContent = require("./dropdown/dropdown.json");
const { WebClient, LogLevel } = require("@slack/web-api");
const mongoose = require("mongoose");
const { saveToDataBase, getTime } = require("./database/database");
//Connect to DB
mongoose.set("useFindAndModify", false);
mongoose
.connect(
process.env.DB,
{ useUnifiedTopology: true },
{ useNewUrlParser: true }
)
.then(() => console.log("DB connected!"));
//Instantiate new client
const client = new WebClient(token, {
logLevel: LogLevel.DEBUG,
});
//Instantiate new slack bolt app
const app = new App({
token: token,
signingSecret: signingSecret,
});
//Listen for app mention event
app.event("app_mention", async ({ event, say }) => {
if (event.text.includes("saved")) {
(async () => {
const savedTime = await getTime(event.user);
client.chat.postEphemeral({
channel: event.channel,
user: event.user,
text: savedTime,
});
})();
}
if (event.text.includes("hello")) return say(`Hello, <@${event.user}>!`);
if (event.text.includes("time")) {
await say({
blocks: dropdownContent,
});
}
});
//Listen and respond to user selection
app.action("text1234", async ({ body, ack, say }) => {
await ack();
const userId = body.user.id,
username = body.user.name,
channelId = body.channel.id;
const timeSlots = body.actions[0].selected_options;
if (timeSlots.length > 2 || timeSlots.length == 1) {
return client.chat.postEphemeral({
channel: channelId,
user: userId,
text: `Hello ${username}, you selected ${timeSlots.length} option(s)! pls select 2 options`,
});
} else {
const selectedTimeSlots = `${timeSlots[0].text.text} and ${timeSlots[1].text.text}`;
return saveToDataBase(userId, selectedTimeSlots);
}
});
app.start(process.env.PORT || 4000);