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 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
How to use
Setup OAuth Scope
Set up a slack app with the following OAuth Scope:
Get channel and timestamp
Bot User OAuth Token
from OAuth & Permissions andSigning Secret
from App Credentials./threads
).EMOJI
you want to get reaction user details for.Fill in the
.env
with the information:Run in console
npm init y && npm install @slack/bolt dotenv
node index.js