创建自定义表情符号

本指南介绍了如何使用 Google Chat API 的 CustomEmoji 资源中的 create 方法在 Google Workspace 组织中创建新的自定义表情符号。

自定义表情符号仅适用于 Google Workspace 账号,并且您的管理员必须为贵组织启用自定义表情符号。如需了解详情,请参阅了解 Google Chat 中的自定义表情符号管理自定义表情符号权限

前提条件

Node.js

创建自定义表情符号

如需创建具有用户身份验证功能的自定义表情符号,请在请求中传递以下内容:

  • 指定 chat.customemojis 授权范围。
  • 调用 CreateCustomEmoji 方法。
  • 在请求正文中,提供 CustomEmoji 资源,并设置 emojiName(您为表情符号选择的唯一标识符)和 payload(您为表情符号选择的图片内容)。

以下示例会创建一个自定义表情符号:

Node.js

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

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

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

  // TODO(developer) Replace FILENAME here.
  const filename = 'FILENAME'
  // Read Custom emoji file content into base64 encoded string
  const fileContent = fs.readFileSync(filename, {encoding: 'base64'})

  // Initialize request argument(s)
  const request = {
    custom_emoji: {
      // TODO(developer): Replace EMOJI_NAME here.
      emoji_name: "EMOJI_NAME",
      payload: {
        file_content: fileContent,
        filename: filename,
      }
    }
  };

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

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

main().catch(console.error);

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

  • FILENAME:映像的文件名。
  • EMOJI_NAME:自定义表情符号的唯一名称,例如 :smiley-face:

Chat API 会返回一个 CustomEmoji 实例,其中详细说明了已创建的自定义表情符号。