Last active
November 17, 2017 08:36
-
-
Save bzar/4d2f137e25d810bc088ca7f96ba2668e to your computer and use it in GitHub Desktop.
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 token = process.env.SLACK_TOKEN | |
const Slack = require('slack') | |
const bot = new Slack({token}) | |
const CHANNEL = process.argv[2]; | |
const SINCE = process.argv[3]; | |
const UNTIL = process.argv[4]; | |
function toSlackTime(t) { | |
return new Date(t).getTime() / 1000.0; | |
} | |
async function getConversation(channelId) { | |
let more = true; | |
let messages = []; | |
let since = toSlackTime(SINCE); | |
let until = toSlackTime(UNTIL); | |
while(more) { | |
const response = await bot.conversations.history({ | |
channel: channelId, | |
oldest: since, | |
latest: until, | |
}); | |
messages = messages.concat(response.messages); | |
more = response.has_more; | |
until = messages[messages.length - 1].ts; | |
} | |
return messages; | |
} | |
(async function main() { | |
const users = (await bot.users.list({})) | |
.members | |
.reduce((us, u) => { us[u.id] = u.real_name; return us }, {}); | |
const channels = (await bot.channels.list({exclude_archived: false})) | |
.channels | |
.filter(c => !c.is_private && (!CHANNEL || c.name == CHANNEL)); | |
const conversations = channels.map( | |
c => ({name: c.name, messages: getConversation(c.id)})); | |
conversations.map(({name, messages}) => { | |
messages.then(messages => { | |
messages | |
.filter(m => m.type == 'message') | |
.reverse() | |
.map(m => { | |
const time = new Date(parseFloat(m.ts) * 1000) | |
.toLocaleTimeString('en-US', {hour12: false}) | |
if(m.reactions) { | |
const reactions = m.reactions | |
.map(r => `:${r.name}:(${r.users.map(u => users[u]).join(', ')})`) | |
.join(', '); | |
console.log(`[${time}] ${users[m.user]}: ${m.text} [${reactions}]`); | |
} else { | |
console.log(`[${time}] ${users[m.user]}: ${m.text}`); | |
} | |
}); | |
}); | |
}); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment