为消息添加回应

本指南介绍了如何使用 Google Chat API 的 Reaction 资源中的 create() 方法向消息添加表情符号回应,例如 👍、🚲 和 🌞。

Reaction 资源表示用户可用于回应消息的表情符号,例如 👍、🚲 和 🌞。

前提条件

Node.js

为消息添加回应

如需创建对消息的表情回应,请在请求中传递以下内容:

  • 指定 chat.messages.reactions.createchat.messages.reactionschat.messages 授权范围。
  • 调用 CreateReaction() 方法,传入 parent 作为要做出反应的消息的资源名称,并传入 reaction 作为 Reaction 的实例,其中 unicode 字段是 Unicode 字符串表示的标准表情符号。

以下示例使用 😀 表情符号回复消息:

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);

如需运行此示例,请替换以下内容:

  • SPACE_NAME:来自空间的 name 的 ID。 您可以通过调用 ListSpaces() 方法或从空间的网址中获取 ID。
  • MESSAGE_NAME:消息的 name 中的 ID。 您可以通过以下方式获取 ID:使用 Chat API 异步创建消息后返回的响应正文,或者创建消息时分配给消息的自定义名称

Chat API 会返回 Reaction 的实例,其中详细说明了创建的反应。