This guide explains how to use the
list method on the
Message resource of
the Google Chat API to see a paginated, filterable list of messages in a space.
message in Google Chat. You can
delete a message in the Google Chat API by calling
corresponding methods. To learn more about text and card messages, see
Google Chat messages overview.
- 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-httplib2 google-auth-oauthlib oauth2client
A published Chat app. To create and publish a Chat app, see Build a Google Chat app.
Authorization configured for the Chat app. Listing messages requires User authentication with the
To list messages with user authentication, pass the following in your request:
- Specify the
- Call the
listmethod on the
The following example lists messages in a Chat space sent after March 16, 2023:
- In your working directory, create a file named
Include the following code in
import os.path from google.auth.transport.requests import Request from google.oauth2.credentials import Credentials from google_auth_oauthlib.flow import InstalledAppFlow from googleapiclient.discovery import build from googleapiclient.errors import HttpError # Define your app's authorization scopes. # When modifying these scopes, delete the file token.json, if it exists. SCOPES = ["https://www.googleapis.com/auth/chat.messages.readonly"] def main(): ''' Authenticates with Chat API via user credentials, then lists messages in a space sent after March 16, 2023. ''' # 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().messages().list( # The space for which to list messages. parent = 'spaces/SPACE', # An optional filter that returns messages # created after March 16, 2023. filter = 'createTime > "2023-03-16T00:00:00-00:00"' ).execute() # Prints details about the created membership. print(result) if __name__ == '__main__': main()
In the code, replace
SPACEwith a space name, which you can obtain from the
spaces.listmethod in the Chat API, or from a space's URL.
In your working directory, build and run the sample:
The Google Chat API returns a list of messages sent in the specified space after March 16, 2023.