Update a message (original) (raw)
This guide explains how to use theupdate()method on the Message resource of the Google Chat API to update a text or card message in a space. Update a message to change message attributes, such as what it says, or the content of a card. You can also prepend a text message to a card message, or append a card to a text 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
- 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.
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.
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.
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.
Update a message on behalf of a user
With user authentication, only the text of a message can be updated.
To update a message with user authentication, pass the following in your request:
- Specify the
chat.messagesauthorization scope. - Call theUpdateMessage()method.
- Pass
messageas an instance ofMessagewith the following:- The
namefield set to the message to update, which includes a space ID and a message ID. - The
textfield set with the new text.
- The
- Pass
updateMaskwith the valuetext.
If the updated message is acard message, then the text prepends to the cards (which continue to display).
Here's how to update a message, or prepend a text message to a card 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 thecustom nameassigned to the message at creation.
The Chat API returns an instance ofMessagethat details the message that's updated.
Update a message as the Chat app
With app authentication, both the text and the cards of a message can be updated.
To update a message with app authentication, pass the following in your request:
- Specify the
chat.botauthorization scope. - Call theUpdateMessage()method.
- Pass
messageas an instance ofMessagewith the following:- The
namefield set to the message to update, which includes a space ID and a message ID. - The
textfield set with the new text if it needs to be updated. - The
cardsV2field set with the new cards if they need to be updated.
- The
- Pass
updateMaskwith the list of fields to updates such astext, andcardsV2.
If the updated message is acard message and text is updated, then the updated text prepends to the cards (which continue to display). If the updated message is atext message and cards are updated, then the updated cards append to the text (which continues to display).
Here's how to update the text and cards of a message withapp 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 thecustom nameassigned to the message at creation.
The Chat API returns an instance ofMessagethat details the message that's updated.