Skip to content

Instantly share code, notes, and snippets.

@ahallora
Created September 18, 2021 19:58
Show Gist options
  • Save ahallora/b40684c8d705147bf8a790c44b82a22f to your computer and use it in GitHub Desktop.
Save ahallora/b40684c8d705147bf8a790c44b82a22f to your computer and use it in GitHub Desktop.
Slack API: Get User details based on reactions
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);
})();
@mrmanc
Copy link

mrmanc commented Oct 25, 2024

FYI I believe this API call will be limited to a max of 50 users.

It’s not 🎉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment