forked from UniversityOfHelsinkiCS/toska-silta
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshared.js
58 lines (49 loc) · 1.92 KB
/
shared.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
const axios = require('axios')
const SLACK_BOT_TOKEN = process.env.SLACK_BOT_TOKEN || ''
const removeShitFromSlackMessage = async (message) => {
let parsedMessage = message
const username_regex = /<@[^>]+>/g
const userIdStrings = parsedMessage.match(username_regex)
if (userIdStrings !== null) {
const userIdStringToUsernameMap = {}
await Promise.all(userIdStrings.map(async idString => {
if (!(idString in userIdStringToUsernameMap)) {
const id = idString.slice(2, -1)
const user = await getInfoForSlackUser(id)
userIdStringToUsernameMap[idString] = user.username
}
}))
parsedMessage = parsedMessage.replaceAll(username_regex, (match) => `@${userIdStringToUsernameMap[match]}`)
}
const embed_url_regex = /<[^>|]+\.[^>|]+\|([^>]+\.[^>]+)>/g
parsedMessage = parsedMessage.replaceAll(embed_url_regex, '$1')
const decodeHtmlCharCodes = str =>
str.replaceAll(/(&#(\d+);)/g, (match, capture, charCode) =>
String.fromCharCode(charCode));
parsedMessage = decodeHtmlCharCodes(parsedMessage)
return parsedMessage
}
const userMap = {}
const getInfoForSlackUser = async (user) => {
if (userMap[user]) return userMap[user]
const { data } = await axios.get(`https://slack.com/api/users.info?token=${SLACK_BOT_TOKEN}&user=${user}`)
const info = {
username: data.user.profile.display_name || data.user.real_name || data.user.name,
avatar_url: data.user.profile.image_72,
}
userMap[user] = info
return info
}
const channelMap = {}
const getNameForSlackChannel = async (channelId) => {
if (channelMap[channelId]) return channelMap[channelId]
const { data } = await axios.get(`https://slack.com/api/conversations.info?token=${SLACK_BOT_TOKEN}&channel=${channelId}`)
const channelName = data.channel.name
channelMap[channelId] = channelName
return channelName
}
module.exports = {
removeShitFromSlackMessage,
getInfoForSlackUser,
getNameForSlackChannel
}