Sử dụng bộ sưu tập để sắp xếp ngăn nắp các trang
Lưu và phân loại nội dung dựa trên lựa chọn ưu tiên của bạn.
Hướng dẫn này giải thích cách sử dụng phương thức create trên tài nguyên CustomEmoji của Google Chat API để tạo một biểu tượng cảm xúc tuỳ chỉnh mới trong một tổ chức Google Workspace.
Tạo thông tin đăng nhập mã ứng dụng khách OAuth cho một ứng dụng dành cho máy tính. Để chạy mẫu trong hướng dẫn này, hãy lưu thông tin đăng nhập dưới dạng tệp JSON có tên là credentials.json vào thư mục cục bộ của bạn.
Để được hướng dẫn, hãy hoàn tất các bước thiết lập môi trường trong phần hướng dẫn nhanh này.
Trong nội dung yêu cầu, hãy cung cấp một tài nguyên CustomEmoji, đặt emojiName (mã nhận dạng duy nhất mà bạn chọn cho biểu tượng cảm xúc) và payload (nội dung hình ảnh mà bạn chọn cho biểu tượng cảm xúc).
Ví dụ sau đây sẽ tạo một biểu tượng cảm xúc tuỳ chỉnh:
import{createClientWithUserCredentials}from'./authentication-utils.js';importfsfrom'fs';constUSER_AUTH_OAUTH_SCOPES=['https://www.googleapis.com/auth/chat.customemojis'];// This sample shows how to create custom emoji with user credentialasyncfunctionmain(){// Create a clientconstchatClient=awaitcreateClientWithUserCredentials(USER_AUTH_OAUTH_SCOPES);// TODO(developer) Replace FILENAME here.constfilename='FILENAME'// Read Custom emoji file content into base64 encoded stringconstfileContent=fs.readFileSync(filename,{encoding:'base64'})// Initialize request argument(s)constrequest={custom_emoji:{// TODO(developer): Replace EMOJI_NAME here.emoji_name:"EMOJI_NAME",payload:{file_content:fileContent,filename:filename,}}};// Make the requestconstresponse=awaitchatClient.createCustomEmoji(request);// Handle the responseconsole.log(response);}main().catch(console.error);
Để chạy mẫu này, hãy thay thế các nội dung sau:
FILENAME: Tên tệp của hình ảnh.
EMOJI_NAME: Tên riêng biệt cho biểu tượng cảm xúc tuỳ chỉnh của bạn, chẳng hạn như :smiley-face:.
Chat API trả về một phiên bản của CustomEmoji, trong đó nêu chi tiết biểu tượng cảm xúc tuỳ chỉnh đã được tạo.
[null,null,["Cập nhật lần gần đây nhất: 2025-08-29 UTC."],[],[],null,["# Create a custom emoji\n\nThis guide explains how to use the\n[`create`](/workspace/chat/api/reference/rpc/google.chat.v1#google.chat.v1.ChatService.CreateCustomEmoji)\nmethod on the [`CustomEmoji`](/workspace/chat/api/reference/rpc/google.chat.v1#google.chat.v1.CustomEmoji) resource of the Google Chat API to create a new\ncustom emoji in a Google Workspace organization.\n\nCustom emojis are only available for Google Workspace accounts, and your\nadministrator must turn custom emoji on for your organization. For more\ninformation, see [Learn about custom emoji in Google Chat](https://support.google.com/chat/answer/12800149) and\n[Manage custom emoji permissions](https://support.google.com/a/answer/12850085).\n\nPrerequisites\n-------------\n\n\n### Node.js\n\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\u003cbr /\u003e\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\nCreate a custom emoji\n---------------------\n\nTo create a custom emoji with\n[user authentication](/workspace/chat/authenticate-authorize-chat-user), pass the following in your request:\n\n- Specify the `chat.customemojis` authorization scope.\n- Call the [`CreateCustomEmoji`](/workspace/chat/api/reference/rpc/google.chat.v1#google.chat.v1.ChatService.CreateCustomEmoji) method.\n- In the request body, provide a `CustomEmoji` resource, setting the `emojiName` (a unique identifier you choose for the emoji) and `payload` (image content you choose for the emoji).\n\nThe following example creates a custom emoji: \n\n### Node.js\n\nchat/client-libraries/cloud/create-custom-emoji-user-cred.js \n[View on GitHub](https://github.com/googleworkspace/node-samples/blob/main/chat/client-libraries/cloud/create-custom-emoji-user-cred.js) \n\n```javascript\nimport {createClientWithUserCredentials} from './authentication-utils.js';\nimport fs from 'fs';\n\nconst USER_AUTH_OAUTH_SCOPES = ['https://www.googleapis.com/auth/chat.customemojis'];\n\n// This sample shows how to create custom emoji with user credential\nasync function main() {\n // Create a client\n const chatClient = await createClientWithUserCredentials(USER_AUTH_OAUTH_SCOPES);\n\n // TODO(developer) Replace FILENAME here.\n const filename = 'FILENAME'\n // Read Custom emoji file content into base64 encoded string\n const fileContent = fs.readFileSync(filename, {encoding: 'base64'})\n\n // Initialize request argument(s)\n const request = {\n custom_emoji: {\n // TODO(developer): Replace EMOJI_NAME here.\n emoji_name: \"EMOJI_NAME\",\n payload: {\n file_content: fileContent,\n filename: filename,\n }\n }\n };\n\n // Make the request\n const response = await chatClient.createCustomEmoji(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- `FILENAME`: A filename of the image.\n- `EMOJI_NAME`: A unique name for your custom emoji, like `:smiley-face:`.\n\nThe Chat API returns an instance of\n[`CustomEmoji`](/workspace/chat/api/reference/rpc/google.chat.v1#google.chat.v1.CustomEmoji) that details the custom emoji that was created.\n\nRelated topics\n--------------\n\n- [Delete a custom emoji](/workspace/chat/delete-custom-emoji)\n- [Get details about a custom emoji](/workspace/chat/get-custom-emoji)\n- [List custom emojis in an organization](/workspace/chat/list-custom-emojis)"]]