-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathslack.js
More file actions
92 lines (72 loc) · 1.93 KB
/
slack.js
File metadata and controls
92 lines (72 loc) · 1.93 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
85
86
87
88
89
90
91
92
const { App, LogLevel } = require("@slack/bolt");
const { Study } = require("./models");
let logLevel = LogLevel.INFO;
const {
SLACK_BOT_TOKEN4: token,
SLACK_APP_TOKEN4: appToken,
SLACK_SIGNING_SECRET4: signingSecret,
} = process.env;
const boltApp = new App({
token,
logLevel,
signingSecret,
socketMode: true,
appToken,
});
boltApp.action("approve", async ({ ack, body }) => {
await ack();
console.log("승인됨");
body.actions.forEach(async (action) => {
const id = action.value;
const beforeStudyStatus = (await Study.findByPk(id)).status;
try {
const res = await Study.update(
{
status: "ALLOWED",
},
{ where: { id } }
);
const afterStudyStatus = (await Study.findByPk(id)).status;
if (beforeStudyStatus !== afterStudyStatus)
postMessage("스터디 개설을 승낙합니다.", body);
} catch (error) {
console.error(error);
}
});
});
boltApp.action("reject", async ({ ack, body }) => {
await ack();
console.log("거절됨");
body.actions.forEach(async (action) => {
const id = action.value;
const beforeStudyStatus = (await Study.findByPk(id)).status;
try {
const res = await Study.update(
{
status: "REJECTED",
},
{ where: { id } }
);
const afterStudyStatus = (await Study.findByPk(id)).status;
if (beforeStudyStatus !== afterStudyStatus)
postMessage("스터디 개설을 거절합니다.", body);
} catch (error) {
console.error(error);
}
});
});
const postMessage = async (text, body) => {
await boltApp.client.chat.postMessage({
token,
channel: body.channel.id,
thread_ts: body.message.ts,
text,
});
};
(async () => {
const port = 3000;
// Start your app
await boltApp.start(process.env.PORT || port);
console.log(`⚡️ Slack Bolt app is running on port ${port}!`);
})();
module.exports = boltApp;