-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
172 lines (156 loc) · 4.63 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
require("dotenv").config();
const axios = require("axios").default;
const { App } = require("@slack/bolt");
const app = new App({
token: process.env.SLACK_BOT_TOKEN,
signingSecret: process.env.SLACK_SIGNING_SECRET,
endpoints: process.env.SLACK_REQUEST_ENDPOINT || "/chomp/events",
});
const fancyLog = (msg, channel = process.env.SLACK_LOG_CHANNEL) => {
if (msg.message) console.error(msg);
else console.log(msg);
app.client.chat.postMessage({
token: process.env.SLACK_BOT_TOKEN,
//ID or name
channel,
text: null,
attachments: [
{
color: msg.message ? "#ff0000" : "#dddddd",
blocks: [
{
type: "section",
text: {
type: "plain_text",
text: `${msg.message || msg}`,
emoji: false,
},
},
],
},
],
});
};
// Listens to incoming messages that contain "!ping"
app.message("!ping", async ({ message, say }) => {
// say() sends a message to the channel where the event was triggered
await say(`Pong! <@${message.user}>`);
});
// List of reactor user ID's
const blacklist = []; //For adding tools
const whitelist = [process.env.REACTION_WHITELIST_OWNER]; //For deleting tools
app.event("reaction_added", async ({ event, ack }) => {
//item_user is user id of message, user is user id of reactor
const { reaction, item, item_user, user } = event;
const { message, channel, ts } = item;
// Ignore blacklisted user
if (blacklist.includes(user)) return;
//List of meaningul reactions
const reactions = [process.env.REACTION_ADD_TOOL, process.env.REACTION_DEL_TOOL];
if (!reactions.includes(reaction)) return;
try {
// Get target message of reaction
// Get author of target message
const [msg, author, reactor] = await Promise.all([
app.client.conversations.history({
token: process.env.SLACK_BOT_TOKEN,
channel,
latest: ts,
limit: 1,
inclusive: true,
}),
app.client.users.profile.get({
token: process.env.SLACK_BOT_TOKEN,
user: item_user,
}),
app.client.users.profile.get({
token: process.env.SLACK_BOT_TOKEN,
user,
}),
]);
switch (reaction) {
case reactions[0]:
//Count only the non-blacklisted reactors
const toolReactionGroup = msg.messages[0].reactions.find(reactionGroup => reactionGroup.name === reactions[0]);
const legitimateReactors = toolReactionGroup.users.filter(user => !blacklist.includes(user));
if (legitimateReactors.length === 1) {
await app.client.chat
.postMessage({
token: process.env.SLACK_BOT_TOKEN,
channel: process.env.SLACK_TOOLS_CHANNEL,
text: msg.messages[0].text,
icon_url: author.profile.image_original || author.profile.image_512,
username: author.profile.display_name || author.profile.real_name,
//unfurl_links: true,
unfurl_media: true,
attachments:
msg.messages[0].files?.map(file => ({
type: "image",
title: {
type: "plain_text",
text: file.title,
emoji: true,
},
image_url: file.url_private,
alt_text: file.name,
})) || null,
})
.then(res => {
fancyLog(`User @ ${reactor.profile.display_name || reactor.profile.real_name} adds tool ${ts} in ${channel}`);
})
.catch(err =>
fancyLog(
new Error(
`${err.message}. User @ ${
reactor.profile.display_name || reactor.profile.real_name
} tries to add tool ${ts} in ${channel}, but failed.`
)
)
);
} else {
fancyLog(
`User @ ${
reactor.profile.display_name || reactor.profile.real_name
} tries to add tool ${ts} in ${channel}, but isn't the first one to add this message so is rejected.`
);
}
break;
case reactions[1]:
//Check that user is in whitelist
if (!whitelist.includes(user)) {
fancyLog(
`User @ ${
reactor.profile.display_name || reactor.profile.real_name
} tried to delete message ${ts}, but is not whitelisted.`
);
break;
}
await app.client.chat
.delete({
token: process.env.SLACK_BOT_TOKEN,
channel: process.env.SLACK_TOOLS_CHANNEL,
ts,
})
.then(res => {
fancyLog(`User @ ${reactor.profile.display_name || reactor.profile.real_name} deletes message ${ts}`);
})
.catch(err =>
fancyLog(
new Error(
`${err.message}. User @ ${
reactor.profile.display_name || reactor.profile.real_name
} tried to delete message ${ts}, but failed.`
)
)
);
break;
}
} catch (err) {
fancyLog(err);
}
});
(async () => {
// Start your app
await app.start(process.env.PORT || 3003);
fancyLog("⚡️ Bolt app is running!");
})();