Organiza tus páginas con colecciones
Guarda y categoriza el contenido según tus preferencias.
En esta guía, se explica cómo usar el método create() en el recurso Reaction de la API de Google Chat para agregar una reacción a un mensaje, como 👍, 🚲 y 🌞.
El recurso Reaction representa un emoji que las personas pueden usar para reaccionar a un mensaje, como 👍, 🚲 y 🌞.
Crea credenciales de ID de cliente de OAuth para una aplicación de escritorio. Para ejecutar la muestra en esta guía, guarda las credenciales como un archivo JSON llamado credentials.json en tu directorio local.
Para obtener orientación, completa los pasos para configurar tu entorno en esta
guía de inicio rápido.
Para crear una reacción a un mensaje, pasa lo siguiente en tu solicitud:
Especifica el permiso de autorización chat.messages.reactions.create, chat.messages.reactions o chat.messages.
Llama al método CreateReaction() y pasa parent como el nombre del recurso del mensaje al que se reacciona y reaction como una instancia de Reaction en la que el campo unicode es un emoji estándar representado por una cadena Unicode.
En el siguiente ejemplo, se reacciona a un mensaje con el emoji 😀:
import{createClientWithUserCredentials}from'./authentication-utils.js';constUSER_AUTH_OAUTH_SCOPES=['https://www.googleapis.com/auth/chat.messages.reactions.create'];// This sample shows how to create reaction to a message with user credentialasyncfunctionmain(){// Create a clientconstchatClient=awaitcreateClientWithUserCredentials(USER_AUTH_OAUTH_SCOPES);// Initialize request argument(s)constrequest={// 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 requestconstresponse=awaitchatClient.createReaction(request);// Handle the responseconsole.log(response);}main().catch(console.error);
Para ejecutar esta muestra, reemplaza lo siguiente:
SPACE_NAME: Es el ID del name del espacio.
Puedes obtener el ID llamando al método ListSpaces() o desde la URL del espacio.
MESSAGE_NAME: Es el ID del name del mensaje.
Puedes obtener el ID del cuerpo de la respuesta que se muestra después de crear un mensaje de forma asíncrona con la API de Chat o con el nombre personalizado asignado al mensaje en el momento de la creación.
La API de Chat devuelve una instancia de Reaction que detalla la reacción creada.
[null,null,["Última actualización: 2025-08-29 (UTC)"],[[["\u003cp\u003eThis guide demonstrates how to add emoji reactions (👍, 🚲, 🌞) to Google Chat messages using the \u003ccode\u003ecreate()\u003c/code\u003e method of the Google Chat API.\u003c/p\u003e\n"],["\u003cp\u003eIt requires a Google Workspace account, a configured Google Cloud project with the Chat API enabled, and the Node.js Cloud Client Library.\u003c/p\u003e\n"],["\u003cp\u003eTo add a reaction, call the \u003ccode\u003eCreateReaction()\u003c/code\u003e method, providing the message's resource name and the desired emoji's unicode representation.\u003c/p\u003e\n"],["\u003cp\u003eA sample Node.js code snippet is included, illustrating the process of creating a reaction using user credentials.\u003c/p\u003e\n"],["\u003cp\u003eYou need to replace placeholders for space and message names within the code with your specific values to execute the sample successfully.\u003c/p\u003e\n"]]],["To add a reaction to a message using the Google Chat API, utilize the `CreateReaction()` method. Specify the message's resource name as the `parent` and provide a `Reaction` instance with a Unicode emoji string in the `unicode` field. Ensure you have the `chat.messages.reactions.create`, `chat.messages.reactions`, or `chat.messages` authorization scope. You must have a Google Workspace account, a configured Google Cloud project, and have set up the Node.js environment, including OAuth client ID credentials. The API returns the created `Reaction` details.\n"],null,["# Add a reaction to a message\n\nThis guide explains how to use the\n[`create()`](/workspace/chat/api/reference/rpc/google.chat.v1#google.chat.v1.ChatService.CreateReaction)\nmethod on the `Reaction` resource of the Google Chat API to add a reaction to a\nmessage---like 👍, 🚲, and 🌞.\n\nThe\n[`Reaction` resource](/workspace/chat/api/reference/rest/v1/spaces.messages.reactions)\nrepresents an emoji that people can use to react to a message, such as 👍, 🚲,\nand 🌞.\n\nPrerequisites\n-------------\n\n\n### Node.js\n\n- A Business or Enterprise [Google Workspace](https://support.google.com/a/answer/6043576) account with access to [Google Chat](https://workspace.google.com/products/chat/).\n\n\u003c!-- --\u003e\n\n- Set up your environment:\n - [Create a Google Cloud project](/workspace/guides/create-project).\n - [Configure the OAuth consent screen](/workspace/guides/configure-oauth-consent).\n - [Enable and configure the Google Chat API](/workspace/chat/configure-chat-api) with a name, icon, and description for your Chat app.\n - Install the Node.js [Cloud Client Library](/workspace/chat/libraries?tab=nodejs#cloud-client-libraries).\n - [Create OAuth client ID credentials](/workspace/chat/authenticate-authorize-chat-user#step-2:) for a desktop application. To run the sample in this guide, save the credentials as a JSON file named `credentials.json` to your local directory.\n\n For guidance, complete the steps for setting up your environment in this [quickstart](/workspace/chat/api/guides/quickstart/nodejs\n #set-up-environment).\n- [Choose an authorization scope](/workspace/chat/authenticate-authorize#asynchronous-chat-calls) that supports user authentication.\n\n\n| The code samples in this page use the gRPC API interface with the Google Cloud client libraries. Alternatively, you can use the REST API interface. For more information about the gRPC and REST interfaces, see [Google Chat API overview](/workspace/chat/api/reference).\n\n\u003cbr /\u003e\n\nAdd a reaction to a message\n---------------------------\n\nTo create a reaction to a message, pass the following in your\nrequest:\n\n- Specify the `chat.messages.reactions.create`, `chat.messages.reactions`, or `chat.messages` authorization scope.\n- Call the [`CreateReaction()`](/workspace/chat/api/reference/rpc/google.chat.v1#google.chat.v1.ChatService.CreateReaction) method, passing the `parent` as the resource name of the message to react to, and the `reaction` as a an instance of [`Reaction`](/workspace/chat/api/reference/rpc/google.chat.v1#google.chat.v1.Reaction) in which the `unicode` field is a standard emoji represented by a unicode string.\n\nThe following example reacts to a message with the 😀 emoji: \n\n### Node.js\n\nchat/client-libraries/cloud/create-reaction-user-cred.js \n[View on GitHub](https://github.com/googleworkspace/node-samples/blob/main/chat/client-libraries/cloud/create-reaction-user-cred.js) \n\n```javascript\nimport {createClientWithUserCredentials} from './authentication-utils.js';\n\nconst USER_AUTH_OAUTH_SCOPES = ['https://www.googleapis.com/auth/chat.messages.reactions.create'];\n\n// This sample shows how to create reaction to a message with user credential\nasync function main() {\n // Create a client\n const chatClient = await createClientWithUserCredentials(USER_AUTH_OAUTH_SCOPES);\n\n // Initialize request argument(s)\n const request = {\n // Replace SPACE_NAME and MESSAGE_NAME here.\n parent: 'spaces/SPACE_NAME/messages/MESSAGE_NAME',\n reaction: {\n // A standard emoji represented by a unicode string.\n emoji: { unicode: '😀' }\n }\n };\n\n // Make the request\n const response = await chatClient.createReaction(request);\n\n // Handle the response\n console.log(response);\n}\n\nmain().catch(console.error);\n```\n\nTo run this sample, replace the following:\n\n- \u003cvar translate=\"no\"\u003eSPACE_NAME\u003c/var\u003e: the ID from the space's [`name`](/workspace/chat/api/reference/rpc/google.chat.v1#google.chat.v1.Space.FIELDS.string.google.chat.v1.Space.name). You can obtain the ID by calling the [`ListSpaces()`](/workspace/chat/api/reference/rpc/google.chat.v1#google.chat.v1.ChatService.ListSpaces) method or from the space's URL.\n- \u003cvar translate=\"no\"\u003eMESSAGE_NAME\u003c/var\u003e: the ID from the message's [`name`](/workspace/chat/api/reference/rpc/google.chat.v1#google.chat.v1.Message.FIELDS.string.google.chat.v1.Message.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](/workspace/chat/create-messages#name_a_created_message) assigned to the message at creation.\n\nThe Chat API returns an instance of\n[`Reaction`](/workspace/chat/api/reference/rpc/google.chat.v1#google.chat.v1.Reaction)\nthat details the reaction that's created.\n\nRelated topics\n--------------\n\n- [Delete a reaction from a message](/workspace/chat/delete-reactions).\n- [List reactions for a message](/workspace/chat/list-reactions)."]]