Get details about a message

  • This guide demonstrates how to retrieve details about a text or card message using the get() method of the Google Chat API's Message resource.

  • You can use either user authentication (with chat.messages.readonly or chat.messages scope) or app authentication (with chat.bot scope) to retrieve message details.

  • To retrieve a message, you'll need the name of the message, which includes the space ID and the message ID.

  • Code samples are provided in Node.js, Python, Java, and Apps Script to illustrate the process of getting a message.

  • After a successful request, the Chat API returns a Message object containing the details of the specified message.

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

Python

Java

Apps Script

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 or chat.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

chat/client-libraries/cloud/get-message-user-cred.js
import{createClientWithUserCredentials}from'./authentication-utils.js';
constUSER_AUTH_OAUTH_SCOPES=[
'https://www.googleapis.com/auth/chat.messages.readonly',
];
// This sample shows how to get message with user credential
asyncfunctionmain(){
// Create a client
constchatClient=awaitcreateClientWithUserCredentials(
USER_AUTH_OAUTH_SCOPES,
);
// Initialize request argument(s)
constrequest={
// Replace SPACE_NAME and MESSAGE_NAME here
name:'spaces/SPACE_NAME/messages/MESSAGE_NAME',
};
// Make the request
constresponse=awaitchatClient.getMessage(request);
// Handle the response
console.log(response);
}
awaitmain();

Python

chat/client-libraries/cloud/get_message_user_cred.py
fromauthentication_utilsimport create_client_with_user_credentials
importgoogle.oauth2.credentials
fromgoogle.appsimport chat_v1 as google_chat
SCOPES = ["https://www.googleapis.com/auth/chat.messages.readonly"]
# This sample shows how to get message with user credential
defget_message_with_user_cred():
 # Create a client
 client = create_client_with_user_credentials(SCOPES)
 # Initialize request argument(s)
 request = google_chat.GetMessageRequest(
 # Replace SPACE_NAME and MESSAGE_NAME here
 name = "spaces/SPACE_NAME/messages/MESSAGE_NAME",
 )
 # Make the request
 response = client.get_message(request)
 # Handle the response
 print(response)
get_message_with_user_cred()

Java

chat/client-libraries/cloud/src/main/java/com/google/workspace/api/chat/samples/GetMessageUserCred.java
importcom.google.chat.v1.ChatServiceClient;
importcom.google.chat.v1.GetMessageRequest;
importcom.google.chat.v1.Message;
// This sample shows how to get message with user credential.
publicclass GetMessageUserCred{
privatestaticfinalStringSCOPE=
"https://www.googleapis.com/auth/chat.messages.readonly";
publicstaticvoidmain(String[]args)throwsException{
try(ChatServiceClientchatServiceClient=
AuthenticationUtils.createClientWithUserCredentials(
ImmutableList.of(SCOPE))){
GetMessageRequest.Builderrequest=GetMessageRequest.newBuilder()
// replace SPACE_NAME and MESSAGE_NAME here
.setName("spaces/SPACE_NAME/members/MESSAGE_NAME");
Messageresponse=chatServiceClient.getMessage(request.build());
System.out.println(JsonFormat.printer().print(response));
}
}
}

Apps Script

chat/advanced-service/Main.gs
/**
 * This sample shows how to get message with user credential
 * 
 * It relies on the OAuth2 scope 'https://www.googleapis.com/auth/chat.messages.readonly'
 * referenced in the manifest file (appsscript.json).
 */
functiongetMessageUserCred(){
// Initialize request argument(s)
// TODO(developer): Replace SPACE_NAME and MESSAGE_NAME here
constname='spaces/SPACE_NAME/messages/MESSAGE_NAME';
// Make the request
constresponse=Chat.Spaces.Messages.get(name);
// Handle the response
console.log(response);
}

To run this sample, replace the following:

  • SPACE_NAME: the ID from the space's name. You can obtain the ID by calling the ListSpaces() method or from the space's URL.
  • MESSAGE_NAME: the ID from the message's name. 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:

The following example gets a message with app authentication:

Node.js

chat/client-libraries/cloud/get-message-app-cred.js
import{createClientWithAppCredentials}from'./authentication-utils.js';
// This sample shows how to get message with app credential
asyncfunctionmain(){
// Create a client
constchatClient=createClientWithAppCredentials();
// Initialize request argument(s)
constrequest={
// Replace SPACE_NAME and MESSAGE_NAME here
name:'spaces/SPACE_NAME/messages/MESSAGE_NAME',
};
// Make the request
constresponse=awaitchatClient.getMessage(request);
// Handle the response
console.log(response);
}
awaitmain();

Python

chat/client-libraries/cloud/get_message_app_cred.py
fromauthentication_utilsimport create_client_with_app_credentials
fromgoogle.appsimport chat_v1 as google_chat
# This sample shows how to get message with app credential
defget_message_with_app_cred():
 # Create a client
 client = create_client_with_app_credentials()
 # Initialize request argument(s)
 request = google_chat.GetMessageRequest(
 # Replace SPACE_NAME and MESSAGE_NAME here
 name = 'spaces/SPACE_NAME/messages/MESSAGE_NAME',
 )
 # Make the request
 response = client.get_message(request=request)
 # Handle the response
 print(response)
get_message_with_app_cred()

Java

chat/client-libraries/cloud/src/main/java/com/google/workspace/api/chat/samples/GetMessageAppCred.java
importcom.google.chat.v1.ChatServiceClient;
importcom.google.chat.v1.GetMessageRequest;
importcom.google.chat.v1.Message;
// This sample shows how to get message with app credential.
publicclass GetMessageAppCred{
publicstaticvoidmain(String[]args)throwsException{
try(ChatServiceClientchatServiceClient=
AuthenticationUtils.createClientWithAppCredentials()){
GetMessageRequest.Builderrequest=GetMessageRequest.newBuilder()
// replace SPACE_NAME and MESSAGE_NAME here
.setName("spaces/SPACE_NAME/members/MESSAGE_NAME");
Messageresponse=chatServiceClient.getMessage(request.build());
System.out.println(JsonFormat.printer().print(response));
}
}
}

Apps Script

chat/advanced-service/Main.gs
/**
 * This sample shows how to get message with app credential
 * 
 * It relies on the OAuth2 scope 'https://www.googleapis.com/auth/chat.bot'
 * used by service accounts.
 */
functiongetMessageAppCred(){
// Initialize request argument(s)
// TODO(developer): Replace SPACE_NAME and MESSAGE_NAME here
constname='spaces/SPACE_NAME/messages/MESSAGE_NAME';
constparameters={};
// Make the request
constresponse=Chat.Spaces.Messages.get(name,parameters,getHeaderWithAppCredentials());
// Handle the response
console.log(response);
}

To run this sample, replace the following:

  • SPACE_NAME: the ID from the space's name. You can obtain the ID by calling the ListSpaces() method or from the space's URL.
  • MESSAGE_NAME: the ID from the message's name. 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 as a Chat app with administrator approval

App authentication with a chat.app.* authorization scope requires one-time administrator approval.

To get details about a message with app authentication using the Chat REST API, pass the following in your request:

  • Call the GetMessage() method.
  • Specify the chat.app.messages.readonly authorization scope.
  • Set name to the resource name of the message to get.

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:

  1. In the Google Cloud console, go to Menu > APIs & Services > Credentials.

    Go to Credentials

  2. Click Create credentials > API key.
  3. 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, see Add API restrictions.

Write a script that calls Chat API

Here's how to get details about a message with app authentication and administrator approval and the Chat REST API:

Python

  1. In your working directory, create a file named chat_messages_get_admin_app.py.
  2. Include the following code in chat_messages_get_admin_app.py:

    fromgoogle.oauth2import service_account
    fromapiclient.discoveryimport 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"]
    defmain():
    '''
     Authenticates with Chat API using app authentication,
     then gets details about a message.
     '''
     # 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().get(
     # The message to get details about.
     #
     # Replace SPACE_NAME with a space name.
     # Obtain the space name from the spaces resource of Chat API,
     # or from a space's URL.
     name='spaces/SPACE_NAME/messages/MESSAGE_NAME',
     ).execute()
     # Print Chat API's response in your command line interface.
     print(result)
    if __name__ == '__main__':
     main()
    
  3. In the code, replace the following:

    • API_KEY: the API key that you created to build the service endpoint for Chat API.
    • SPACE_NAME: the ID from the space's name. You can obtain the ID by calling the ListSpaces() method or from the space's URL.
    • MESSAGE_NAME: the ID from the message's name. 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.
  4. In your working directory, build and run the sample:

    python3chat_messages_get_admin_app.py

The Chat API returns an instance of Message that details the specified message.

Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

Last updated 2025年10月13日 UTC.