This guide explains how to use the patch
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.
Chat API also supports the
update
method, but we
strongly recommend calling the
patch
method because it
uses a PATCH
HTTP request while
update
uses a PUT
HTTP request. To learn more, see the
AIP-134's PATCH
and PUT
section.
The
Message
resource
represents a
text
or
card
message in Google Chat. You can
create
, get
, update
, or delete
a message in the Google Chat API by calling
corresponding methods. To learn more about text and card messages, see
Google Chat messages overview.
Prerequisites
Python
- Python 3.6 or greater
- The pip package management tool
The latest Google client libraries for Python. To install or update them, run the following command in your command-line interface:
pip3 install --upgrade google-api-python-client google-auth-httplib2 google-auth-oauthlib oauth2client
A published Chat app. To create and publish a Chat app, see Build a Google Chat app.
Authorization configured for the Chat app:
- Updating a
text message
supports both of the following authentication methods:
- User authentication,
with the
chat.messages
authorization scope, can update messages created by that user. - App authentication
with the
chat.bot
authorization scope, can update messages created by that app.
- User authentication,
with the
- Updating a
card message
requires
app authentication
with the
chat.bot
authorization scope.
- Updating a
text message
supports both of the following authentication methods:
Update a text message, or prepend a text message to a card message, with user authentication
To update a text message with user authentication, pass the following in your request:
- Specify the
chat.messages
authorization scope. - The
name
of the message to update. updateMask='text'
- A
body
that specifies the updated message.
If the updated message is a card message, then the text message prepends to the card message (which continues to display).
Here's how to update a text message, or prepend a text message to a card message with user authentication:
Python
- In your working directory, create a file named
chat_update_text_message_user.py
. Include the following code in
chat_update_text_message_user.py
:import os.path from google.auth.transport.requests import Request from google.oauth2.credentials import Credentials from google_auth_oauthlib.flow import InstalledAppFlow from googleapiclient.discovery import build from googleapiclient.errors import HttpError # 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.messages"] def main(): ''' Authenticates with Chat API via user credentials, then updates a message. ''' # Authenticate with Google Workspace # and get user authorization. flow = InstalledAppFlow.from_client_secrets_file( 'client_secrets.json', SCOPES) creds = flow.run_local_server() # Build a service endpoint for Chat API. chat = build('chat', 'v1', credentials=creds) # Update a Chat message. result = chat.spaces().messages().patch( # The message to update, and the updated message. # # Replace SPACE with a space name. # Obtain the space name from the spaces resource of Chat API, # or from a space's URL. # # Replace MESSAGE with a message name. # Obtain the message name from the response body returned # after creating a message asynchronously with Chat REST API. name='spaces/SPACE/messages/MESSAGE', updateMask='text', body={'text': 'Updated message!'} ).execute() # Prints details about the created membership. print(result) if __name__ == '__main__': main()
In the code, replace the following:
SPACE
: a space name, which you can obtain from thespaces.list
method in the Chat API, or from a space's URL.MESSAGE
: a message name, which you can obtain 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.
In your working directory, build and run the sample:
python3 chat_update_text_message_user.py
Update a text message, or prepend a text message to a card message, with app authentication
To update a text message with app authentication, pass the following in your request:
- Specify the
chat.bot
authorization scope. - The
name
of the message to update. updateMask='text'
- A
body
that specifies the updated message.
If the updated message is a card message, then the text message prepends to the card message (which continues to display).
Here's how to update a text message to a text message, or prepend a text message to a card message with app authentication:
Python
- In your working directory, create a file named
chat_update_text_message_app.py
. Include the following code in
chat_update_text_message_app.py
:from httplib2 import Http from oauth2client.service_account import ServiceAccountCredentials from apiclient.discovery import build # Specify required scopes. SCOPES = ['https://www.googleapis.com/auth/chat.bot'] # Specify service account details. CREDENTIALS = ServiceAccountCredentials.from_json_keyfile_name( 'credentials.json', SCOPES) # Build the URI and authenticate with the service account. chat = build('chat', 'v1', http=CREDENTIALS.authorize(Http())) # Update a Chat message. result = chat.spaces().messages().patch( # The message to update, and the updated message. # # Replace SPACE with a space name. # Obtain the space name from the spaces resource of Chat API, # or from a space's URL. # # Replace MESSAGE with a message name. # Obtain the message name from the response body returned # after creating a message asynchronously with Chat REST API. name='spaces/SPACE/messages/MESSAGE', updateMask='text', body={'text': 'Updated message!'} ).execute() # Print Chat API's response in your command line interface. print(result)
In the code, replace the following:
SPACE
: a space name, which you can obtain from thespaces.list
method in the Chat API, or from a space's URL.MESSAGE
: a message name, which you can obtain 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.
In your working directory, build and run the sample:
python3 chat_update_text_message_app.py
Update a card message, or append a card message to a text message
To update a card message, pass the following in your request:
- The
chat.bot
authorization scope. Updating a card message requires app authentication. - The
name
of the message to update. updateMask='cardsV2'
- A
body
that specifies the updated message.
If the updated message is a text message, then a card appends to the text message (which continues to display). If the updated message is itself a card, then the displayed card is updated.
Here's how to update a message to a card message:
Python
- In your working directory, create a file named
chat_update_card_message.py
. Include the following code in
chat_update_card_message.py
:from httplib2 import Http from oauth2client.service_account import ServiceAccountCredentials from apiclient.discovery import build # Specify required scopes. SCOPES = ['https://www.googleapis.com/auth/chat.bot'] # Specify service account details. CREDENTIALS = ServiceAccountCredentials.from_json_keyfile_name( 'credentials.json', SCOPES) # Build the URI and authenticate with the service account. chat = build('chat', 'v1', http=CREDENTIALS.authorize(Http())) # Update a Chat message. result = chat.spaces().messages().patch( # The message to update, and the updated message. # # Replace SPACE with a space name. # Obtain the space name from the spaces resource of Chat API, # or from a space's URL. # # Replace MESSAGE with a message name. # Obtain the message name from the response body returned # after creating a message asynchronously with Chat REST API. name='spaces/SPACE/messages/MESSAGE', updateMask='cardsV2', body= { 'cardsV2': [{ 'cardId': 'updateCardMessage', 'card': { 'header': { 'title': 'An Updated Card Message!', 'subtitle': 'Updated with Chat REST API', 'imageUrl': 'https://developers.google.com/chat/images/chat-product-icon.png', 'imageType': 'CIRCLE' }, 'sections': [ { 'widgets': [ { 'buttonList': { 'buttons': [ { 'text': 'Read the docs!', 'onClick': { 'openLink': { 'url': 'https://developers.google.com/chat' } } } ] } } ] } ] } }] } ).execute() # Print Chat API's response in your command line interface. print(result)
In the code, replace the following:
SPACE
: a space name, which you can obtain from thespaces.list
method in the Chat API, or from a space's URL.MESSAGE
: a message name, which you can obtain 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.
In your working directory, build and run the sample:
python3 chat_update_card_message.py
The Chat API returns an instance of
Message
that details the message that's updated.
Related topics
- Format a message.
- Delete a message.
- Get details about a message.
- List messages in a space.
- Send a message.