在 Google Chat 聊天室中固定或取消固定消息

借助 Chat API 中的 MessagePin 资源,您的应用可以在 Google Chat 聊天室中置顶消息、取消置顶消息,以及获取所有置顶消息的列表。固定消息会在 Chat 界面中向聊天室的所有成员显示。此 API 可帮助您的应用代表用户管理已固定的消息。

如需了解用户如何在聊天室中固定消息,请参阅在聊天室和消息中固定消息、文件和链接

前提条件

Node.js

注意事项

  • 您只能置顶聊天室中已有的消息。您无法在同一请求中创建新消息并将其置顶。
  • 您无法置顶仅自己可见的消息。例如,您无法固定从应用收到的私密消息。
  • 每个 Chat 聊天室最多可以有 100 条已固定的消息。如果应用尝试固定第 101 条消息,该 API 会返回错误。

置顶消息

如需固定包含用户身份验证的消息,请在请求中传递以下内容:

  • 指定 chat.spaces.pinschat.spaces 授权范围。
  • 调用 messagePins.create
  • 指定 parent(空间名称),并提供一个 body,其中包含原始消息的 message 资源名称。

以下示例展示了如何在聊天室中固定消息:

Python

  1. 在工作目录中,创建一个名为 chat_pin_message.py 的文件。
  2. 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()
    
  3. 在代码中,替换以下内容:

    • SPACE:来自空间 name 的 ID。
    • MESSAGE:消息的 name 中的 ID。
  4. 在工作目录中,构建并运行示例:

    python3 chat_pin_message.py

取消置顶消息

如需取消固定消息(使用用户身份验证),请在请求中传递以下内容:

  • 指定 chat.spaces.pinschat.spaces 授权范围。
  • 调用 messagePins.delete
  • name 设置为要删除的 MessagePin 的资源名称。

如需取消固定消息,请按以下步骤操作:

Python

  1. 在工作目录中,创建一个名为 chat_unpin_message.py 的文件。
  2. 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()
    
  3. 在代码中,替换以下内容:

    • SPACE:来自空间 name 的 ID。
    • MESSAGE:消息的 name 中的 ID。
  4. 在工作目录中,构建并运行示例:

    python3 chat_unpin_message.py

获取已固定的消息列表

如需获取您有权访问的空间中已固定的消息列表,请使用用户身份验证,并在请求中传递以下内容:

  • 指定以下读取范围之一:chat.spaces.pins.readonlychat.spaces.readonlychat.spaces.pinschat.spaces
  • 调用 messagePins.list
  • 指定 parent 作为要从中获取已固定消息的聊天室名称。

以下是如何列出已置顶的消息:

Python

  1. 在工作目录中,创建一个名为 chat_list_pinned_messages.py 的文件。
  2. 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()
    
  3. 在代码中,将 SPACE 替换为相应空间的 name 中的空间 ID。

  4. 在工作目录中,构建并运行示例:

    python3 chat_list_pinned_messages.py