How to count reactions in Slack

  • 11 February 2022
  • 6 replies
  • 3688 views
How to count reactions in Slack
Userlevel 7
Badge +11
  • Community Manager
  • 4442 replies

Hey friends! I stumbled across a question asked in Community recently which got me thinking about whether there was better way to get a count of the number of reactions a Slack message has had:

This question has been asked before, e.g. here:
https://community.zapier.com/general-questions-3/trigger-zap-based-on-number-of-reactions-2324
The proposed solutions are cumbersome, though.

I wondered if it would be possible to let the zap add a `:check:` reaction as a latch, and then check for the existence of that reaction. However I couldn't see how to check for the existence of a reaction. Does anyone know of a way?  

 

The Challenge

The Slack integration isn’t currently able to give us a count of the total number of reactions a message has had. We could keep a record of each reaction as it’s added by storing this information into a spreadsheet or database app of some kind and then do some calculations to get the current total, but that not really ideal. We need to find a more efficient way to see how many reactions a Slack message has had.

A Solution

Use a Webhooks by Zapier step to connect to Slack’s API and get a count of the number of reactions a message has had. If you’re thinking to yourself “Webhooks? Argh, this is going to be complicated :rolling_eyes: ”, it’s ok. Webhooks are honestly not as scary as they may seem. Plus, I’m going to share plenty of screenshots along the way to help break all the necessary steps down. You've got this! :muscle:

In Slack

First, we’re going to need to create an App in Slack. I know, I know it sounds like it’s going to be a lot of work but we’re just going to be doing the bare minimum here to get this workaround up and running. I promise, it’s not as scary or time-consuming as it may seem.

Create your App

Head over to the Your Apps page in Slack and click on the Create New App button:

9739b67afa94dbd98d7258c36063fb42.png

This will open up a pop up window where you can select how you want to create your app (From scratch or From an app manifest). In this example I’m just going to go with the From scratch option:

68006ec6ede01c5a5e6533fc6842565e.png
Next, enter a name for your app in the App Name field. And select the Slack workspace you want it added to:

66c213bdbbe927ab75a9c1f7c4dcd166.png
Not too difficult so far, right? Now let’s head to the OAuth & Permissions page to set some permissions for our app. After you enter create the app, you’ll be presented with a couple of different ways to get to that page:

04d6fad041ffa066679b784eff21f87b.png

Clicking either of the highlighted links (shown in the image above) will take you to the OAuth & Permissions page.

Select your OAuth scope

Once you’ve got to the OAuth & Permissions page, scroll down to the Scopes section click on Add an OAuth Scope button: 

05ecc546d53e0e903d8192b27213a457.png

This will add a dropdown menu where you can select the permission scope for the app. As we only need to read the reactions that are added we’ll want to select the reactions:read option:

c853a6e1d4da538bfa078e90822d0e44.png

Great, we’re nearly there now. Let’s scroll back up the OAuth & Permissions page and click the Install to Workspace button at the top:

22beebddef04dc9815f29a8de87ccb8e.png
You’ll then see a screen asking for permission to access your workspace. Click Allow to get the OAuth Token added to your Slack Workspace.

146a08f121a7649f38b7d837700403a8.png

Copy the OAuth token

At the top of the OAuth & Permissions page in Slack you’ll now see that there’s a User OAuth Token available to copy:

20881e38578b02f9f6afd69f73977d7a.png
Woohoo! :tada: Keep this token somewhere safe for now, we’re going to need this later when we’re setting up the webhook step. 

In Zapier

Phew! Now that our Slack App is set up, let’s head on over to Zapier and set up the Zap.

Set up your Slack Trigger 

We're going use the New Reaction Added trigger for this Zap. And set it up to trigger whenever anyone uses a  👀 reaction (reactji) on a message in any public channel:

4f623d444204f3b7a6f780b9c0885d6a.png

Once you’ve got your trigger set up to check for whatever reaction is appropriate for your use case it’s time to tackle the Webhook step.

Set up your Webhook Action

For this to work we’re going to need to set up a Webhooks by Zapier action step using the GET action Event option:

e568192814a7dd7b601d59225a0993f8.png

Next, in the URL field enter https://slack.com/api/reactions.get (this is the Slack API endpoint that we’ll send our GET request to in order to get information about the reactions that have been added to the message).

Under that in the Query String Params section you’ll want to set up two params one for the channel and one for the timestamp.

For the channel select the Message Channel ID field from our trigger step (step 1).

For the the timestamp, select the Message Ts field. This will help to narrow down our search to find the exact message that triggered the Zap. The set up should look something like this:

12d07d2754234702c2cadf54580434a8.png
We’re almost done with this, we just need to scroll down and add two key and value pairs into the Headers section. One for the Authorization and one for the Content-Type. 

For the Authorization key you’ll enter a value of Bearer and then paste in the OAuth token we obtained from Slack earlier. Oh, and make sure to leave a space inbetween the word Bearer and the token itself, or it won’t work!

Then for the Content-Type key give it a value of application/x-www-form-urlencoded.

In the end, we should end up with something that looks like this 

9e6d76c84620148e7063825c0fd81f95.png

Let’s test it out to check it worked:

c992786ba0110a123c02284659f212e6.png
Yay, it worked! Hmm, but what should we do with this number now that we have it?

Use a Filter

From here you could user a Filter step to check whether the Count field is a specific value. Say we only want to let the Zap run for the first reaction received, we’d set up a Filter step to continue if the value in the Message Reactions Count is less than 2:

461bf69567da4df232e66f573e780467.png
 

Of course, that’s not the only Filter set up you could use. Let’s say you wanted to send an email or perform some other kind of action once a message gets a certain number of reactions, say 20. That’s totally possible too! We’d just set the Filter to check that the Message Reactions Count field from the Webhook step is 20:

4f387d846881a975c3b60f80a65aea50.png
These are just a couple of examples to get you started. Hopefully it’ll inspire you to come up with other inventive ways to check reaction counts in your Zaps! 

Further Reading

Well that’s all from me for now. Happy Zapping! :zap:


6 replies

Userlevel 7
Badge +11

Ah I see, thanks for clarifying that @Jean. I can certainly see how that makes things a bit trickier to deal with! Thanks again for sharing the workaround you came up with for dealing with that. And I’m so glad to hear those edge cases won’t be a major blocker for you! :relaxed:

Userlevel 2

Is it the reactions count that you’re seeing appear as a comma-separated string or a different field?

I’m seeing two comma-separated strings: names (like “muscle,feedback”) and counts (like “2,1”). I then have to split on comma, and index to match them up. It would be nice if the input showed up as a nested dictionary, so that I could do e.g. input.get(“reactions”, {}).get(“feedback”, {}).get(“count”).

Regarding edge cases, I think we can live with that :slight_smile:

 

 

 

Userlevel 7
Badge +11

Thanks for reaching out and sharing that helpful code here, @Jean:relaxed:

Is it the reactions count that you’re seeing appear as a comma-separated string or a different field? If so, it looks like the reactions count data is sent over as in array of line items rather than a comma-separated string. Often when selecting a field containing line items values this can sometimes look like the values are comma-separated. Do you think that could be the case here?

For the edge cases:
1) The Zap should trigger every time a reaction is added, but the Filter step would prevent any further actions, beyond the Filter step, from running (depending on how you’ve set up your Filter that is).

2) I’ve not tested it myself but I would have thought that if an emoji reaction is removed then the count would then be set to zero (but the Zap would not be triggered when the reaction is removed). Then, if someone adds the same emoji reaction it will re-trigger the Zap then the count would be set to one again. If this is a likely to be a common occurrence then it might make more sense to add a record of the message that received the initial reaction in a database/spreadsheet like app (like Airtable or Google Sheets for example) and then use a search action to look for it to see if it was previously triggered on. 

I hope that helps! 

Userlevel 2

I’m curious about some other edge cases as well:

  • If I add an emoji, remove it, and then add it again, the count will be 1 again. I think Zapier only triggers on the first time I add the emoji, is that correct?
  • Or f I add an emoji, remove it, and then someone else adds it, the count will be 1 again.
Userlevel 2

I believe this runs into problems if there are multiple different reactions. I had to add a Python step like this:

reactions = input.get('webhook_reaction_names').split(',')
counts = input.get('webhook_reaction_counts').split(',')
if 'myreaction' in reactions:
idx = reactions.index('myreaction')
count_myreaction = counts[idx]
if count_myreaction == '1':
output = {'continue': True}
return output
output = {'continue': False}
return output

Am I missing something about the Filter step? Ideally I’d like to select and test a specific key in the input, but Zapier flattens the input into a comma-separated string.

Userlevel 1
Badge

Thank you, this was a huge help!

Reply