Elencare le reazioni per un messaggio

Questa guida spiega come utilizzare il metodo list nella risorsa Reaction dell'API Google Chat per elencare le reazioni a un messaggio, come 👍, ⋮ e dmca.

La Reaction risorsa rappresenta un'emoji che le persone possono utilizzare per reagire a un messaggio, come 👍, ☁, e dmca.

Prerequisiti

Python

  • Python 3.6 o versioni successive
  • Lo strumento di gestione dei pacchetti pip
  • Le librerie client di Google più recenti. Per installarle o aggiornarle, esegui questo comando nell'interfaccia a riga di comando:
    pip3 install --upgrade google-api-python-client google-auth-oauthlib
    

Elenca reazioni

Per elencare le reazioni per un messaggio, trasmetti quanto segue nella richiesta:

  • Specifica chat.messages.reactions.readonly, chat.messages.reactions, chat.messages.readonly o chat.messages.
  • Chiama il [Metodo list]/workspace(/chat/api/reference/rest/v1/spaces.messages.reactions/list) il Reaction risorsa.

Nell'esempio seguente sono elencate le reazioni per un messaggio specifico:

Python

  1. Nella directory di lavoro, crea un file denominato chat_reactions_list.py.
  2. Includi il seguente codice in chat_reactions_list.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.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()
    
  3. Nel codice, sostituisci quanto segue:

    • SPACE: il nome di uno spazio, che puoi ottenere il Metodo spaces.list nell'API Chat o dall'URL di uno spazio.
    • MESSAGE: il nome di un messaggio, che puoi ottenere dal corpo della risposta restituito dopo la creazione del messaggio in modo asincrono. con l'API Chat o con nome personalizzato e assegnato al messaggio al momento della creazione.
  4. Nella directory di lavoro, crea ed esegui l'esempio:

    python3 chat_reactions_list.py
    

L'API Chat restituisce un array di reazioni impaginato.