Created
January 7, 2020 22:11
Quick script to extract all messages in a Slack channel and group by total reactions
This file contains hidden or 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 { WebClient } = require('@slack/web-api'); | |
async function asyncForEach(array, callback) { | |
for (let index = 0; index < array.length; index++) { | |
await callback(array[index], index, array); | |
} | |
} | |
// An access token (from your Slack app or custom integration - xoxp, xoxb) | |
const token = process.env.SLACK_TOKEN; | |
if(! token) { | |
console.log("missing $SLACK_TOKEN env var api token"); | |
return; | |
} | |
const web = new WebClient(token); | |
const conversationId = 'CG4C97BC0'; // #performance-wins channel | |
const messages = []; | |
let message_count = 0; | |
(async () => { | |
let finished = false; | |
let next_cursor = ""; | |
while(! finished) { | |
let res = await web.conversations.history({ channel: conversationId, cursor: next_cursor }); | |
res.messages.forEach(msg => { | |
if(msg.subtype && (msg.subtype == "channel_leave" || msg.subtype == "channel_join")) | |
return; | |
message_count++; | |
let reactions = msg.reactions ? | |
msg.reactions.reduce( | |
(acc, current) => acc + current.count, 0 | |
) : | |
0; | |
if(reactions > 0) { | |
messages.push( | |
{ | |
reaction_count: reactions, | |
short_message: msg.text.substring(0, 80).replace(/(\r\n|\n|\r|,|\t)/gm, ""), | |
message: msg.text.replace(/(\r\n|\n|\r|,|\t)/gm, ""), | |
user: msg.user | |
} | |
) | |
} | |
}); | |
finished = ! res.has_more; | |
next_cursor = res.response_metadata.next_cursor; | |
} | |
asyncForEach(messages, async (msg) => { | |
let userInfo = await web.users.info({ user: msg.user }); | |
console.log(msg.reaction_count + ", " + userInfo.user.name + ", " + userInfo.user.real_name + ", " + msg.message); | |
}); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment