获取消息详情

本指南介绍如何在 Google Chat API 的 Message 资源中使用 get 方法,以返回有关短信或卡片消息的详细信息。

Message 资源表示 Google Chat 中的文本卡片消息。您可以通过调用相应的方法,在 Google Chat API 中对消息执行 creategetupdatedelete 操作。如需详细了解文本和卡片消息,请参阅 Google Chat 消息概览

前提条件

Python

  • Python 3.6 或更高版本
  • pip 软件包管理工具
  • 适用于 Python 的最新 Google 客户端库。如需安装或更新它们,请在命令行界面中运行以下命令:

    pip3 install --upgrade google-api-python-client google-auth-oauthlib
    
  • 一个启用了并配置了 Google Chat API 的 Google Cloud 项目。如需了解相关步骤,请参阅构建 Google Chat 应用
  • 已为 Chat 应用配置授权。获取消息支持以下两种身份验证方法:

    • 具有 chat.messages.readonlychat.messages 授权范围的用户身份验证可以获取用户有权访问的消息。
    • 具有 chat.bot 授权范围的应用身份验证可以获取发送至应用的消息。

接收包含用户身份验证的消息

如需获取采用用户身份验证的消息的相关详细信息,请在请求中传递以下内容:

  • 指定 chat.messages.readonlychat.messages 授权范围。
  • Message 资源调用 get 方法
  • name 设置为要获取的消息的资源名称。

以下示例会获得包含用户身份验证的消息:

Python

  1. 在您的工作目录中,创建一个名为 chat_message_get_user.py 的文件。
  2. chat_message_get_user.py 中添加以下代码:

    from google_auth_oauthlib.flow import InstalledAppFlow
    from googleapiclient.discovery import build
    
    # Define your app's authorization scopes.
    # When modifying these scopes, delete the file token.json, if it exists.
    SCOPES = ["https://www.googleapis.com/auth/chat.messages.readonly"]
    
    def main():
        '''
        Authenticates with Chat API via user credentials,
        then gets a message.
        '''
    
        # Authenticate with Google Workspace
        # and get user authorization.
        flow = InstalledAppFlow.from_client_secrets_file(
                          'client_secrets.json', SCOPES)
        creds = flow.run_local_server()
    
        # Build a service endpoint for Chat API.
        chat = build('chat', 'v1', credentials=creds)
    
        # Use the service endpoint to call Chat API.
        result = chat.spaces().messages().get(
    
            # The message to get.
            #
            # Replace SPACE with a space name.
            # Obtain the space name from the spaces resource of Chat API,
            # or from a space's URL.
            #
            # Replace MESSAGE with a message name.
            # Obtain the message name from the response body returned
            # after creating a message asynchronously with Chat REST API.
            name = 'spaces/SPACE/messages/MESSAGE'
    
        ).execute()
    
        # Prints details about the created membership.
        print(result)
    
    if __name__ == '__main__':
        main()
    
  3. 在代码中,替换以下内容:

    • SPACE:聊天室名称,可通过 Chat API 中的 spaces.list 方法或聊天室网址获取。
    • MESSAGE:消息名称,您可以从通过 Chat API 异步创建消息后返回的响应正文中获取,也可以使用在创建消息时分配给该消息的自定义名称获取。
  4. 在您的工作目录中,构建并运行示例:

    python3 chat_message_get_user.py
    

Chat API 会返回 Message 的实例,其中详细说明了指定消息。

接收采用应用身份验证的消息

如需获取采用应用身份验证的消息的相关详细信息,请在请求中传递以下内容:

  • 指定 chat.bot 授权范围。
  • Message 资源调用 get 方法
  • name 设置为要获取的消息的资源名称。

以下示例会收到包含应用身份验证的消息:

Python

  1. 在您的工作目录中,创建一个名为 chat_get_message_app.py 的文件。
  2. chat_get_message_app.py 中添加以下代码:

    from google.oauth2 import service_account
    from apiclient.discovery import build
    
    # Specify required scopes.
    SCOPES = ['https://www.googleapis.com/auth/chat.bot']
    
    # Specify service account details.
    CREDENTIALS = (
        service_account.Credentials.from_service_account_file('credentials.json')
        .with_scopes(SCOPES)
    )
    
    # Build the URI and authenticate with the service account.
    chat = build('chat', 'v1', credentials=CREDENTIALS)
    
    # Get a Chat message.
    result = chat.spaces().messages().get(
    
        # The message to get.
        #
        # Replace SPACE with a space name.
        # Obtain the space name from the spaces resource of Chat API,
        # or from a space's URL.
        #
        # Replace MESSAGE with a message name.
        # Obtain the message name from the response body returned
        # after creating a message asynchronously with Chat REST API.
        name='spaces/SPACE/messages/MESSAGE'
    
      ).execute()
    
    # Print Chat API's response in your command line interface.
    print(result)
    
  3. 在代码中,替换以下内容:

    • SPACE:发布消息的聊天室的 name,可通过 Chat API 中的 spaces.list 方法或聊天室的网址获取。

    • MESSAGE:消息名称,您可以从通过 Chat API 异步创建消息后返回的响应正文中获取,也可以使用在创建消息时分配给该消息的自定义名称获取。

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

    python3 chat_get_message_app.py
    

Chat API 会返回 Message 的实例,其中详细说明了指定消息。