Add a reaction to a message

This guide explains how to use the create method on the Reaction resource of the Google Chat API to add a reaction to 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-oauthlib
    
  • A Google Cloud project with the Google Chat API enabled and configured. For steps, see Build a Google Chat app.
  • Authorization configured for the Chat app. Creating a reaction requires User authentication with the chat.messages.reactions.create, chat.messages.reactions, or chat.messages authorization scope.

Add a reaction to a message

To create a reaction to a message, pass the following in your request:

  • Specify the chat.messages.reactions.create, chat.messages.reactions, or chat.messages authorization scope.
  • Call the create method on the Reaction resource.
  • Set parent to the resource name of the message to react to.
  • Set body (the request body) to an instance of Reaction in which the unicode field is a standard emoji represented by a unicode string.

The following example reacts to a message with the 😀 emoji:

Python

  1. In your working directory, create a file named chat_reaction_create.py.
  2. Include the following code in chat_reaction_create.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.create"]
    
    def main():
        '''
        Authenticates with Chat API via user credentials,
        then creates 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().create(
    
            # The message to create a reaction 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',
    
            # The reaction to the message.
            body = {
    
                'emoji': {
    
                    # A standard emoji represented by a unicode string.
                    'unicode': '😀'
                }
    
            }
    
        ).execute()
    
        # Prints details about the created reaction.
        print(result)
    
    if __name__ == '__main__':
        main()
    
  3. In the code, replace the following:

    • SPACE: the name of the space where the message is posted, which you can obtain from the spaces.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.

  4. In your working directory, build and run the sample:

    python3 chat_reaction_create.py
    

The Chat API returns an instance of Reaction that details the reaction that's created.