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.
In the Chat API, a Chat message is represented by the
Message
resource.
While Chat users can only send messages that contain text,
Chat apps can use many other messaging features, including
displaying static or interactive user interfaces, collecting information from
users, and delivering messages privately. To learn more about messaging
features available for the Chat API, see the
Google Chat messages overview.
Prerequisites
Python
- A Business or Enterprise Google Workspace account with access to Google Chat.
- Set up your environment:
- Create a Google Cloud project.
- Configure the OAuth consent screen.
- Enable and configure the Google Chat API with a name, icon, and description for your Chat app.
- Install the Python Google API Client Library.
-
Create OAuth client ID credentials for a desktop application. To run the sample in this
guide, save the credentials as a JSON file named
client_secrets.json
to your local directory.
- Choose an authorization scope that supports user authentication.
List messages
To list messages with user authentication, pass the following in your request:
- Specify the
chat.messages.readonly
orchat.messages
authorization scope. - Call the
list
method on theMessage
resource.
The following example lists messages in a Chat space sent after March 16, 2023:
Python
- In your working directory, create a file named
chat_messages_list.py
. Include the following code in
chat_messages_list.py
: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 = ["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 the list of messages. print(result) if __name__ == '__main__': main()
In the code, replace
SPACE
with a space name, which you can obtain from thespaces.list
method in the Chat API, or from a space's URL.In your working directory, build and run the sample:
python3 chat_messages_list.py
The Chat API returns a list of messages sent in the specified space
after March 16, 2023. If there are no messages from the request, the
Chat API response returns an empty object. When using a
REST/HTTP interface, the response contains an empty JSON object, {}
.