Method: spaces.messages.create
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.
Tạo tin nhắn trong không gian Google Chat. Để biết ví dụ, hãy xem phần Gửi tin nhắn.
Hỗ trợ các loại xác thực sau:
- Xác thực ứng dụng với phạm vi uỷ quyền:
https://www.googleapis.com/auth/chat.bot
- Xác thực người dùng bằng một trong các phạm vi uỷ quyền sau:
https://www.googleapis.com/auth/chat.messages.create
https://www.googleapis.com/auth/chat.messages
https://www.googleapis.com/auth/chat.import
(chỉ dành cho dấu cách ở chế độ nhập)
Chat sẽ phân bổ người gửi thư theo cách khác nhau tuỳ thuộc vào loại xác thực mà bạn sử dụng trong yêu cầu.
Hình ảnh sau đây cho thấy cách Chat phân bổ tin nhắn khi bạn sử dụng tính năng xác thực ứng dụng. Chat hiển thị ứng dụng Chat dưới dạng người gửi tin nhắn. Nội dung của thông báo có thể chứa văn bản (text
), thẻ (cardsV2
) và tiện ích phụ kiện (accessoryWidgets
).

Hình ảnh sau đây cho thấy cách Chat phân bổ tin nhắn khi bạn sử dụng tính năng xác thực người dùng. Chat hiển thị người dùng là người gửi tin nhắn và phân bổ ứng dụng Chat cho tin nhắn đó bằng cách hiển thị tên ứng dụng. Nội dung của thông báo chỉ có thể chứa văn bản (text
).

Kích thước tối đa của thư, bao gồm cả nội dung thư, là 32.000 byte.
Đối với các yêu cầu webhook, phản hồi không chứa toàn bộ thông báo. Phản hồi chỉ điền vào các trường name
và thread.name
ngoài thông tin có trong yêu cầu.
Yêu cầu HTTP
POST https://chat.googleapis.com/v1/{parent=spaces/*}/messages
URL sử dụng cú pháp Chuyển mã gRPC.
Tham số đường dẫn
Thông số |
parent |
string
Bắt buộc. Tên tài nguyên của không gian mà bạn muốn tạo tin nhắn. Định dạng spaces/{space}
|
Tham số truy vấn
Thông số |
threadKey (deprecated) |
string
Không bắt buộc. Không dùng nữa: Hãy sử dụng thread.thread_key . Mã nhận dạng của chuỗi tin nhắn. Hỗ trợ tối đa 4.000 ký tự. Để bắt đầu hoặc thêm vào một chuỗi tin nhắn, hãy tạo một tin nhắn và chỉ định threadKey hoặc thread.name . Để biết ví dụ về cách sử dụng, hãy xem phần Bắt đầu hoặc trả lời chuỗi tin nhắn.
|
requestId |
string
Không bắt buộc. Mã yêu cầu duy nhất cho thông báo này. Việc chỉ định mã yêu cầu hiện có sẽ trả về thông báo được tạo bằng mã đó thay vì tạo thông báo mới.
|
messageReplyOption |
enum (MessageReplyOption )
Không bắt buộc. Chỉ định xem một tin nhắn có bắt đầu một chuỗi tin nhắn hay trả lời một chuỗi tin nhắn. Chỉ hỗ trợ trong không gian có tên. Khi phản hồi các lượt tương tác của người dùng, trường này sẽ bị bỏ qua. Đối với các lượt tương tác trong một chuỗi tin nhắn, tin nhắn trả lời sẽ được tạo trong cùng một chuỗi tin nhắn. Nếu không, tin nhắn trả lời sẽ được tạo dưới dạng một chuỗi tin nhắn mới.
|
messageId |
string
Không bắt buộc. Mã nhận dạng tuỳ chỉnh cho một thông báo. Cho phép ứng dụng Chat nhận, cập nhật hoặc xoá thư mà không cần lưu trữ mã nhận dạng do hệ thống chỉ định trong tên tài nguyên của thư (được biểu thị trong trường name của thư). Giá trị của trường này phải đáp ứng các yêu cầu sau:
- Bắt đầu bằng
client- . Ví dụ: client-custom-name là mã nhận dạng tuỳ chỉnh hợp lệ, nhưng custom-name thì không.
- Chứa tối đa 63 ký tự và chỉ được chứa chữ thường, số và dấu gạch nối.
- Là duy nhất trong một không gian. Ứng dụng Chat không thể sử dụng cùng một mã nhận dạng tuỳ chỉnh cho nhiều tin nhắn.
Để biết thông tin chi tiết, hãy xem bài viết Đặt tên cho thư.
|
Nội dung yêu cầu
Nội dung yêu cầu chứa một bản sao của Message
.
Nội dung phản hồi
Nếu thành công, nội dung phản hồi sẽ chứa một thực thể Message
mới tạo.
Phạm vi uỷ quyền
Yêu cầu một trong các phạm vi OAuth sau:
https://www.googleapis.com/auth/chat.bot
https://www.googleapis.com/auth/chat.import
https://www.googleapis.com/auth/chat.messages
https://www.googleapis.com/auth/chat.messages.create
Để biết thêm thông tin, hãy xem Hướng dẫn uỷ quyền.
MessageReplyOption
Chỉ định cách trả lời tin nhắn. Chúng tôi có thể thêm các tiểu bang khác trong tương lai.
Enum |
MESSAGE_REPLY_OPTION_UNSPECIFIED |
Mặc định. Bắt đầu một luồng mới. Khi sử dụng tuỳ chọn này, mọi thread ID hoặc threadKey có trong đó đều bị bỏ qua. |
REPLY_MESSAGE_FALLBACK_TO_NEW_THREAD |
Tạo thư dưới dạng thư trả lời cho chuỗi tin nhắn do thread ID hoặc threadKey chỉ định. Nếu không thành công, thông báo sẽ bắt đầu một luồng mới. |
REPLY_MESSAGE_OR_FAIL |
Tạo thư dưới dạng thư trả lời cho chuỗi tin nhắn do thread ID hoặc threadKey chỉ định. Nếu bạn sử dụng threadKey mới, một luồng mới sẽ được tạo. Nếu không tạo được thông báo, lỗi NOT_FOUND sẽ được trả về. |
Trừ phi có lưu ý khác, nội dung của trang này được cấp phép theo Giấy phép ghi nhận tác giả 4.0 của Creative Commons và các mẫu mã lập trình được cấp phép theo Giấy phép Apache 2.0. Để biết thông tin chi tiết, vui lòng tham khảo Chính sách trang web của Google Developers. Java là nhãn hiệu đã đăng ký của Oracle và/hoặc các đơn vị liên kết với Oracle.
Cập nhật lần gần đây nhất: 2025-07-25 UTC.
[null,null,["Cập nhật lần gần đây nhất: 2025-07-25 UTC."],[[["\u003cp\u003eCreates a message in a Google Chat space, attributing it to the Chat app or user based on authentication.\u003c/p\u003e\n"],["\u003cp\u003eSupports sending text, cards, and widgets using app authentication, while user authentication only allows text.\u003c/p\u003e\n"],["\u003cp\u003eOffers different message reply options for starting new threads or replying within existing ones.\u003c/p\u003e\n"],["\u003cp\u003eRequires specific authorization scopes for the request, such as \u003ccode\u003echat.bot\u003c/code\u003e or \u003ccode\u003echat.messages\u003c/code\u003e.\u003c/p\u003e\n"],["\u003cp\u003eProvides a way to name a message with a custom ID for easy retrieval and management within a space.\u003c/p\u003e\n"]]],["This document outlines the process for creating messages in Google Chat spaces via the `create()` method, using either user or app authentication. Messages can include text, cards, and widgets, with a maximum size of 32,000 bytes. The process involves a POST request to a specified URL with path parameters for the space and query parameters like `threadKey`, `requestId`, `messageReplyOption` and `messageId`. The request body defines the message content, and the successful response returns the new message details. It also specifies the required OAuth scopes.\n"],null,["# Method: spaces.messages.create\n\n- [HTTP request](#body.HTTP_TEMPLATE)\n- [Path parameters](#body.PATH_PARAMETERS)\n- [Query parameters](#body.QUERY_PARAMETERS)\n- [Request body](#body.request_body)\n- [Response body](#body.response_body)\n- [Authorization scopes](#body.aspect)\n- [MessageReplyOption](#MessageReplyOption)\n- [Try it!](#try-it)\n\nCreates a message in a Google Chat space. For an example, see [Send a message](https://developers.google.com/workspace/chat/create-messages).\n\nSupports the following types of [authentication](https://developers.google.com/workspace/chat/authenticate-authorize):\n\n- [App authentication](https://developers.google.com/workspace/chat/authenticate-authorize-chat-app) with the authorization scope:\n - `https://www.googleapis.com/auth/chat.bot`\n- [User authentication](https://developers.google.com/workspace/chat/authenticate-authorize-chat-user) with one of the following authorization scopes:\n - `https://www.googleapis.com/auth/chat.messages.create`\n - `https://www.googleapis.com/auth/chat.messages`\n - `https://www.googleapis.com/auth/chat.import` (import mode spaces only)\n\nChat attributes the message sender differently depending on the type of authentication that you use in your request.\n\nThe following image shows how Chat attributes a message when you use app authentication. Chat displays the Chat app as the message sender. The content of the message can contain text (`text`), cards (`cardsV2`), and accessory widgets (`accessoryWidgets`).\n\nThe following image shows how Chat attributes a message when you use user authentication. Chat displays the user as the message sender and attributes the Chat app to the message by displaying its name. The content of message can only contain text (`text`).\n\nThe maximum message size, including the message contents, is 32,000 bytes.\n\nFor [webhook](https://developers.google.com/workspace/chat/quickstart/webhooks) requests, the response doesn't contain the full message. The response only populates the `name` and `thread.name` fields in addition to the information that was in the request.\n\n### HTTP request\n\n`POST https://chat.googleapis.com/v1/{parent=spaces/*}/messages`\n\nThe URL uses [gRPC Transcoding](https://google.aip.dev/127) syntax.\n\n### Path parameters\n\n| Parameters ||\n|----------|----------------------------------------------------------------------------------------------------------|\n| `parent` | `string` Required. The resource name of the space in which to create a message. Format: `spaces/{space}` |\n\n### Query parameters\n\n| Parameters ||\n|------------------------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| `threadKey` **(deprecated)** | `string` Optional. Deprecated: Use [thread.thread_key](/workspace/chat/api/reference/rest/v1/spaces.messages#Message.Thread.FIELDS.thread_key) instead. ID for the thread. Supports up to 4000 characters. To start or add to a thread, create a message and specify a `threadKey` or the [thread.name](/workspace/chat/api/reference/rest/v1/spaces.messages#Message.Thread.FIELDS.name). For example usage, see [Start or reply to a message thread](https://developers.google.com/workspace/chat/create-messages#create-message-thread). |\n| `requestId` | `string` Optional. A unique request ID for this message. Specifying an existing request ID returns the message created with that ID instead of creating a new message. |\n| `messageReplyOption` | `enum (`[MessageReplyOption](/workspace/chat/api/reference/rest/v1/spaces.messages/create#MessageReplyOption)`)` Optional. Specifies whether a message starts a thread or replies to one. Only supported in named spaces. When [responding to user interactions](https://developers.google.com/workspace/chat/receive-respond-interactions), this field is ignored. For interactions within a thread, the reply is created in the same thread. Otherwise, the reply is created as a new thread. |\n| `messageId` | `string` Optional. A custom ID for a message. Lets Chat apps get, update, or delete a message without needing to store the system-assigned ID in the message's resource name (represented in the message `name` field). The value for this field must meet the following requirements: - Begins with `client-`. For example, `client-custom-name` is a valid custom ID, but `custom-name` is not. - Contains up to 63 characters and only lowercase letters, numbers, and hyphens. - Is unique within a space. A Chat app can't use the same custom ID for different messages. For details, see [Name a message](https://developers.google.com/workspace/chat/create-messages#name_a_created_message). |\n\n### Request body\n\nThe request body contains an instance of [Message](/workspace/chat/api/reference/rest/v1/spaces.messages#Message).\n\n### Response body\n\nIf successful, the response body contains a newly created instance of [Message](/workspace/chat/api/reference/rest/v1/spaces.messages#Message).\n\n### Authorization scopes\n\nRequires one of the following OAuth scopes:\n\n- `https://www.googleapis.com/auth/chat.bot`\n- `https://www.googleapis.com/auth/chat.import`\n- `https://www.googleapis.com/auth/chat.messages`\n- `https://www.googleapis.com/auth/chat.messages.create`\n\nFor more information, see the [Authorization guide](/workspace/chat/authenticate-authorize).\n\nMessageReplyOption\n------------------\n\nSpecifies how to reply to a message. More states might be added in the future.\n\n| Enums ||\n|----------------------------------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| `MESSAGE_REPLY_OPTION_UNSPECIFIED` | Default. Starts a new thread. Using this option ignores any [thread ID](/workspace/chat/api/reference/rest/v1/spaces.messages#Message.Thread.FIELDS.name) or [`threadKey`](/workspace/chat/api/reference/rest/v1/spaces.messages#Message.Thread.FIELDS.thread_key) that's included. |\n| `REPLY_MESSAGE_FALLBACK_TO_NEW_THREAD` | Creates the message as a reply to the thread specified by [thread ID](/workspace/chat/api/reference/rest/v1/spaces.messages#Message.Thread.FIELDS.name) or [`threadKey`](/workspace/chat/api/reference/rest/v1/spaces.messages#Message.Thread.FIELDS.thread_key). If it fails, the message starts a new thread instead. |\n| `REPLY_MESSAGE_OR_FAIL` | Creates the message as a reply to the thread specified by [thread ID](/workspace/chat/api/reference/rest/v1/spaces.messages#Message.Thread.FIELDS.name) or [`threadKey`](/workspace/chat/api/reference/rest/v1/spaces.messages#Message.Thread.FIELDS.thread_key). If a new `threadKey` is used, a new thread is created. If the message creation fails, a `NOT_FOUND` error is returned instead. |"]]