在 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