Add a reaction to a message

This guide explains how to use the create() method on the Reaction resource of the Google Chat API to add a reaction to a message—like 👍, 🚲, and 🌞.

The Reaction resource represents an emoji that people can use to react to a message, such as 👍, 🚲, and 🌞.

Prerequisites

Node.js

Add a reaction to a message

To create a reaction to a message, pass the following in your request:

  • Specify the chat.messages.reactions.create, chat.messages.reactions, or chat.messages authorization scope.
  • Call the CreateReaction() method, passing the parent as the resource name of the message to react to, and the reaction as a an instance of Reaction in which the unicode field is a standard emoji represented by a unicode string.

The following example reacts to a message with the 😀 emoji:

Node.js

chat/client-libraries/cloud/create-reaction-user-cred.js
import {createClientWithUserCredentials} from './authentication-utils.js';

const USER_AUTH_OAUTH_SCOPES = ['https://www.googleapis.com/auth/chat.messages.reactions.create'];

// This sample shows how to create reaction to a message with user credential
async function main() {
  // Create a client
  const chatClient = await createClientWithUserCredentials(USER_AUTH_OAUTH_SCOPES);

  // Initialize request argument(s)
  const request = {
    // Replace SPACE_NAME and MESSAGE_NAME here.
    parent: 'spaces/SPACE_NAME/messages/MESSAGE_NAME',
    reaction: {
      // A standard emoji represented by a unicode string.
      emoji: { unicode: '😀' }
    }
  };

  // Make the request
  const response = await chatClient.createReaction(request);

  // Handle the response
  console.log(response);
}

main().catch(console.error);

To run this sample, replace the following:

  • SPACE_NAME: the ID from the space's name. You can obtain the ID by calling the ListSpaces() method or from the space's URL.
  • MESSAGE_NAME: the ID from the message's name. You can obtain the ID from the response body returned after creating a message asynchronously with the Chat API, or with the custom name assigned to the message at creation.

The Chat API returns an instance of Reaction that details the reaction that's created.