-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
158 lines (145 loc) · 3.94 KB
/
app.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
require('dotenv').config();
const { App } = require("@slack/bolt");
// #channel-watch
const NOTIFY_CHANNEL_ID = process.env.SLACK_NOTIFY_CHANNEL_ID;
const app = new App({
token: process.env.SLACK_BOT_TOKEN,
signingSecret: process.env.SLACK_SIGNING_SECRET,
appToken: process.env.SLACK_APP_TOKEN,
socketMode: true,
});
const channelCache = new Map();
const getChannleList = async (client, logger) => {
try {
let cursor;
do {
const result = await app.client.conversations.list({ limit: 1000, cursor });
result.channels.forEach(channel => {
channelCache.set(channel.id, channel.name);
});
cursor = result.response_metadata?.next_cursor;
} while (cursor);
} catch (error) {
logger.error(error);
}
};
const postNotification = async (
client,
emoji,
action,
chid,
chname,
logger,
isDeleted = false,
oldName = null
) => {
try {
if (isDeleted) {
messageText = `${emoji} ${action}: #${chname}`;
} else if (oldName) {
messageText = `${emoji} ${action}: <#${chid}|${chname}> (#${oldName} → #${chname})`;
} else {
messageText = `${emoji} ${action}: <#${chid}|${chname}>`;
}
await client.chat.postMessage({
channel: NOTIFY_CHANNEL_ID,
text: messageText,
});
logger.info(`Posted notification: ${messageText}`);
} catch (error) {
logger.error(error);
}
};
app.view("modal-id", async ({ ack, view, logger }) => {
logger.info(`Submitted data: ${view.state.values}`);
await ack();
});
app.event("channel_archive", async ({ event, client, logger }) => {
try {
const { channel: chid } = event;
const result = await app.client.conversations.info({ channel: chid });
await postNotification(
client,
":ghost:",
"Archived",
chid,
result.channel.name,
logger
);
} catch (error) {
logger.error(error);
}
});
app.event("channel_unarchive", async ({ event, client, logger }) => {
try {
const { channel: chid } = event;
const result = await app.client.conversations.info({ channel: chid });
await postNotification(
client,
":sushi:",
"Unarchived",
chid,
result.channel.name,
logger
);
} catch (error) {
logger.error(error);
}
});
app.event("channel_created", async ({ event, client, logger }) => {
try {
const { id: chid, name: chname } = event.channel;
channelCache.set(chid, chname);
await postNotification(
client,
":hatching_chick:",
"Created",
chid,
chname,
logger
);
} catch (error) {
logger.error(error);
}
});
app.event("channel_rename", async ({ event, client, logger }) => {
try {
const { id: chid, name: chname } = event.channel;
const oldName = channelCache.get(chid) || "Unknown";
channelCache.set(chid, chname);
await postNotification(
client,
":ocean:",
"Renamed",
chid,
chname,
logger,
false,
oldName
);
} catch (error) {
logger.error(error);
}
});
app.event("channel_deleted", async ({ event, client, logger }) => {
try {
const { channel: chid } = event;
const chname = channelCache.get(chid) || "Unknown";
await postNotification(
client,
":fire:",
"Deleted",
chid,
chname,
logger,
true
);
} catch (error) {
logger.error(error);
}
});
(async () => {
await getChannleList(app.client, app.logger);
await app.start();
console.log("⚡️ Bolt app started");
})();