建立自訂表情符號

本指南說明如何使用 Google Chat API 的 CustomEmoji 資源中的 create 方法,在 Google Workspace 機構中建立新的自訂表情符號。

自訂表情符號僅適用於 Google Workspace 帳戶,且管理員必須為貴機構啟用自訂表情符號。詳情請參閱「瞭解 Google Chat 中的自訂表情符號」和「管理自訂表情符號權限」。

必要條件

Node.js

建立自訂表情符號

如要使用使用者驗證建立自訂表情符號,請在要求中傳遞下列項目:

  • 指定 chat.customemojis 授權範圍。
  • 呼叫 CreateCustomEmoji 方法。
  • 在要求主體中提供 CustomEmoji 資源,並設定 emojiName (您為表情符號選擇的專屬 ID) 和 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 的例項,其中詳列已建立的自訂表情符號。