-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
190 lines (178 loc) · 5.29 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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
const { App, ExpressReceiver } = require("@slack/bolt");
const dotenv = require("dotenv");
dotenv.config();
const databaseData = {};
const database = {
set: async (key, data) => {
databaseData[key] = data;
},
get: async (key) => {
return databaseData[key];
},
};
const receiver = new ExpressReceiver({
signingSecret: process.env.SLACK_SIGNING_SECRET,
clientId: process.env.SLACK_CLIENT_ID,
clientSecret: process.env.SLACK_CLIENT_SECRET,
stateSecret: "my-state-secret",
scopes: [
"channels:manage",
"channels:read",
"chat:write",
"commands",
"incoming-webhook",
"groups:read",
"reactions:read",
],
installationStore: {
storeInstallation: async (installation) => {
// change the line below so it saves to your database
if (
installation.isEnterpriseInstall &&
installation.enterprise !== undefined
) {
// support for org wide app installation
return await database.set(installation.enterprise.id, installation);
}
if (installation.team !== undefined) {
// single team app installation
return await database.set(installation.team.id, installation);
}
throw new Error("Failed saving installation data to installationStore");
},
fetchInstallation: async (installQuery) => {
// change the line below so it fetches from your database
if (
installQuery.isEnterpriseInstall &&
installQuery.enterpriseId !== undefined
) {
// org wide app installation lookup
return await database.get(installQuery.enterpriseId);
}
if (installQuery.teamId !== undefined) {
// single team app installation lookup
return await database.get(installQuery.teamId);
}
throw new Error("Failed fetching installation");
},
deleteInstallation: async (installQuery) => {
// change the line below so it deletes from your database
if (
installQuery.isEnterpriseInstall &&
installQuery.enterpriseId !== undefined
) {
// org wide app installation deletion
return await database.delete(installQuery.enterpriseId);
}
if (installQuery.teamId !== undefined) {
// single team app installation deletion
return await database.delete(installQuery.teamId);
}
throw new Error("Failed to delete installation");
},
},
redirectUri:
"https://a66e-2401-4900-1c5c-d73-98d6-8a8a-2aa1-2fb8.in.ngrok.io/slack/oauth_redirect",
installerOptions: {
redirectUriPath: "/slack/oauth_redirect",
// If this is true, /slack/install redirects installers to the Slack authorize URL
// without rendering the web page with "Add to Slack" button.
// This flag is available in @slack/bolt v3.7 or higher
directInstall: true,
},
});
const app = new App({ receiver });
app.event("member_joined_channel", async ({ event, client, say }) => {
try {
// Call chat.postMessage with the built-in client
const welcomeChannelId = event.channel;
const result = await client.chat.postMessage({
channel: welcomeChannelId,
text: `Welcome to the team, <@${event.user}>! 🎉 You can introduce yourself in this channel.`,
});
console.log(result);
} catch (error) {
console.error(error);
}
});
app.message("Hello", async ({ message, say }) => {
try {
await say({
blocks: [
{
type: "section",
text: {
type: "mrkdwn",
text: `Hey there <@${message.user}>!`,
},
accessory: {
type: "button",
text: {
type: "plain_text",
text: "Click Me",
},
action_id: "button_click",
},
},
],
text: `Hey there <@${message.user}>!`,
});
} catch (error) {
console.error(error);
}
});
app.action("button_click", async ({ body, ack, say }) => {
// Acknowledge the action
await ack();
await say(`<@${body.user.id}> clicked the button`);
});
app.message("knock knock", async ({ message, say }) => {
await say(`_Who's there?_`);
});
app.command("/echo", async ({ command, ack, respond }) => {
// Acknowledge command request
await ack();
console.log(command);
await respond(`${command.text}`);
});
app.event("reaction_added", async ({ event, say }) => {
let date = new Date();
let dateString = new Date(date.getTime() - date.getTimezoneOffset() * 60000)
.toISOString()
.split("T")[0];
if (event.reaction === "calendar") {
await say({
blocks: [
{
type: "section",
text: {
type: "mrkdwn",
text: "Pick a date for me to remind you",
},
accessory: {
type: "datepicker",
action_id: "datepicker_remind",
initial_date: dateString,
placeholder: {
type: "plain_text",
text: "Select a date",
},
},
},
],
text: "Pick a date for me to remind you",
});
}
});
app.action("datepicker_remind", async ({ body, ack, say }) => {
// Acknowledge the action
await ack();
await say(
`<@${body.user.id}> picked a date: ${body.actions[0].selected_date}`
);
});
const startApp = async () => {
await app.start(3000);
console.log("⚡️ Bolt app started");
};
startApp();