Add and Remove Reactions

Learn to use Slack's API calls to add and remove reactions.

Reactions are a pictorial way to interact with messages, but our application can also use them. For example, we can make a Slack application that counts reactions and responds accordingly, like a polling application.

Let’s look at the following endpoints in this lesson:

  1. reactions.add: This endpoint adds a reaction to an item.
  2. reactions.remove: This endpoint removes a reaction from an item.

Add a reaction

To add a reaction, we access the https://slack.com/api/reactions.add endpoint.

Request parameters

Some important query parameters for the reactions.add endpoint are as follows:

Parameter

Type

Category

Description

token

token

required

Authentication tokens carry the required scopes that govern the usage of different Slack applications and APIs. We usually pass these tokens as an HTTP Authorization header or as a POST parameter.

channel

string

required

This is channel where the message to add reaction to was posted.

name

string

required

This is the reaction's (the emoji's) name.

timestamp

string

required

This is the timestamp of the message that we want to add a reaction to.

Let’s call the reactions.add endpoint. Click the “Run” button to send a “me” message to the channel and add a reaction to the message.

Press + to interact
import fetch from "node-fetch";
const url = new URL("https://slack.com/api/reactions.add");
const headerParameters = {
Authorization: "Bearer {{TOKEN}}",
"Content-Type": "application/json; charset=UTF-8",
};
async function addReaction(reaction, timestamp) {
try {
const bodyParameters = JSON.stringify({
name: reaction,
channel: "{{CHANNEL_ID}}",
timestamp: timestamp,
});
const options = {
method: "POST",
headers: headerParameters,
body: bodyParameters,
};
const response = await fetch(url, options);
printResponse(response);
} catch (error) {
printError(error);
}
}
async function driver() {
const timestamp = await sendMeMessage();
console.log(timestamp);
await addReaction("eyes", timestamp);
await addReaction("tada", timestamp);
}
driver();

Let’s look at the highlighted lines in the code widget shown above:

  • Line 10: We define a function addReaction to add a reaction to a message.
  • Lines 12–16: We specify the request parameters, including the name of the reaction, the channel where we want to post the message, and the timestamp of the message to which the API call will make the reaction.
  • Line 24: We make a call to the reactions.add endpoint.
  • Line 32: We post a me message and retrieve its timestamp.
  • Lines 34–35: We call the addReaction function to add two reactions to the me message.

Response fields

The response from this endpoint contains only the ok parameter indicating whether the request was successful or not.

Remove a reaction

To remove a reaction, we access the https://slack.com/api/reactions.remove endpoint. We must specify either one of file or file_comment, or both the channel and the timestamp parameters.

Request parameters

Some important query parameters for the reactions.remove endpoint are as follows:

Parameter

Type

Category

Description

token

token

required

Authentication tokens carry the required scopes that govern the usage of different Slack applications and APIs. We usually pass these tokens as an HTTP Authorization header or as a POST parameter.

name

string

required

This is the reaction's name.

channel

string

optional

This is the ID of a channel.

timestamp

string

optional

This is the timestamp.

file

string

optional

This is the ID of a file.

file_comment

string

optional

This is the file comment ID of a file.

Let’s call the reactions.remove endpoint. Click the “Run” button to remove the reaction we gave in the previous code widget.

Press + to interact
import fetch from "node-fetch";
const url = new URL("https://slack.com/api/reactions.remove");
const headerParameters = {
Authorization: "Bearer {{TOKEN}}",
"Content-Type": "application/json; charset=UTF-8",
};
const bodyParameters = JSON.stringify({
name: "eyes",
channel: "{{CHANNEL_ID}}",
timestamp: "{{TS}}",
});
const options = {
method: "POST",
headers: headerParameters,
body: bodyParameters,
};
async function removeReaction() {
try {
const response = await fetch(url, options);
printResponse(response);
} catch (error) {
printError(error);
}
}
removeReaction();

Let’s look at the highlighted lines in the code widget shown above:

  • Lines 10–14: We specify the request parameters, including the reaction name, channel ID, and the message’s timestamp, to remove that specific reaction from it.
  • Line 24: We make a call to the reactions.remove endpoint.

Response fields

Once again, the response from this endpoint contains only the ok parameter, indicating whether the request was successful or not.