Last active
November 26, 2018 19:35
-
-
Save fanatid/14986bdaea6d4e5bb8f9dbf81d848513 to your computer and use it in GitHub Desktop.
Slack private messages history export
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const msgs = require('./history.json') | |
const data = {} | |
const usersObj = {} | |
for (const msg of msgs) { | |
const msgTs = new Date(parseFloat(msg.ts) * 1000) | |
const ts = new Date(msgTs.getFullYear(), msgTs.getMonth(), msgTs.getDate()) | |
const key = `${ts.getFullYear()}/${ts.getMonth()+1}/${ts.getDate()}` | |
if (!data[key]) data[key] = { key, ts } | |
usersObj[msg.user] = true | |
if (!data[key][msg.user]) data[key][msg.user] = 1 | |
else data[key][msg.user] += 1 | |
} | |
const users = Object.keys(usersObj) | |
process.stdout.write('date') | |
for (const user of users) process.stdout.write(';' + user) | |
process.stdout.write('\n') | |
for (const item of Object.values(data).sort((a, b) => a.valueOf() < b.valueOf())) { | |
process.stdout.write(item.key) | |
for (const user of users) process.stdout.write(';' + (item[user] || 0)) | |
process.stdout.write('\n') | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const fs = require('fs') | |
const fetch = require('node-fetch') | |
const token = 'xoxp-...' | |
const channel = '...' | |
async function getHistory (oldest) { | |
const count = 1000 | |
const url = `https://slack.com/api/conversations.history?token=${token}&channel=${channel}&count=${count}&oldest=${oldest}` | |
const res = await fetch(url) | |
return res.json() | |
} | |
;(async () => { | |
const history = [] | |
let oldest = 1 | |
while (true) { | |
const data = await getHistory(oldest) | |
const fromTs = new Date(parseFloat(data.messages[data.messages.length - 1].ts) * 1000) | |
const toTs = new Date(parseFloat(data.messages[0].ts) * 1000) | |
console.log(`received ${data.messages.length} messages, ${fromTs} => ${toTs}`) | |
oldest = data.messages[0].ts | |
history.push(...data.messages.reverse()) | |
if (!data.has_more) break | |
await new Promise((resolve) => setTimeout(resolve, 1500)) | |
} | |
fs.writeFileSync('./history.json', JSON.stringify(history)) | |
})().catch((err) => { | |
console.log(err.stack || err) | |
process.exit(1) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment