Created
September 18, 2021 19:58
-
-
Save ahallora/b40684c8d705147bf8a790c44b82a22f to your computer and use it in GitHub Desktop.
Slack API: Get User details based on 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
require('dotenv').config(); | |
const { App } = require('@slack/bolt'); | |
const reaction = process.env.EMOJI; | |
const url = process.env.URL; | |
const app = new App({ | |
signingSecret: process.env.SLACK_SIGNING_SECRET, | |
token: process.env.SLACK_BOT_TOKEN, | |
}); | |
const findChannelIdAndTimestamp = (url) => { | |
const [channel, timestamp] = url.split('/').pop().split('-'); | |
return { channel, timestamp }; | |
}; | |
const { channel, timestamp } = findChannelIdAndTimestamp(url); | |
if (!url.includes('/thread') || !channel || !timestamp) { | |
throw Error('Unable to parse Slack Thread URL.'); | |
} | |
const getReactions = async (channel, timestamp) => { | |
const convos = await app.client.conversations.info({ | |
channel, | |
}); | |
const { | |
channel: { name, is_channel, is_member }, | |
} = convos; | |
if (!is_channel) { | |
throw Error('Please provide a valid URL to a Slack Channel'); | |
} | |
if (!is_member) { | |
await app.client.conversations.join({ | |
channel, | |
}); | |
} | |
const { | |
message: { text, reactions }, | |
} = await app.client.reactions.get({ | |
channel, | |
timestamp, | |
full: true, | |
}); | |
const reactionUsers = reactions.find((r) => r.name === reaction)?.users; | |
var users = await Promise.all( | |
reactionUsers.map(async (user) => { | |
const { | |
user: { | |
name, | |
real_name, | |
profile: { email }, | |
}, | |
} = await app.client.users.info({ | |
user, | |
}); | |
return { | |
name, | |
real_name, | |
email, | |
}; | |
}) | |
); | |
const output = { channel: name, message: text, reaction, users }; | |
console.log(` | |
Users reacted with :${output.reaction}: are: | |
${output.users.map((user) => `${user.real_name};${user.email}`).join('\n')} | |
`); | |
}; | |
(async () => { | |
await app.start(process.env.PORT || 3000); | |
console.log('⚡️ Bolted. Looking for ' + channel); | |
await getReactions(channel, timestamp); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
FYI I believe this API call will be limited to a max of 50 users.It’s not 🎉