借助 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