-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
executable file
·146 lines (126 loc) · 4.04 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
#!/usr/bin/env node
require("dotenv").config("./.env");
const translate = require("./translate.js");
const detectLang = require("./detectLang.js");
const { App } = require("@slack/bolt");
var fromToTable = [
{ from: "general", to: "general-autotranslate" },
{ from: "test-auto-translate", to: "test-auto-translate-2" },
{ from: "random", to: "random-autotranslate" },
];
const app = new App({
token: process.env.BOT_TOKEN,
appToken: process.env.SLACK_APP_TOKEN,
socketMode: true,
});
(async () => {
await app.start();
console.log("⚡️ Bolt app started");
})();
app.message(async ({ message, client }) => {
console.log(`Message received`);
var msgKeys = Object.keys(message);
var skip = false;
if (msgKeys.includes("previous_message")) {
skip = true;
console.log(`Message format not recognized, probably an edited message`);
}
// console.log(`Message received: ${message.text}`);
if (!skip) {
// get id of post channel
try {
// get channel list
var chanList = await client.conversations.list({});
// get id of channels of interest
for (let i = 0; i < fromToTable.length; i++) {
let fromChan = chanList.channels.find(function (chan, index) {
if (chan.name == fromToTable[i].from) return true;
});
if (typeof fromChan !== "undefined") {
fromToTable[i].fromId = fromChan.id;
} else {
fromToTable[i].fromId = "";
}
let toChan = chanList.channels.find(function (chan, index) {
if (chan.name == fromToTable[i].to) return true;
});
if (typeof toChan !== "undefined") {
fromToTable[i].toId = toChan.id;
} else {
fromToTable[i].toId = "";
}
}
} catch (error) {
console.error(error);
var chanList = "Unidentified";
}
console.log(fromToTable);
// check if message come from channel of interest
var chanOfInterest = fromToTable.find(function (chan, index) {
if (chan.fromId == message.channel) return true;
});
}
if (typeof chanOfInterest !== "undefined" && !skip) {
console.log(`Message is:\n ${JSON.stringify(message)}`);
// get sender information:
try {
// Call the users.info method using the WebClient
// console.log("get sender information:");
var fromUser = await client.users.info({
user: message.user,
});
var fromName = fromUser.user.display_name;
} catch (error) {
console.error(error);
var fromName = "Unidentified User";
}
// Detect language:
try {
// console.log("translate message");
var fromLanguage = detectLang(
(text = message.text),
(japPropThresh = 0.1),
(verbose = true)
);
} catch (error) {
console.error(error);
var fromLanguage = "language detection error";
}
// translate message:
if (fromLanguage === "JA") {
var toLang = "EN";
var postmsg = ` ---- DeepL API translation, from Japanese to English ----\n`;
} else {
var toLang = "JA";
var postmsg = ` ---- DeepL API translation, from English to Japanese ----\n`;
}
try {
// console.log("translate message");
var resp = await translate(
(fromLang = fromLanguage),
(toLang = toLang),
(msg = message.text)
);
console.log(`translation is:\n ${JSON.stringify(resp)}`);
} catch (error) {
console.error(error);
var resp = { transMsg: "Error during translation" };
}
// post message:
postmsg = postmsg.concat(`${resp.transMsg}`);
// detect attatchment:
if (message.attachments) {
postmsg = postmsg.concat(`> --- attachement not translated sorry ---`);
}
let result = await client.chat.postMessage({
text: postmsg,
channel: chanOfInterest.toId,
// as_user: false,
username: `AutoTranslate: ${fromUser.user.profile.display_name}`,
icon_url: fromUser.user.profile.image_48,
});
// await say(resp.transMsg);
} else {
console.log("Not tracked channel, or skipped");
}
});