Method: spaces.messages.create
在 Google Chat 聊天室中创建消息。如需查看示例,请参阅发送消息。
create()
方法需要进行用户身份验证或应用身份验证。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。最多支持 4,000 个字符。如需发起或添加会话,请创建消息并指定 threadKey 或 thread.name 。如需查看使用示例,请参阅发起或回复消息串。
|
requestId |
string
可选。此消息的唯一请求 ID。指定现有请求 ID 会返回使用该 ID 创建的消息,而不是创建新消息。
|
messageReplyOption |
enum (MessageReplyOption )
可选。指定消息是发起话题还是回复话题。仅在命名空间中受支持。 响应用户互动时,系统会忽略此字段。对于会话中的互动,系统会在同一会话中创建回复。否则,系统会将回复创建为新会话。
|
messageId |
string
可选。消息的自定义 ID。让 Chat 应用能够获取、更新或删除消息,而无需将系统分配的 ID 存储在消息的资源名称(在消息的 name 字段中表示)中。 此字段的值必须满足以下要求:
- 以
client- 开头。例如,client-custom-name 是有效的自定义 ID,但 custom-name 不是。
- 最多包含 63 个字符,且只能包含小写字母、数字和连字符。
- 在聊天室内是唯一的。聊天应用无法为不同的消息使用相同的自定义 ID。
如需了解详情,请参阅为消息命名。
|
请求正文
请求正文包含一个 Message
实例。
响应正文
如果成功,响应正文将包含一个新创建的 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 错误。 |
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-02-27。
[null,null,["最后更新时间 (UTC):2025-02-27。"],[[["Creates a message in a Google Chat space, attributing it to the Chat app or user based on authentication."],["Supports sending text, cards, and widgets using app authentication, while user authentication only allows text."],["Offers different message reply options for starting new threads or replying within existing ones."],["Requires specific authorization scopes for the request, such as `chat.bot` or `chat.messages`."],["Provides a way to name a message with a custom ID for easy retrieval and management within a space."]]],["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"]]