Get details about a Google Chat space event

This guide explains how to use the get method on the SpaceEvent resource of the Google Chat API to get details about an event from a Google Chat space.

The SpaceEvent resource represents a change to a space or its child resources, such as messages, reactions, and memberships. To learn about the supported event types, see the eventType field of the SpaceEvent resource reference documentation.

You can request events up to 28 days before the time of the request. The event contains the most recent version of the resource that changed. For example, if you request an event about a new message but the message was later updated, the server returns the updated Message resource in the event payload.

To call this method, you must use user authentication. To get an event, the authenticated user must be a member of the space where the event occurred.

Prerequisites

Python

  • Python 3.6 or greater
  • The pip package management tool
  • The latest Google client libraries for Python. To install or update them, run the following command in your command-line interface:

    pip3 install --upgrade google-api-python-client google-auth-oauthlib
    
  • A Google Cloud project with the Google Chat API enabled and configured. For steps, see Build a Google Chat app.
  • Authorization configured for the Chat app. Getting a space event requires user authentication with an scope that supports the event type. To choose a scope, see the Authentication and authorization overview.

Get details about a space event

To get details about a SpaceEvent in Google Chat, do the following:

  • Call the get method on the SpaceEvent resource.
  • Pass the name of the SpaceEvent to get. Obtain the SpaceEvent name from the SpaceEvent resource of Google Chat.
  • With user authentication, specify an authorization scope that supports the event type in your request. As a best practice, choose the most restrictive scope that still allows your app to function.

Here's how to get a SpaceEvent with user authentication:

Python

  1. In your working directory, create a file named chat_space_event_get.py.
  2. Include the following code in 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. In the code, replace the following:

    • SCOPE: An authorization scope based on the event type. For example, if you are getting a space event about a new membership, use the chat.memberships.readonly scope, formatted as https://www.googleapis.com/auth/chat.memberships.readonly. You can obtain the event type from the spaces.spaceEvents.list method. To learn how to use this method, see List events from a space.
    • SPACE: A space name, which you can obtain from the spaces.list method in the Chat API, or from a space's URL.
    • SPACE_EVENT: The name of the space event, which you can obtain from the spaces.spaceEvents.list method.
  4. In your working directory, build and run the sample:

    python3 chat_space_event_get.py
    

The Chat API returns an instance of SpaceEvent with details about the event.