本指南介绍了如何使用 Google Chat API 的 Section 资源中的 position 方法来更改 Google Chat 中某个部分的排序顺序。
如需了解详情,请参阅在 Google Chat 中创建和整理版块。
前提条件
Python
- 拥有可访问 Google Chat 的 Google Workspace 商务版或企业版账号。
- 设置环境:
- 创建 Google Cloud 项目。
- 配置 OAuth 权限请求页面。
- 启用并配置 Google Chat API,为您的 Chat 应用指定名称、图标和说明。
- 安装 Python Cloud 客户端库。
- 为桌面应用
创建 OAuth 客户端 ID 凭据。如需运行本指南中的示例,请将凭据保存为名为
credentials.json的 JSON 文件,并将其保存到本地目录中。
- 选择支持用户身份验证的授权范围。
更改版块的位置
如需更改包含用户身份验证的部分的位置,请在请求中传递以下内容:
- 指定
chat.users.sections授权范围。 - 调用
PositionSection方法。 - 在请求正文中,指定要定位的部分的
name和新位置:- 将
sortOrder设置为绝对位置(大于 0)。 - 或者,将
relativePosition设置为START或END。
- 将
以下示例将某个部分移至列表开头:
Python
from google.cloud import chat_v1
def position_section():
# Create a client
client = chat_v1.ChatServiceClient()
# Initialize request
request = chat_v1.PositionSectionRequest(
name="SECTION_NAME",
relative_position=chat_v1.PositionSectionRequest.Position.START
)
# Make the request
response = client.position_section(request=request)
print(response)
如需运行此示例,请替换以下内容:
SECTION_NAME:版块的资源名称。您可以通过调用ListSections方法来获取资源名称。
Chat API 会返回更新后的 Section 实例。