取得訊息的詳細資料

本指南說明如何針對以下項目的 Message 資源使用 get 方法: Google Chat API 來傳回文字或卡片訊息的詳細資料。

Message 項資源 代表的 文字資訊卡 訊息。你可以 creategetupdatedelete,透過呼叫方式在 Google Chat API 中傳送訊息 對應的方法如要進一步瞭解簡訊和資訊卡訊息,請參閱 Google Chat 訊息總覽

必要條件

Python

  • Python 3.6 以上版本
  • pip 套件管理工具
  • 最新的 Google 用戶端程式庫。安裝或更新 在指令列介面中執行下列指令:
    pip3 install --upgrade google-api-python-client google-auth-oauthlib
    

接收使用者驗證訊息

如何取得與 使用者驗證 並在要求中傳遞下列資訊:

  • 指定 chat.messages.readonlychat.messages 授權範圍。
  • get 方法Message 項資源
  • 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 message.
        print(result)
    
    if __name__ == '__main__':
        main()
    
  3. 請在程式碼中替換下列內容:

    • SPACE:聊天室名稱,您可以從中取得 這個 spaces.list 方法 或聊天室網址傳送
    • MESSAGE:訊息名稱,您可以取得 建立非同步訊息後傳回的回應主體 透過 Chat API 或 自訂名稱 是在建立訊息時指派的
  4. 在工作目錄中建構並執行範例:

    python3 chat_message_get_user.py
    

Chat API 會傳回 Message敬上 詳述指定訊息

接收應用程式驗證訊息

如何取得與 應用程式驗證, 並在要求中傳遞下列資訊:

以下範例會取得 應用程式驗證

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 訊息也會張貼,您可以在 spaces.list 方法 或聊天室網址傳送

    • MESSAGE:訊息名稱,您可以取得 建立非同步訊息後傳回的回應主體 透過 Chat API 或 自訂名稱 是在建立訊息時指派的

  4. 在工作目錄中建構並執行範例:

    python3 chat_get_message_app.py
    

Chat API 會傳回 Message敬上 詳述指定訊息