This guide explains how to use the list
method on the Reaction
resource
of the Google Chat API to list reactions for a message—like 👍, 🚲, and 🌞.
The
Reaction
resource
represents an emoji that people can use to react to a message, such as 👍, 🚲,
and 🌞.
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. Listing reactions requires User authentication with the
chat.messages.reactions.readonly
,chat.messages.reactions
,chat.messages.readonly
, orchat.messages
authorization scope.
List reactions
To list the reactions for a message, pass the following in your request:
- Specify the
chat.messages.reactions.readonly
,chat.messages.reactions
,chat.messages.readonly
, orchat.messages
authorization scope. - Call the
list
method on theReaction
resource.
The following example lists reactions for a specified message:
Python
- In your working directory, create a file named
chat_reactions_list.py
. Include the following code in
chat_reactions_list.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.reactions.readonly"] def main(): ''' Authenticates with Chat API via user credentials, then lists reactions 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().list( # The message to list reactions to. # # 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. parent = 'spaces/SPACE/messages/MESSAGE' ).execute() # Prints details about the created reactions. 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_reactions_list.py
The Chat API returns a paginated array of reactions.