List messages (original) (raw)
This guide explains how to use thelist()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 theMessage 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 theGoogle Chat messages overview.
Prerequisites
Node.js
- A Business or EnterpriseGoogle Workspace account with access toGoogle 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 Node.js Cloud Client Library.
- Create access credentials based on how you want to authenticate in your Google Chat API request:
* To authenticate as a Chat user,create OAuth client ID credentials and save the credentials as a JSON file namedcredentials.jsonto your local directory.
* To authenticate as the Chat app,create service account credentials and save the credentials as a JSON file namedcredentials.json.
- Choose an authorization scope based on whether you want to authenticate as a user or the Chat app.
- A Google Chat space. To create one using the Google Chat API, seeCreate a space. To create one in Chat, visit theHelp Center documentation.
Python
- A Business or EnterpriseGoogle Workspace account with access toGoogle 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 Cloud Client Library.
- Create access credentials based on how you want to authenticate in your Google Chat API request:
* To authenticate as a Chat user,create OAuth client ID credentials and save the credentials as a JSON file namedcredentials.jsonto your local directory.
* To authenticate as the Chat app,create service account credentials and save the credentials as a JSON file namedcredentials.json.
- Choose an authorization scope based on whether you want to authenticate as a user or the Chat app.
- A Google Chat space. To create one using the Google Chat API, seeCreate a space. To create one in Chat, visit theHelp Center documentation.
Java
- A Business or EnterpriseGoogle Workspace account with access toGoogle 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 Java Cloud Client Library.
- Create access credentials based on how you want to authenticate in your Google Chat API request:
* To authenticate as a Chat user,create OAuth client ID credentials and save the credentials as a JSON file namedcredentials.jsonto your local directory.
* To authenticate as the Chat app,create service account credentials and save the credentials as a JSON file namedcredentials.json.
- Choose an authorization scope based on whether you want to authenticate as a user or the Chat app.
- A Google Chat space. To create one using the Google Chat API, seeCreate a space. To create one in Chat, visit theHelp Center documentation.
Apps Script
- A Business or EnterpriseGoogle Workspace account with access toGoogle 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.
- Create a standalone Apps Script project, and turn on the Advanced Chat Service.
- In this guide, you must use either user or app authentication. To authenticate as the Chat app, create service account credentials. For steps, seeAuthenticate and authorize as a Google Chat app.
- Choose an authorization scope based on whether you want to authenticate as a user or the Chat app.
- A Google Chat space. To create one using the Google Chat API, seeCreate a space. To create one in Chat, visit theHelp Center documentation.
List messages as a user
To list messages withuser authentication, pass the following in your request:
- Specify the
chat.messages.readonlyorchat.messagesauthorization scope. - Call theListMessages()method.
The following example lists messages in a Chat space:
Node.js
Python
Java
Apps Script
To run this sample, replace SPACE_NAME with the ID from the space'snamefield. You can obtain the ID by calling theListSpaces()method or from the space's URL.
The Chat API returns alist of messagessent in the specified space. 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, {}.
List messages as a Chat app
App authentication requires one-timeadministrator approval.
To list messages from a space withapp authenticationusing the Chat REST API, pass the following in your request:
- Specify one of the following authorization scopes:
https://www.googleapis.com/auth/chat.app.messages.readonly
- Call the list methodon themessages resource.
- Pass the
nameof the space to list messages from.
Create an API key
To call a Developer Preview API method, you must use a non-public developer preview version of the API discovery document. To authenticate the request, you must pass an API key.
To create the API Key, open your app's Google Cloud project and do the following:
- In the Google Cloud console, go to Menu > APIs & Services > Credentials.
Go to Credentials - Click Create credentials > API key.
- Your new API key is displayed.
- Click Copy to copy your API key for use in your app's code. The API key can also be found in the "API Keys" section of your project's credentials.
- To prevent unauthorized use, we recommend restricting where and for which APIs the API key can be used. For more details, seeAdd API restrictions.
Write a script that calls Chat API
Here's how to list messages withapp authentication and theChat REST API:
Python
- In your working directory, create a file named
chat_messages_list_app.py. - Include the following code in
chat_messages_list_app.py:
from google.oauth2 import service_account
from apiclient.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.app.messages.readonly"]
def main():
'''
Authenticates with Chat API using app authentication,
then lists messages 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, discoveryServiceUrl='https://chat.googleapis.com/$discovery/rest?version=v1&labels=DEVELOPER_PREVIEW&key=API_KEY')
# Use the service endpoint to call Chat API.
result = chat.spaces().messages().list(
# The space to list messages 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'
).execute()
# Print Chat API's response in your command line interface.
print(result)
if __name__ == '__main__':
main() - In the code, replace the following:
API_KEY: the API key that you created to build the service endpoint for Chat API.SPACE_NAME: a space name, which you can obtain from the spaces.list methodin the Chat API, or from a space's URL.
- In your working directory, build and run the sample:
python3 chat_messages_list_app.py The Chat API returns alist of messagessent in the specified space. 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, {}.