Google Chat スペースのイベントを一覧表示する

このガイドでは、Google Chat API の SpaceEvent リソースの list() メソッドを使用して、Space 内の リソースの変更を一覧表示する方法について説明します。

SpaceEvent リソース は、Space の子リソース(メッセージ、リアクション、メンバーシップなど)を含む、ターゲット Space の変更を表します。サポートされているイベントタイプとイベント ペイロードのリストについて詳しくは、eventTypepayloadフィールドの SpaceEvent リソースのリファレンス ドキュメントをご覧ください。

リクエストの時刻から 28 日前までのイベントを一覧表示できます。サーバーは、影響を受けるリソースの最新バージョンを含むイベントを返します。 たとえば、新しい Space メンバーに関するイベントを一覧表示すると、サーバーは最新のメンバーシップの詳細を含む Membership リソースを返します。リクエストされた期間中に新しいメンバーが削除された場合、イベント ペイロードには空の Membership リソースが含まれます。

Space のイベントを一覧表示するには、認証されたユーザーまたは Chat アプリが Space のメンバーである必要があります。

前提条件

Node.js

Python

Space イベントを一覧表示する(ユーザー認証)

Chat スペースの Space イベントを一覧表示するには、リクエストで次の情報を渡します。

  • リクエスト内の各イベントタイプをサポートする認可スコープを 1 つ以上指定します。ベスト プラクティスとして、アプリが機能する範囲で最も制限の厳しいスコープを選択します。スコープを選択するには、 認証と認可の概要をご覧ください。

  • ListSpaceEvents() メソッドを呼び出し、一覧表示するイベントタイプの filter を渡します。 少なくとも 1 つのイベントタイプを指定する必要があります。また、日付でフィルタすることもできます。 サポートされているイベントタイプの一覧については、 eventType リソース リファレンス ドキュメントの SpaceEvent フィールドをご覧ください。

次の例では、Space 内の新しいメンバーシップとメッセージに関するイベントを一覧表示します。

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 を Space の nameの ID に置き換えます。 ID は、 ListSpaces() メソッドを呼び出すか、Space の URL から取得できます。

Chat API は、 新しいメンバーシップとメッセージに関する Space イベントのページ分割されたリスト を返します。

Space イベントを一覧表示する(Chat アプリの認証)

アプリの認証には、管理者の 1 回限りの 承認が必要です

アプリの認証と Chat REST API を使用して Space の Space イベントを一覧表示するには、リクエストで次の情報を渡します。

  • リクエスト内の各イベントタイプをサポートする認可スコープを 1 つ以上指定します。ベスト プラクティスとして、アプリが機能する範囲で最も制限の厳しいスコープを選択します。スコープの選択について詳しくは、 認証と認可の概要をご覧ください。
    • 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 メソッドを呼び出します。
  • メッセージを一覧表示する Space の name を渡します。
  • filter を渡して、特定のイベントタイプをクエリします。

Chat API を呼び出すスクリプトを作成する

アプリの認証Chat REST APIを使用して Space イベントを一覧表示する方法は次のとおりです。

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. コード内で、次のように置き換えます。

  4. 作業ディレクトリで、サンプルをビルドして実行します。

    python3 chat_spaceevents_list_app.py

Chat API は、 新しいメンバーシップとメッセージに関する Space イベントのページ分割されたリスト を返します。