-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
84 lines (66 loc) · 2.51 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
require('dotenv').config()
const { createEventAdapter } = require('@slack/events-api')
const { WebClient } = require('@slack/web-api')
const assert = require('assert')
const axios = require('axios')
const uuid = require('uuid/v4')
const port = process.env.PORT || 3000
const token = process.env.SLACK_TOKEN
const PTTAI_GATEWAY_URL = process.env.PTTAI_GATEWAY_URL
const PTTAI_TOKEN = process.env.PTTAI_TOKEN
const SLACK_SIGNING_SECRET = process.env.SLACK_SIGNING_SECRET
assert.ok(port, 'PORT is undefined')
assert.ok(token, 'SLACK_TOKEN is undefined')
assert.ok(PTTAI_GATEWAY_URL, 'PTTAI_GATEWAY is undefined')
assert.ok(PTTAI_TOKEN, 'PTTAI_TOKEN is undefiend')
assert.ok(SLACK_SIGNING_SECRET, 'SLACK_SIGNING_SECRET is undefined')
const userMentions = /<@([^<]+)>/g
const mentionedUserID = /<@([^<]+)>/
async function main () {
const web = new WebClient(token)
let channels = await loadChannels(web)
console.log(channels)
let users = await loadUsers(web)
const slackEvents = createEventAdapter(SLACK_SIGNING_SECRET)
slackEvents.on('message', async (event) => {
console.log(event)
let text = event.text
if (users[event.user]) {
// replace mentioned user ID to user name
if (text.match(userMentions)) {
for (let mention of text.match(userMentions)) {
let userID = mention.match(mentionedUserID)[1]
if (userID) {
text = text.replace(userID, users[userID])
}
}
}
console.log(`Received a message event: user ${users[event.user]} in channel ${channels[event.channel]} says ${text} with ${event.files ? event.files.length : 0} files`)
let fileURLs = event.files ? event.files.map(f => f.url_private) : []
await post(channels[event.channel], `<${users[event.user]}>: ${text} ${fileURLs.join(' ')}`)
}
})
slackEvents.on('error', (err) => {
if (err instanceof TypeError) return
console.error(err)
})
slackEvents.start(port).then(() => {
console.log(`server listening on port ${port}`)
})
}
async function loadUsers (web) {
let t = {}
let res = await web.users.list()
res.members.forEach(x => { t[x.id] = x.name })
return t
}
async function loadChannels (web) {
let t = {}
let res = await web.channels.list()
res.channels.forEach(x => { t[x.id] = x.name })
return t
}
async function post (topic, text) {
return axios.request({ url: `${PTTAI_GATEWAY_URL}/topics/${topic}`, data: { data: { id: uuid(), message: { type: 'text', value: text } } }, method: 'POST', params: { token: PTTAI_TOKEN } })
}
main()