I'm building a slack FAQ app that uses message reactions to gather the best answers to questions. My plan is to save any Slack messages with positive reactions by using the reaction_added
event to get the TS attribute and then the conversations.history
method to get the message's content.
This works well for parent-level or non-threaded messages, however it doesn't work for reply messages inside threads. For some reason the conversations.history
method returns an unrelated message when using the TS of a thread reply.
I've checked the Slack API conversations.history
method documentation to see if replies are handled in any special way. I reviewed conversations.replies
method to see if it might be helpful, but since reaction_added
event simply provides a TS id for the message and no thread_ts value that can be used with the conversations.replies
method.
I'm using bolt framework. Here's a snippet of the code that tries to use the reaction_added
event with conversations.history
method to get the message content:
app.event('reaction_added', async ({ event, context, say }) => {
try {
const result = await app.client.conversations.history({
token: process.env.SLACK_USER_TOKEN,
channel: event.item.channel,
latest: event.item.ts,
limit: 1,
inclusive: true
});
save(`${result.messages[0].text}`);
}
catch (error) {
console.error(error);
}
});
Expected result: Message contents of thread reply that a reaction is posted for
Actual result: Message contents of the latest message in the slack channel
Threads and replies provide your users with a way to go into more detail about a specific topic. This can be very helpful to keep the conversation organized and reduce noise. To create a thread you simply send a message with a parent_id. Have a look at the example below:
Awaiting reactions A common use case for reactions in commands is having a user confirm or deny an action or creating a poll system. Luckily, we actually already have a guide page covering this! Check out that page if you want a more in-depth explanation.
We’re adding Reactions to conversations today to help people express their feelings more simply and effectively. While people have been able to include emoji in responses for a long time, using them as reactions resulted in a lot of noise.
Here, e is a synthetic event. React defines these synthetic events according to the W3C spec, so you don’t need to worry about cross-browser compatibility. React events do not work exactly the same as native events. See the SyntheticEvent reference guide to learn more.
I'm not sure if it changed recently or I misread the documentation, but conversations.replies
API endpoint does not require a thread_ts
value containing the parent thread timestamp in order to retrieve a thread reply.
The event.item.ts
value provided by the reaction_added
event is sufficient to retrieve the message contents of the reply where a reaction was added.
So to get the message contents of a message where a reaction was added, you can update the code in my original question to:
app.event('reaction_added', async ({ event, context, say }) => {
try {
const result = await app.client.conversations.replies({
token: process.env.SLACK_USER_TOKEN,
channel: event.item.channel,
ts: event.item.ts
});
save(`${result.messages[0].text}`);
}
catch (error) {
console.error(error);
}
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With