取得 Google Chat 聊天室活動的詳細資料

本指南將說明如何使用 已啟用 get 方法 SpaceEvent 資源 可透過 Google Chat API 取得 Google Chat 聊天室中的活動詳細資料。

SpaceEvent 資源代表聊天室或其子項的變更 來分享相關資源,例如訊息、回應和會員資格學習 有關支援的事件類型,請參閱 eventType 欄位 SpaceEvent 項資源 參考文件

您最早可以在要求時間的 28 天前要求事件。事件 包含已變更資源的最新版本。舉例來說 您要求取得新訊息的事件,但訊息之後更新, 伺服器會在事件酬載中傳回更新後的 Message 資源。

如要呼叫這個方法,您必須使用 user 驗證。若要取得 事件,已驗證的使用者必須是活動聊天室成員。 發生。

必要條件

Python

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

取得聊天室活動的詳細資料

如要在 Google Chat 中取得SpaceEvent的詳細資料,請按照下列步驟操作:

以下說明如何SpaceEvent 使用者驗證

Python

  1. 在工作目錄中,建立名為 chat_space_event_get.py 的檔案。
  2. chat_space_event_get.py 中加入下列程式碼:

    """Gets a SpaceEvent resource from the Chat API."""
    
    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 = ['SCOPE']
    
    # 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()
        .spaceEvents()
        .get(
            # The space event 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 SPACE_EVENT with a SpaceEvent name.
            # Obtain the spaceEvent name from the SpaceEvent resource of
            # Chat API.
            name='spaces/SPACE/spaceEvents/SPACE_EVENT'
        )
        .execute()
    )
    
    # Prints details about the created spaceEvent.
    print(result)
    
  3. 請在程式碼中替換下列內容:

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

    python3 chat_space_event_get.py
    

Chat API 會傳回 SpaceEvent敬上 並附上活動詳情