Method: spaces.messages.create
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
在 Google Chat 聊天室中建立訊息。如需範例,請參閱「傳送訊息」一文。
支援下列類型的驗證:
- 使用授權範圍的應用程式驗證:
https://www.googleapis.com/auth/chat.bot
- 使用下列任一授權範圍進行使用者驗證:
https://www.googleapis.com/auth/chat.messages.create
https://www.googleapis.com/auth/chat.messages
https://www.googleapis.com/auth/chat.import
(僅限匯入模式聊天室)
視您在要求中使用的驗證類型而定,Chat 會為訊息寄件者指派不同的屬性。
下圖說明使用應用程式驗證功能時,Chat 如何為訊息加上屬性。Chat 會將 Chat 應用程式顯示為訊息寄件者。訊息內容可包含文字 (text
)、資訊卡 (cardsV2
) 和配件小工具 (accessoryWidgets
)。

下圖顯示使用者驗證功能時,Chat 如何歸屬訊息。Chat 會將使用者顯示為訊息傳送者,並顯示 Chat 應用程式的名稱,以便將訊息歸屬給該應用程式。訊息內容只能包含文字 (text
)。

郵件大小 (包括郵件內容) 的上限為 32,000 個位元組。
對於 webhook 要求,回應不會包含完整訊息。除了要求中的資訊之外,回應只會填入 name
和 thread.name
欄位。
HTTP 要求
POST https://chat.googleapis.com/v1/{parent=spaces/*}/messages
這個網址使用 gRPC 轉碼語法。
路徑參數
參數 |
parent |
string
必要欄位。建立訊息的聊天室資源名稱。 格式:spaces/{space}
|
查詢參數
參數 |
threadKey (deprecated) |
string
(非必要) 已淘汰:請改用 thread.thread_key 。會話串 ID。最多支援 4000 個半形字元。如要開始或加入討論串,請建立訊息,並指定 threadKey 或 thread.name 。如需使用範例,請參閱「發起或回覆訊息討論串」。
|
requestId |
string
(非必要) 此訊息的專屬要求 ID。指定現有要求 ID 會傳回使用該 ID 建立的訊息,而不會建立新訊息。
|
messageReplyOption |
enum (MessageReplyOption )
(非必要) 指定訊息是發起討論串還是回覆討論串。僅支援在已命名的聊天室中使用。 回應使用者互動時,系統會忽略這個欄位。如果是討論串內的互動,回覆會建立在同一討論串中。否則,系統會將回覆內容建立為新討論串。
|
messageId |
string
(非必要) 訊息的自訂 ID。讓 Chat 應用程式能夠取得、更新或刪除訊息,而無需在訊息的資源名稱 (在訊息的 name 欄位中表示) 中儲存系統指派的 ID。 這個欄位的值必須符合下列規定:
- 開頭為
client- 。例如,client-custom-name 是有效的自訂 ID,但 custom-name 則不是。
- 最多可包含 63 個字元,且只能使用小寫英文字母、數字和連字號。
- 在單一空間中不得重複。Chat 應用程式無法為不同的訊息使用相同的自訂 ID。
詳情請參閱「命名訊息」。
|
回應主體
如果成功,回應主體會包含新建立的 Message
例項。
授權範圍
需要下列其中一種 OAuth 範圍:
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
詳情請參閱授權指南。
MessageReplyOption
列舉 |
MESSAGE_REPLY_OPTION_UNSPECIFIED |
預設。啟動新執行緒。使用這個選項會忽略任何包含的 thread ID 或 threadKey 。 |
REPLY_MESSAGE_FALLBACK_TO_NEW_THREAD |
建立訊息,做為對 thread ID 或 threadKey 指定的會話串的回覆。如果失敗,則訊息會改為啟動新的討論串。 |
REPLY_MESSAGE_OR_FAIL |
建立訊息,做為對 thread ID 或 threadKey 指定的會話串的回覆。如果使用新的 threadKey ,系統就會建立新的執行緒。如果訊息建立作業失敗,系統會改為傳回 NOT_FOUND 錯誤。 |
除非另有註明,否則本頁面中的內容是採用創用 CC 姓名標示 4.0 授權,程式碼範例則為阿帕契 2.0 授權。詳情請參閱《Google Developers 網站政策》。Java 是 Oracle 和/或其關聯企業的註冊商標。
上次更新時間:2025-07-25 (世界標準時間)。
[null,null,["上次更新時間:2025-07-25 (世界標準時間)。"],[[["\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. |"]]