借助 Chat API 中的 MessagePin 资源
,您的应用可以置顶消息、取消置顶消息,以及
获取 Google Chat 聊天室中所有已置顶消息的列表。已置顶的消息会在 Chat 界面中向聊天室的所有成员显示。此 API 可帮助您的应用代表用户管理已置顶的消息。
如需了解用户如何在聊天室中置顶消息,请参阅在聊天室和消息中固定消息、文件和& 链接。
前提条件
Node.js
- 拥有可访问 Google Chat的 Google Workspace商务版或企业版账号。
- 设置环境:
- 创建 Google Cloud 项目。
- 配置 OAuth 权限请求页面。
- 启用并配置 Google Chat API,为您的 Chat 应用指定名称、 图标和说明。
- 安装 Node.js 版 Cloud 客户端库。
-
为桌面应用创建 OAuth 客户端 ID 凭据。如需运行本
指南中的示例,请将凭据另存为名为
credentials.json的 JSON 文件,并保存到本地 目录中。
如需获得相关指导,请按照此快速入门中的步骤设置环境。
- 选择支持用户身份验证的授权范围。
注意事项
- 您只能置顶聊天室中已存在的消息。您无法在同一请求中创建新消息并将其置顶。
- 您无法置顶仅自己可见的消息。例如,您无法置顶从应用收到的私信。
- 每个 Chat 聊天室最多可以有 100 条已置顶的消息。如果您的应用尝试置顶第 101 条消息,API 会返回错误。
置顶消息
如需在进行用户身份验证的情况下置顶消息, 请在请求中传递以下内容:
- 指定
chat.spaces.pins或chat.spaces授权范围。 - 调用
messagePins.create。 - 指定
parent(聊天室名称),并提供一个body,其中包含原始消息的message资源名称。
以下示例会在聊天室中置顶一条消息:
Python
- 在工作目录中,创建一个名为
chat_pin_message.py的文件。 在
chat_pin_message.py中添加以下代码:from google_auth_oauthlib.flow import InstalledAppFlow from googleapiclient.discovery import build # Define your app's authorization scopes. SCOPES = ["https://www.googleapis.com/auth/chat.spaces.pins"] def main(): ''' Authenticates with Chat API via user credentials, then pins a message in a space. ''' # Authenticate with Google Workspace and get user authorization. flow = InstalledAppFlow.from_client_secrets_file('credentials.json', SCOPES) creds = flow.run_local_server() # Build a service endpoint for Chat API. service = build('chat', 'v1', credentials=creds) # Pin a message. result = service.spaces().messagePins().create( # The space to pin the message in. # # Replace SPACE with a space ID or name. # Obtain the space name from the spaces resource of Chat API, # or from a space's URL. parent='spaces/SPACE', # The message to pin. body={ 'message': 'spaces/SPACE/messages/MESSAGE' } ).execute() # Print Chat API's response in your command line interface. print(result) if __name__ == '__main__': main()在代码中,替换以下内容:
在工作目录中,构建并运行示例:
python3 chat_pin_message.py
取消置顶消息
如需在进行 用户身份验证 的情况下取消置顶消息,请在请求中传递以下内容:
- 指定
chat.spaces.pins或chat.spaces授权范围。 - 调用
messagePins.delete。 - 将
name设置为要删除的MessagePin的资源名称。
下面介绍如何取消置顶消息:
Python
- 在工作目录中,创建一个名为
chat_unpin_message.py的文件。 在
chat_unpin_message.py中添加以下代码:from google_auth_oauthlib.flow import InstalledAppFlow from googleapiclient.discovery import build # Define your app's authorization scopes. SCOPES = ["https://www.googleapis.com/auth/chat.spaces.pins"] def main(): ''' Authenticates with Chat API via user credentials, then unpins a message from a space. ''' # Authenticate with Google Workspace and get user authorization. flow = InstalledAppFlow.from_client_secrets_file('credentials.json', SCOPES) creds = flow.run_local_server() # Build a service endpoint for Chat API. service = build('chat', 'v1', credentials=creds) # Unpin a message. result = service.spaces().messagePins().delete( # The resource name of the message pin to delete. # # Replace SPACE with a space ID or name, and MESSAGE with the message ID. name='spaces/SPACE/messagePins/MESSAGE' ).execute() print(result) if __name__ == '__main__': main()在代码中,替换以下内容:
在工作目录中,构建并运行示例:
python3 chat_unpin_message.py
获取已置顶消息的列表
如需获取您有权访问的聊天室中已置顶消息的列表,请使用 用户身份验证,并在 请求中传递以下内容:
- 指定以下读取范围之一:
chat.spaces.pins.readonly、chat.spaces.readonly、chat.spaces.pins或chat.spaces。 - 调用
messagePins.list。 - 将
parent指定为要从中提取已置顶消息的聊天室名称。
下面介绍如何列出已置顶的消息:
Python
- 在工作目录中,创建一个名为
chat_list_pinned_messages.py的文件。 在
chat_list_pinned_messages.py中添加以下代码:from google_auth_oauthlib.flow import InstalledAppFlow from googleapiclient.discovery import build # Define your app's authorization scopes. SCOPES = ["https://www.googleapis.com/auth/chat.spaces.pins.readonly"] def main(): ''' Authenticates with Chat API via user credentials, then lists pinned messages in a space. ''' # Authenticate with Google Workspace and get user authorization. flow = InstalledAppFlow.from_client_secrets_file('credentials.json', SCOPES) creds = flow.run_local_server() # Build a service endpoint for Chat API. service = build('chat', 'v1', credentials=creds) # List pinned messages. result = service.spaces().messagePins().list( # The space to list pinned messages from. # # Replace SPACE with a space ID or name. parent='spaces/SPACE' ).execute() print(result) if __name__ == '__main__': main()在代码中,将
SPACE替换为聊天室name中的聊天室 ID。在工作目录中,构建并运行示例:
python3 chat_list_pinned_messages.py