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
risorsa Reaction
rappresenta un'emoji che le persone possono usare per reagire a un messaggio, ad esempio 👍, 🚲 e 🌞.
Prerequisiti
Python
- Un'azienda o un'impresa Google Workspace con accesso a Google Chat.
- Configura l'ambiente:
- Creare un progetto Google Cloud.
- Configura la schermata per il consenso OAuth.
- Abilita e configura l'API Google Chat con un nome, l'icona e la descrizione dell'app Chat.
- Installa il Python Libreria client delle API di Google.
-
Crea le credenziali dell'ID client OAuth per un'applicazione desktop. Per eseguire l'esempio in questa
guida, salva le credenziali come file JSON denominato
client_secrets.json
nella directory locale.
- Scegli un ambito di autorizzazione che supporti l'autenticazione utente.
Elenco delle reazioni
Per elencare le reazioni per un messaggio, trasmetti quanto segue nella richiesta:
- Specifica l'ambito di autorizzazione
chat.messages.reactions.readonly
,chat.messages.reactions
,chat.messages.readonly
ochat.messages
. - Chiama il
[Metodo
list
]/workspace(/chat/api/reference/rest/v1/spaces.messages.reactions/list) ilReaction
risorsa.
Nell'esempio seguente sono elencate le reazioni per un messaggio specifico:
Python
- Nella directory di lavoro, crea un file denominato
chat_reactions_list.py
. 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()
Nel codice, sostituisci quanto segue:
SPACE
: un nome dello spazio, che puoi ottenere dal metodospaces.list
nell'API Chat o dall'URL di uno spazio.MESSAGE
: un nome del messaggio, che puoi ottenere dal corpo della risposta restituito dopo aver creato un messaggio in modo asincrono con l'API Chat o con il nome personalizzato assegnato al messaggio al momento della creazione.
Nella directory di lavoro, crea ed esegui l'esempio:
python3 chat_reactions_list.py
L'API Chat restituisce un array paginato di reazioni.