-
Notifications
You must be signed in to change notification settings - Fork 96
/
index.ts
108 lines (88 loc) · 2.86 KB
/
index.ts
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
import { loadEnv } from './dotenv';
loadEnv();
import { App } from '@slack/bolt';
import { ConsoleLogger, LogLevel } from '@slack/logger';
import * as middleware from './custom-middleware';
import { DeepLApi } from './deepl';
import * as runner from './runnner';
import * as reacjilator from './reacjilator';
const logLevel = process.env.SLACK_LOG_LEVEL as LogLevel || LogLevel.INFO;
const logger = new ConsoleLogger();
logger.setLevel(logLevel);
const deepLAuthKey = process.env.DEEPL_AUTH_KEY;
if (!deepLAuthKey) {
throw "DEEPL_AUTH_KEY is missing!";
}
const deepL = new DeepLApi(deepLAuthKey, logger);
const app = new App({
logger,
token: process.env.SLACK_BOT_TOKEN!!,
signingSecret: process.env.SLACK_SIGNING_SECRET!!
});
middleware.enableAll(app);
// -----------------------------
// shortcut
// -----------------------------
app.shortcut("deepl-translation", async ({ ack, body, client }) => {
await ack();
await runner.openModal(client, body.trigger_id);
});
app.view("run-translation", async ({ ack, client, body }) => {
const text = body.view.state.values.text.a.value!;
const lang = body.view.state.values.lang.a.selected_option!.value;
await ack({
response_action: "update",
view: runner.buildLoadingView(lang, text)
});
const translatedText: string | null = await deepL.translate(text, lang);
await client.views.update({
view_id: body.view.id,
view: runner.buildResultView(lang, text, translatedText || ":x: Failed to translate it for some reason")
});
});
app.view("new-runner", async ({ body, ack }) => {
await ack({
response_action: "update",
view: runner.buildNewModal(body.view.private_metadata)
})
})
// -----------------------------
// reacjilator
// -----------------------------
import { ReactionAddedEvent } from './types/reaction-added';
app.event("reaction_added", async ({ body, client }) => {
const event = body.event as ReactionAddedEvent;
if (event.item['type'] !== 'message') {
return;
}
const channelId = event.item['channel'];
const messageTs = event.item['ts'];
if (!channelId || !messageTs) {
return;
}
const lang = reacjilator.lang(event);
if (!lang) {
return;
}
const replies = await reacjilator.repliesInThread(client, channelId, messageTs);
if (replies.messages && replies.messages.length > 0) {
const message = replies.messages[0];
if (message.text) {
const translatedText = await deepL.translate(message.text, lang);
if (translatedText == null) {
return;
}
if (reacjilator.isAlreadyPosted(replies, translatedText)) {
return;
}
await reacjilator.sayInThread(client, channelId, translatedText, message);
}
}
});
// -----------------------------
// starting the app
// -----------------------------
(async () => {
await app.start(Number(process.env.PORT) || 3000);
console.log('⚡️ Bolt app is running!');
})();