This guide explains how to use the delete
method on the Reaction
resource
of the Google Chat API to delete a reaction from a message—like 👍, 🚲, and 🌞.
Deleting a reaction doesn't delete the message.
The
Reaction
resource
represents an emoji that people can use to react to a message, such as 👍, 🚲,
and 🌞.
Prerequisites
Python
- A Business or Enterprise Google Workspace account with access to Google Chat.
- Python 3.6 or greater
- The pip package management tool
- The latest Google client libraries. To install or update them,
run the following command in your command-line interface:
pip3 install --upgrade google-api-python-client google-auth-oauthlib
- 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 OAuth client ID credentials for a desktop application. To run the sample in this
guide, save the credentials as a JSON file (
client_secrets.json
) to your local directory.
- Choose an authorization scope that supports user authentication.
Delete a reaction
To delete a reaction from a message, pass the following in your request:
- Specify the
chat.messages.reactions
or thechat.messages
authorization scope. - Call the
delete
method on theReaction
resource. - Set
name
to the resource name of the reaction to delete.
The following example deletes the 😀 reaction from a message:
Python
- In your working directory, create a file named
chat_reaction_delete.py
. Include the following code in
chat_reaction_delete.py
:from google_auth_oauthlib.flow import InstalledAppFlow from googleapiclient.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.messages.reactions"] def main(): ''' Authenticates with Chat API via user credentials, then deletes a reaction to 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) # Use the service endpoint to call Chat API. result = chat.spaces().messages().reactions().delete( # The reaction to delete. # # 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. # # Replace REACTION with a reaction name. # Obtain the reaction name from the reaction resource of Chat API. name = 'spaces/SPACE/messages/MESSAGE/reactions/REACTION' ).execute() 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 Chat API, or with the custom name assigned to the message at creation.REACTION
: a reaction name, which you can obtain from thespaces.messages.reactions.list
method in the Chat API or from the response body returned after creating a reaction asynchronously with the Chat API.
In your working directory, build and run the sample:
python3 chat_reaction_delete.py
If successful, the response body is empty, which indicates that the reaction is deleted.