This guide explains how to use the
get()
method on the Message
resource of the Google Chat API to return details about a
text or card message.
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
Node.js
- 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 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 named
client_secrets.json
to your local directory. - To authenticate as the Chat app,
create service account
credentials and save the credentials as a JSON file named
credentials.json
.
- To authenticate as a Chat user,
create OAuth client ID
credentials and save the credentials as a JSON file named
- Choose an authorization scope based on whether you want to authenticate as a user or the Chat app.
- A Google Chat space where the authenticated user or calling Chat app is a member. To authenticate as the Chat app, add the Chat app to the space.
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 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 named
client_secrets.json
to your local directory. - To authenticate as the Chat app,
create service account
credentials and save the credentials as a JSON file named
credentials.json
.
- To authenticate as a Chat user,
create OAuth client ID
credentials and save the credentials as a JSON file named
- Choose an authorization scope based on whether you want to authenticate as a user or the Chat app.
- A Google Chat space where the authenticated user or calling Chat app is a member. To authenticate as the Chat app, add the Chat app to the space.
Java
- 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 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 named
client_secrets.json
to your local directory. - To authenticate as the Chat app,
create service account
credentials and save the credentials as a JSON file named
credentials.json
.
- To authenticate as a Chat user,
create OAuth client ID
credentials and save the credentials as a JSON file named
- Choose an authorization scope based on whether you want to authenticate as a user or the Chat app.
- A Google Chat space where the authenticated user or calling Chat app is a member. To authenticate as the Chat app, add the Chat app to the space.
Apps Script
- 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.
- 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, see Authenticate 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 where the authenticated user or calling Chat app is a member. To authenticate as the Chat app, add the Chat app to the space.
Get a message with user authentication
To get details about a message with user authentication, pass the following in your request:
- Specify the
chat.messages.readonly
orchat.messages
authorization scope. - Call the
GetMessage()
method. - Set
name
to the resource name of the message to get.
The following example gets a message with user authentication:
Node.js
Python
Java
Apps Script
To run this sample, replace the following:
SPACE_NAME
: the ID from the space'sname
. You can obtain the ID by calling theListSpaces()
method or from the space's URL.MESSAGE_NAME
: the ID from the message'sname
. You can obtain the ID from the response body returned after creating a message asynchronously with the Chat API, or with the custom name assigned to the message at creation.
The Chat API returns an instance of
Message
that details the specified message.
Get a message with app authentication
To get details about a message with app authentication, pass the following in your request:
- Specify the
chat.bot
authorization scope. - Call the
GetMessage()
method. - Set
name
to the resource name of the message to get.
The following example gets a message with app authentication:
Node.js
Python
Java
Apps Script
To run this sample, replace the following:
SPACE_NAME
: the ID from the space'sname
. You can obtain the ID by calling theListSpaces()
method or from the space's URL.MESSAGE_NAME
: the ID from the message'sname
. You can obtain the ID from the response body returned after creating a message asynchronously with the Chat API, or with the custom name assigned to the message at creation.
The Chat API returns an instance of
Message
that details the specified message.