カスタム絵文字を作成する

このガイドでは、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 のインスタンスを返します。