本页面介绍了如何以 Google Chat 应用。
私信是指只有 对指定 Chat 用户可见。您可以在以下位置使用私信: 可与多人创建聊天室 聊天应用。例如,您的 Chat 应用可以私下发送消息以执行任何操作 以下:
- 响应斜杠命令。例如,如果
用户调用您的 Chat 应用的
/about
斜杠 指令,您的 Chat 应用就可以使用 用于说明您的 Chat 应用的 及其使用方法 - 通知或发送仅与一位用户相关的信息。对于 例如,通知用户自己被分配了任务,或提醒他们 以完成任务。
- 发送错误消息。例如,如果用户省略了必需的参数文本 对于斜杠命令,Chat 应用可以发送 私人消息来说明错误并帮助用户设置命令格式。
- 当用户被添加到聊天室中时,向他们发送私密欢迎消息 解释了相关准则或 Chat 应用的使用方法。
当 Chat 应用发送私信时,消息 会显示一个标签,通知用户相应邮件仅对他们可见:
前提条件
Node.js
- 一款已启用互动功能的 Google Chat 应用。要创建 交互式 Chat 应用,请完成此快速入门。
为了私下响应斜杠命令,请为以下内容配置斜杠命令: Chat 应用。如需制作此类报告,请参阅 响应斜杠命令。
要使用
messages.create()
方法,必须使用应用身份验证。
Python
- 一款已启用互动功能的 Google Chat 应用。要创建 交互式 Chat 应用,请完成此快速入门。
为了私下响应斜杠命令,请为以下内容配置斜杠命令: Chat 应用。如需制作此类报告,请参阅 响应斜杠命令。
要使用
messages.create()
方法,必须使用应用身份验证。
Apps 脚本
- 一款已启用互动功能的 Google Chat 应用。要创建 交互式聊天应用,请完成此快速入门。
为了私下响应斜杠命令,请为以下内容配置斜杠命令: Chat 应用。如需制作此类报告,请参阅 响应斜杠命令。
要使用
messages.create()
方法,必须使用应用身份验证。
发送私人消息
要以 Chat 应用的形式私密发送消息,您可以指定
privateMessageViewer
请在消息中填写此字段。您可以像
您通过响应用户互动来创建消息
异步调用 Google Chat API 的
create()
方法(在
Message
资源。有关发送短信或卡片消息的步骤,请参阅
发送消息。
以下示例显示了包含以下内容的私信的 JSON:
Hello private world!
:
{
"text": "Hello private world!",
"privateMessageViewer": "USER"
}
在此示例中,USER
表示 Chat 用户
可以查看邮件的人,格式为
User
资源。如果回复的是
可以指定互动事件中的 User
对象。
如需查看示例,请参阅以下部分
以私密方式响应斜杠命令。
或者,要指定私人邮件的查看者,您可以使用
User
的 name
字段
资源:
{
"text": "Hello private world!",
"privateMessageViewer": {
"name": "users/USER_ID"
}
}
在此示例中,您将使用 name
字段指定查看者的 User
Google Chat 中的资源名称。替换 USER_ID
用户的唯一 ID,例如 12345678987654321
或
hao@cymbalgroup.com
。
如需详细了解如何指定用户,请参阅 识别并指定 Google Chat 用户。
以私密方式响应斜杠命令
以下代码展示了 Chat 应用的示例 以通过私信响应斜杠命令。
Chat 应用会处理
MESSAGE
互动事件
以及回复/help
斜杠命令的私人短信
解释了使用方法:
Node.js
/**
* Responds to a MESSAGE event in Google Chat.
*
* @param {!Object} req Request sent from Google Chat app
* @param {!Object} res Response to send back
*
* @return {!Object} respond to slash command
*/
exports.onMessage = function onMessage(req, res) {
if (req.method === 'GET' || !req.body.message) {
return res.send('Hello! This function is meant to be used in Google Chat app.');
}
const event = req.body;
// Checks for the presence of event.message.slashCommand.
// If the slash command is "/help", responds with a private text message.
if (event.message.slashCommand) {
switch (event.message.slashCommand.commandId) {
case '1': // /help
return res.json({
privateMessageViewer: event.user,
text: 'This Chat app was created by Cymbal Labs. To get help with this app, <https://cymbalgroup.com/docs|see our documentation> or <https://cymbalgroup.com/support|contact our support team>.'
});
}
}
// If the Chat app doesn't detect a slash command, it responds
// with a private text message
return res.json({
privateMessageViewer: event.user,
text: 'Try a slash command.'
});
};
Apps 脚本
/**
* Responds to a MESSAGE event in Google Chat.
*
* @param {Object} event the event object from Google Chat
*/
function onMessage(event) {
if (event.message.slashCommand) {
switch (event.message.slashCommand.commandId) {
case 1: // Responds to /help
return {
"privateMessageViewer": event.user,
"text": "This Chat app was created by Cymbal Labs. To get help with this app, <https://cymbalgroup.com/docs|see our documentation> or <https://cymbalgroup.com/support|contact our support team>."
};
}
}
else {
return { "text": "Try a slash command.", "privateMessageViewer": event.user };
}
}
Python
from typing import Any, Mapping
import flask
import functions_framework
@functions_framework.http
def main(req: flask.Request) -> Mapping[str, Any]:
"""Responds to a MESSAGE event in Google Chat.
Args:
req (flask.Request): the event object from Chat API.
Returns:
Mapping[str, Any]: open a Dialog in response to a card's button click.
"""
if req.method == 'GET':
return 'Hello! This function must be called from Google Chat.'
request = req.get_json(silent=True)
# Checks for the presence of event.message.slashCommand.
# If the slash command is "/help", responds with a private text message.
if request.get('message', {}).get('slashCommand'):
command_id = request.get('message', {}).get('slashCommand').get('commandId')
if command_id == '1': # /help
return {
'privateMessageViewer': request.get('user'),
'text': (
'This Chat app was created by Cymbal Labs. To get help with this'
' app, <https://cymbalgroup.com/docs|see our documentation> or'
' <https://cymbalgroup.com/support|contact our support team>.'
),
}
return {
'privateMessageViewer': request.get('user'),
'text': 'Try a slash command.',
}
限制
要发送私信,私信中不能包含或使用 以下:
- 附件。
- 配件操作。
- 部分私密的消息。例如,Chat 应用 无法发送仅包含文字和卡片的短信 一个用户,但聊天室中的所有人都可以看到该卡片。
- 用户身份验证。 只有 Chat 扩展应用可以发送私信,因此您的 Chat 应用无法验证用户的身份,因此无法发送 私密消息。
更新或删除私信
要更新或删除 Google Chat 消息,您必须调用
Chat API。您不能更改私信的查看者,
将消息公开。因此,在更新私信时,您必须忽略
API 调用中的 privateMessageViewer
字段(该字段只是输出)。
如需更新私信,请参阅 更新消息。 要删除私信,请参阅 删除消息。