本指南說明如何使用 Google Chat API 的 SpaceEvent 資源中的 list() 方法,列出空間中資源的變更。
SpaceEvent 資源代表目標聊天室的變更,包括聊天室的子項資源,例如訊息、回應和成員。如要進一步瞭解支援的事件類型和事件酬載清單,請參閱 SpaceEvent 資源參考文件說明文件的eventType和payload欄位。
你最多可以列出要求時間前 28 天的活動。伺服器會傳回含有受影響資源最新版本的事件。舉例來說,如果您列出有關新空間成員的事件,伺服器會傳回包含最新成員詳細資料的 Membership 資源。如果在要求期間有新成員遭移除,活動酬載會包含空白的 Membership 資源。
如要列出聊天室中的活動,已驗證的使用者或 Chat 應用程式必須是聊天室成員。
必要條件
Node.js
- 具有 Google Chat 存取權的 Business 或 Enterprise 版 Google Workspace 帳戶。
- 設定環境:
- 建立 Google Cloud 專案。
- 設定 OAuth 同意畫面。
- 啟用及設定 Google Chat API,並為 Chat 應用程式命名、設定圖示和說明。
- 安裝 Node.js Cloud 用戶端程式庫。
- 根據您要在 Google Chat API 要求中驗證身分的方式,建立存取憑證:
- 如要以 Chat 使用者身分進行驗證,請建立 OAuth 用戶端 ID 憑證,並將憑證儲存至本機目錄中名為
credentials.json的 JSON 檔案。 - 如要以 Chat 應用程式的身分進行驗證,請建立服務帳戶憑證,並將憑證儲存為名為
credentials.json的 JSON 檔案。
- 如要以 Chat 使用者身分進行驗證,請建立 OAuth 用戶端 ID 憑證,並將憑證儲存至本機目錄中名為
- 根據您要以使用者或 Chat 應用程式的身分驗證,選擇授權範圍。
Python
- 具有 Google Chat 存取權的 Business 或 Enterprise 版 Google Workspace 帳戶。
- 設定環境:
- 建立 Google Cloud 專案。
- 設定 OAuth 同意畫面。
- 啟用及設定 Google Chat API,並為 Chat 應用程式命名、設定圖示和說明。
- 安裝 Python Cloud 用戶端程式庫。
- 根據您要在 Google Chat API 要求中驗證身分的方式,建立存取憑證:
- 如要以 Chat 使用者身分進行驗證,請建立 OAuth 用戶端 ID 憑證,並將憑證儲存至本機目錄中名為
credentials.json的 JSON 檔案。 - 如要以 Chat 應用程式的身分進行驗證,請建立服務帳戶憑證,並將憑證儲存為名為
credentials.json的 JSON 檔案。
- 如要以 Chat 使用者身分進行驗證,請建立 OAuth 用戶端 ID 憑證,並將憑證儲存至本機目錄中名為
- 根據您要以使用者或 Chat 應用程式的身分驗證,選擇授權範圍。
列出即時通室事件 (使用者驗證)
如要列出 Chat 聊天室的活動,請在要求中傳遞下列項目:
指定一或多個授權範圍,以支援要求中的每個事件類型。最佳做法是選擇限制最多的範圍,但仍允許應用程式運作。如要選擇範圍,請參閱「驗證與授權總覽」。
呼叫
ListSpaceEvents()方法,並傳遞要列出的事件類型filter。您必須指定至少一個事件類型,也可以依日期篩選。 如需支援的事件類型清單,請參閱SpaceEvent資源eventType欄位的參考說明文件。
以下範例列出有關新成員和訊息的事件:
Node.js
如要執行這個範例,請將 SPACE_NAME 替換為空間的 name 中的 ID。您可以呼叫 ListSpaces() 方法或從空間的網址取得 ID。
Chat API 會傳回空間事件的分頁清單,其中包含新成員和訊息。
列出空間事件 (Chat 應用程式驗證)
應用程式驗證需要管理員核准一次。
如要使用應用程式驗證和 Chat REST API 列出空間事件,請在要求中傳遞下列項目:
- 指定一或多個授權範圍,以支援要求中的每個事件類型。最佳做法是選擇限制最多的範圍,但仍允許應用程式運作。如要進一步瞭解如何選擇範圍,請參閱「驗證和授權總覽」。
https://www.googleapis.com/auth/chat.app.membershipshttps://www.googleapis.com/auth/chat.app.memberships.readonlyhttps://www.googleapis.com/auth/chat.app.messages.readonlyhttps://www.googleapis.com/auth/chat.app.spaceshttps://www.googleapis.com/auth/chat.app.spaces.readonly
- 呼叫
spaceEvents資源的list方法。 - 傳遞要列出訊息的聊天室
name。 - 傳遞
filter即可查詢特定事件類型。
編寫呼叫 Chat API 的指令碼
以下說明如何使用應用程式驗證和 Chat REST API 列出即時通訊空間事件:
Python
- 在工作目錄中,建立名為
chat_spaceevents_list_app.py的檔案。 在
chat_spaceevents_list_app.py中加入下列程式碼:from google.oauth2 import service_account from apiclient.discovery import build # Define your app's authorization scopes. # Set authorization scopes based on the # event type. For example, if you are getting a space event # about a new membership, use the `chat.app.memberships` scope. # # When modifying these scopes, delete the file token.json, if it exists. SCOPES = ["https://www.googleapis.com/auth/chat.app.memberships", "https://www.googleapis.com/auth/chat.app.memberships.readonly", "https://www.googleapis.com/auth/chat.app.messages.readonly", "https://www.googleapis.com/auth/chat.app.spaces", "https://www.googleapis.com/auth/chat.app.spaces.readonly"] def main(): ''' Authenticates with Chat API using app authentication, then lists space events from a specified space. ''' # Specify service account details. creds = ( service_account.Credentials.from_service_account_file('credentials.json') .with_scopes(SCOPES) ) # 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().list( # The space to list events from. # # Replace SPACE_NAME with a space name. # Obtain the space name from the spaces resource of Chat API, # or from a space's URL. parent='spaces/SPACE_NAME', # A required filter. Filters events by event type. # # Update this filter to match your requirements. filter='eventTypes:"google.workspace.chat.message.v1.created"' ).execute() # Print Chat API's response in your command line interface. print(result) if __name__ == '__main__': main()在程式碼中,請替換下列項目:
SPACE_NAME:聊天室名稱,可透過 Chat API 中的spaces.list方法或聊天室網址取得。
在工作目錄中,建構並執行範例:
python3 chat_spaceevents_list_app.py
Chat API 會傳回空間事件的分頁清單,其中包含新成員和訊息。