Last active
September 18, 2021 19:59
-
-
Save afeld/ac56da55274ff4803498 to your computer and use it in GitHub Desktop.
get emoji reactions for a message in Slack – unfortunately their API "might not always contain all users that have reacted". limited to 50 as of 2/12/16.
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
'use strict'; | |
const Promise = require('bluebird'); | |
const WebClient = require('slack-client').WebClient; | |
const token = process.env.SLACK_API_TOKEN; | |
if (!token) { | |
throw "Please set SLACK_API_TOKEN"; | |
} | |
const client = new WebClient(token, {logLevel: 'debug'}); | |
const listChannels = Promise.promisify(client.channels.list, {context: client.channels}); | |
const getReactions = Promise.promisify(client.reactions.get, {context: client.reactions}); | |
const listUsers = Promise.promisify(client.users.list, {context: client.users}); | |
// returns a Promise for the channel object | |
const getChannel = (name) => { | |
return listChannels(true).then((channels) => { | |
return channels.channels.find((channel) => { | |
return channel.name === 'news'; | |
}); | |
}); | |
}; | |
// returns a Promise for the list of reaction objects | |
const getReactionsFor = (timestamp, channelId) => { | |
const opts = { | |
channel: channelId, | |
timestamp: timestamp | |
}; | |
return getReactions(opts).then((data) => { | |
return data.message.reactions; | |
}); | |
}; | |
const getUsersById = () => { | |
return listUsers(false).then((data) => { | |
let usersById = {}; | |
data.members.forEach((user) => { | |
usersById[user.id] = user; | |
}); | |
return usersById; | |
}); | |
}; | |
const printReactions = (timestamp, channelId) => { | |
Promise.all([ | |
getReactionsFor(timestamp, channelId), | |
getUsersById() | |
]).then((results) => { | |
const reactions = results[0]; | |
const usersById = results[1]; | |
reactions.forEach((reaction) => { | |
console.log(`${reaction.name}:`); | |
reaction.users.forEach((userId) => { | |
const user = usersById[userId]; | |
console.log(` ${user.profile.email}`); | |
}); | |
}); | |
}); | |
}; | |
getChannel('news').then((channel) => { | |
printReactions('1454530550.000057', channel.id); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Here's a rewrite with the latest Slack API for anyone curious:
https://gist.github.com/ahallora/b40684c8d705147bf8a790c44b82a22f