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

本指南說明如何使用 Google Chat API 的 SpaceEvent 資源中的 get() 方法,取得 Google Chat 聊天室的事件詳細資料。

SpaceEvent 資源代表聊天室或其子項資源的變更,例如訊息、回應和成員。如要瞭解支援的事件類型,請參閱SpaceEvent 資源參考文件的eventType 欄位。

您最多可以要求 28 天前的活動。事件包含變更資源的最新版本。舉例來說,如果您要求取得新訊息的事件,但該訊息稍後更新,伺服器會在事件酬載中傳回更新後的 Message 資源。

如要呼叫這個方法,您必須使用使用者驗證。如要取得活動,已驗證的使用者必須是活動發生所在空間的成員。

必要條件

Node.js

Python

取得聊天室活動的詳細資料 (使用者驗證)

如要取得 Google Chat 中的空間事件詳細資料,請在要求中傳遞下列項目:

  • 指定授權範圍,支援要求中的事件類型。 最佳做法是選擇最嚴格的範圍,但仍允許應用程式運作。
  • 呼叫 GetSpaceEvent() 方法,並傳遞要取得的空間事件 name

以下範例會取得即時通訊空間事件:

Node.js

這個 Node.js 程式碼範例使用 Chat RPC API

chat/client-libraries/cloud/get-space-event-user-cred.js
import {createClientWithUserCredentials} from './authentication-utils.js';

// Replace SCOPE_NAME here with an authorization scope based on the event type
const USER_AUTH_OAUTH_SCOPES = ['SCOPE_NAME'];

// This sample shows how to get space event with user credential
async function main() {
  // Create a client
  const chatClient = await createClientWithUserCredentials(
    USER_AUTH_OAUTH_SCOPES,
  );

  // Initialize request argument(s)
  const request = {
    // Replace SPACE_NAME and SPACE_EVENT_NAME here
    name: 'spaces/SPACE_NAME/spaceEvents/SPACE_EVENT_NAME',
  };

  // Make the request
  const response = await chatClient.getSpaceEvent(request);

  // Handle the response
  console.log(response);
}

await main();

如要執行這個範例,請替換下列項目:

  • SCOPE_NAME:根據事件類型設定授權範圍。舉例來說,如果收到有關新會員的聊天室事件,請使用 chat.memberships.readonly 範圍,格式為 https://www.googleapis.com/auth/chat.memberships.readonly。您可以透過 ListSpaceEvents() 方法取得事件類型。如要瞭解如何使用這個方法,請參閱「列出空間中的活動」。
  • SPACE_NAME:來自聊天室 name 的 ID。您可以呼叫 ListSpaces() 方法或從空間的網址取得 ID。
  • SPACE_EVENT_NAME:來自空間事件的 ID name。 您可以透過 ListSpaceEvents() 方法取得 ID。如要瞭解如何使用這個方法,請參閱「列出空間中的活動」。

Chat API 會傳回 SpaceEvent 的執行個體,其中包含事件的詳細資料。

取得聊天室活動的詳細資料 (Chat 應用程式驗證)

應用程式驗證需要管理員核准一次。

如要使用 Chat REST API,從應用程式驗證的聊天室取得聊天室事件詳細資料,請在要求中傳遞下列項目:

  • 指定一或多個授權範圍,以支援要求中的每個事件類型。最佳做法是選擇限制最多的範圍,但仍允許應用程式運作。如要進一步瞭解如何選擇範圍,請參閱驗證和授權總覽
    • https://www.googleapis.com/auth/chat.app.memberships
    • https://www.googleapis.com/auth/chat.app.messages.readonly
    • https://www.googleapis.com/auth/chat.app.spaces
  • 呼叫 spaceEvents 資源的 get 方法
  • 傳遞要從中取得活動詳細資料的聊天室 name

建立 API 金鑰

如要呼叫開發人員預先發布版 API 方法,您必須使用非公開的開發人員預先發布版 API 探索文件。如要驗證要求,您必須傳遞 API 金鑰。

如要建立 API 金鑰,請開啟應用程式的 Google Cloud 專案,然後執行下列操作:

  1. 在 Google Cloud 控制台中,依序前往「選單」圖示 >「API 和服務」 >「憑證」

    前往「憑證」

  2. 依序按一下「建立憑證」>「API 金鑰」
  3. 系統會顯示新的 API 金鑰。
    • 按一下「複製」圖示 ,即可複製 API 金鑰,在應用程式的程式碼中使用。您也可以在專案憑證的「API 金鑰」專區中找到 API 金鑰。
    • 為避免未經授權的使用行為,建議您為可使用該 API 金鑰的 API 及使用位置新增限制。詳情請參閱「新增 API 限制」一文。

編寫呼叫 Chat API 的指令碼

以下說明如何透過應用程式驗證Chat REST API 取得聊天室活動的詳細資料:

Python

這個 Python 程式碼範例使用 Chat REST API

  1. 在工作目錄中,建立名為 chat_spaceevents_get_app.py 的檔案。
  2. chat_spaceevents_get_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.readonly` 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.messages.readonly",
              "https://www.googleapis.com/auth/chat.app.spaces"]
    
    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, discoveryServiceUrl='https://chat.googleapis.com/$discovery/rest?version=v1&labels=DEVELOPER_PREVIEW&key=API_KEY')
    
        # Use the service endpoint to call Chat API.
        result = chat.spaces().spaceEvents().get(
    
            # The space to get event details 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.
            name='spaces/SPACE_NAME/spaceEvents/SPACE_EVENT_NAME',
    
        ).execute()
    
        # Print Chat API's response in your command line interface.
        print(result)
    
    if __name__ == '__main__':
        main()
    
  3. 在程式碼中,請替換下列項目:

    • API_KEY:您建立的 API 金鑰,用於建構 Chat API 的服務端點。
    • SPACE_NAME:聊天室名稱,可透過 Chat API 中的 spaces.list 方法或聊天室網址取得。
    • SPACE_EVENT_NAME:來自空間事件的 ID name。 您可以透過 ListSpaceEvents() 方法取得 ID。如要瞭解如何使用這個方法,請參閱「列出空間中的活動」。
  4. 在工作目錄中,建構並執行範例:

    python3 chat_spaceevents_get_app.py

Chat API 會傳回空間事件的分頁清單,其中包含新成員和訊息。