-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
58 lines (52 loc) · 1.9 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
const fs = require('fs')
const got = require('got')
const WebSocket = require('ws')
const token = require('./token').bot
const userBlackList = [
'UHDQR2VT3',
'UHJ5JVBPX'
]
;
(async () => {
// https://api.slack.com/methods/conversations.list
let rawChannels = JSON.parse((await got(`https://slack.com/api/conversations.list?token=${token}`)).body).channels
let channels = {}
for (let i = 0; i < rawChannels.length; i++) {
channels[rawChannels[i].id] = rawChannels[i].name
}
// https://api.slack.com/methods/rtm.connect/
let url = JSON.parse((await got(`https://slack.com/api/rtm.connect?token=${token}`)).body).url
console.log(`URL: ${url}`)
const client = new WebSocket(url);
// https://api.slack.com/rtm
client.on('open', () => {
console.log('wss Connected')
})
client.on('message', async data => {
let object = JSON.parse(data)
if (!userBlackList.includes(object.user)) {
if (object.type == 'hello') {
console.log('Hello from Slack')
}
if (object.type == 'message' && object.text) {
let date = new Date()
let text = object.text
let channel = channels[object.channel] || object.channel
console.log(`${channel}: ${text}`)
fs.appendFileSync(`./records/${date.getFullYear()}-${date.getMonth()+1}-${date.getDate()}-${channel}.txt`, text + '\n')
}
if (object.type == 'channel_created' || object.type == 'channel_deleted' || object.type == 'channel_rename') {
let updateChannels = JSON.parse((await got(`https://slack.com/api/conversations.list?token=${token}`)).body).channels
for (let i = 0; i < updateChannels.length; i++) {
channels[updateChannels[i].id] = updateChannels[i].name
}
console.log('updateChannels')
}
}
})
client.on('close', () => {
console.log('wss Closed')
})
})()
setTimeout(process.exit, 1000 * 60 * 60 * 18)
// auto restart every 18 hours