Get details about a message (original) (raw)

This guide explains how to use theget()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 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

Python

Java

Apps Script

Get a message with user authentication

To get details about a message withuser authentication, pass the following in your request:

The following example gets a message withuser authentication:

Node.js

Python

Java

Apps Script

To run this sample, replace the following:

The Chat API returns an instance ofMessagethat details the specified message.

Get a message with app authentication

To get details about a message withapp authentication, pass the following in your request:

The following example gets a message withapp authentication:

Node.js

Python

Java

Apps Script

To run this sample, replace the following:

The Chat API returns an instance ofMessagethat details the specified message.

Get a message as a Chat app with administrator approval

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

To get details about a message withapp authenticationusing the Chat REST API, pass the following in your request:

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, seeAdd API restrictions.

Write a script that calls Chat API

Here's how to get details about a message withapp authentication and administrator approvaland 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:
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 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()  
  1. 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'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 thecustom nameassigned to the message at creation.
  2. In your working directory, build and run the sample:
python3 chat_messages_get_admin_app.py  

The Chat API returns an instance ofMessagethat details the specified message.