Skip to content

Instantly share code, notes, and snippets.

@ahallora
Created September 18, 2021 19:58
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • 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);
})();
@ahallora
Copy link
Author

How to use

Setup OAuth Scope

Set up a slack app with the following OAuth Scope:

  • channels:join
  • channels:read
  • chat:write
  • users:read
  • users:read.email

Get channel and timestamp

  • Get the Bot User OAuth Token from OAuth & Permissions and Signing Secret from App Credentials.
  • Find the thread in slack and copy the URL (ensure the URL includes /threads).
  • Type the EMOJI you want to get reaction user details for.

Fill in the .env with the information:

SLACK_SIGNING_SECRET=123abc...
SLACK_BOT_TOKEN=xoxb-...
URL=https://app.slack.com/client/T124ABC32/C12ABCDEF/thread/C12ABCDEF-1631693056.014500
EMOJI=italian-chef-gusto

Run in console

  • Run npm init y && npm install @slack/bolt dotenv
  • Run node index.js

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