列出 Google Chat 聊天室中的事件

本指南說明如何使用 Google Chat API 的 SpaceEvent 資源中的 list() 方法,列出空間中資源的變更。

SpaceEvent 資源代表目標聊天室的變更,包括聊天室的子項資源,例如訊息、回應和成員。如要進一步瞭解支援的事件類型和事件酬載清單,請參閱 SpaceEvent 資源參考文件說明文件的eventTypepayload欄位。

你最多可以列出要求時間前 28 天的活動。伺服器會傳回含有受影響資源最新版本的事件。舉例來說,如果您列出有關新空間成員的事件,伺服器會傳回包含最新成員詳細資料的 Membership 資源。如果在要求期間有新成員遭移除,活動酬載會包含空白的 Membership 資源。

如要列出聊天室中的活動,已驗證的使用者或 Chat 應用程式必須是聊天室成員。

必要條件

Node.js

Python

列出即時通室事件 (使用者驗證)

如要列出 Chat 聊天室的活動,請在要求中傳遞下列項目:

  • 指定一或多個授權範圍,以支援要求中的每個事件類型。最佳做法是選擇限制最多的範圍,但仍允許應用程式運作。如要選擇範圍,請參閱「驗證與授權總覽」。

  • 呼叫 ListSpaceEvents() 方法,並傳遞要列出的事件類型 filter。您必須指定至少一個事件類型,也可以依日期篩選。 如需支援的事件類型清單,請參閱SpaceEvent 資源eventType 欄位的參考說明文件。

以下範例列出有關新成員和訊息的事件:

Node.js

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

// Authorization scopes based on the event types
const USER_AUTH_OAUTH_SCOPES = [
  'https://www.googleapis.com/auth/chat.memberships.readonly',
  'https://www.googleapis.com/auth/chat.messages.readonly',
];

// This sample shows how to list space events 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 here
    parent: 'spaces/SPACE_NAME',
    // A required filter. Filters events about new memberships and messages.
    filter:
      'eventTypes:"google.workspace.chat.membership.v1.created" OR eventTypes:"google.workspace.chat.message.v1.created"',
  };

  // Make the request
  const pageResult = chatClient.listSpaceEventsAsync(request);

  // Handle the response. Iterating over pageResult will yield results and
  // resolve additional pages automatically.
  for await (const response of pageResult) {
    console.log(response);
  }
}

await main();

如要執行這個範例,請將 SPACE_NAME 替換為空間的 name 中的 ID。您可以呼叫 ListSpaces() 方法或從空間的網址取得 ID。

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

列出空間事件 (Chat 應用程式驗證)

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

如要使用應用程式驗證Chat REST API 列出空間事件,請在要求中傳遞下列項目:

  • 指定一或多個授權範圍,以支援要求中的每個事件類型。最佳做法是選擇限制最多的範圍,但仍允許應用程式運作。如要進一步瞭解如何選擇範圍,請參閱「驗證和授權總覽」。
    • 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
  • 呼叫 spaceEvents 資源的 list 方法
  • 傳遞要列出訊息的聊天室 name
  • 傳遞 filter 即可查詢特定事件類型。

編寫呼叫 Chat API 的指令碼

以下說明如何使用應用程式驗證Chat REST API 列出即時通訊空間事件:

Python

  1. 在工作目錄中,建立名為 chat_spaceevents_list_app.py 的檔案。
  2. 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()
    
  3. 在程式碼中,請替換下列項目:

    • SPACE_NAME:聊天室名稱,可透過 Chat API 中的 spaces.list 方法或聊天室網址取得。
  4. 在工作目錄中,建構並執行範例:

    python3 chat_spaceevents_list_app.py

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