-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
executable file
·64 lines (57 loc) · 2.17 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
const axios = require('axios');
const franc = require('franc');
const striptags = require('striptags');
const translate = require('google-translate-api');
const RtmClient = require('@slack/client').RtmClient;
const RTM_EVENTS = require('@slack/client').RTM_EVENTS;
const user_token = process.env.USER_TOKEN;
const bot_token = process.env.BOT_API_KEY;
const rtm = new RtmClient(bot_token);
rtm.on(RTM_EVENTS.MESSAGE, async (data) => {
const { bot_id, channel, thread_ts, user, text } = data;
if (!bot_id) {
const request_url = `https://slack.com/api/users.info?token=${user_token}&user=${user}`;
const { is_bot, real_name } = (await axios.get(request_url)).data.user;
if ((channel[0] === 'C' || channel[0] === 'U' || channel[0] === 'G') && !data.hasOwnProperty('subtype')) {
if (!is_bot && isAbleToReply(text)) {
const isAbleToReplyBackInEnglish = text.length > 20;
const translatedReply = await translateReply(text, real_name);
if (isAbleToReplyBackInEnglish) {
sendMessage(thread_ts, translatedReply, channel);
}
}
}
}
});
rtm.start();
function isAbleToReply(text) {
const isFinnish = franc(text) === 'fin';
const simpleFinnishTerms = ['kiitos', 'kiitii', 'moi', 'hyvää', 'onnea', 'hei'];
let notSimpleFinnish = true;
simpleFinnishTerms.forEach((term) => {
if (text.toLowerCase().includes(term)) {
return notSimpleFinnish = false;
}
});
return isFinnish && (notSimpleFinnish || text.length > 20) && notOnlyHttpLink(text);
}
function notOnlyHttpLink(text) {
return !(text.includes('http') && text.split(' ').length === 1);
}
function sendMessage(thread_ts, text, channel) {
if (thread_ts) {
rtm.send({
text,
channel,
thread_ts,
type: RTM_EVENTS.MESSAGE,
});
}
else {
rtm.sendMessage(text, channel);
}
};
async function translateReply(text, userFirstName) {
const translatedTextInEnglish = (await translate(striptags(text), {to: 'en'})).text;
return `${userFirstName} said "${translatedTextInEnglish}"`;
}