Questa guida spiega come utilizzare il metodo create
nella risorsa Reaction
dell'API Google Chat per aggiungere una reazione 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
- 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.
-
Creare credenziali con ID client OAuth per un'applicazione desktop. Per eseguire l'esempio in questo
guida, salva le credenziali come file JSON denominato
client_secrets.json
nel tuo nella directory locale.
- Scegli un ambito di autorizzazione che supporti l'autenticazione degli utenti.
Aggiungere una reazione a un messaggio
Per creare una reazione a un messaggio, trasmetti quanto segue nel tuo richiesta:
- Specifica
chat.messages.reactions.create
,chat.messages.reactions
o Ambito dell'autorizzazionechat.messages
. - Chiama il
Metodo
create
ilReaction
risorsa. - Imposta
parent
sul nome della risorsa del messaggio a cui reagire. - Imposta
body
(il corpo della richiesta) su un'istanza diReaction
in cui il campounicode
è un'emoji standard rappresentata da un unicode stringa.
L'esempio seguente reagisce a un messaggio con l'emoji 😀:
Python
- Nella directory di lavoro, crea un file denominato
chat_reaction_create.py
. Includi il seguente codice 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()
Nel codice, sostituisci quanto segue:
SPACE
: ilname
dello spazio in cui che viene pubblicato, reperibile nella Metodospaces.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.
Nella directory di lavoro, crea ed esegui l'esempio:
python3 chat_reaction_create.py
L'API Chat restituisce un'istanza
Reaction
che descrive in dettaglio la reazione creata.