Z tego przewodnika dowiesz się, jak używać metody list
w zasobie Reaction
interfejsu Google Chat API w celu wyświetlania reakcji na wiadomość, takich jak 👍, 🚲 i 🌞.
Reaction
zasób
to emotikon, którego użytkownicy mogą używać do zareagowania na wiadomość, np. 👍, 🚲,
i 🌞.
Wymagania wstępne
Python
- Konto Google Workspace w wersji Business lub Enterprise z dostępem do Google Chat.
- Skonfiguruj środowisko:
- Utwórz projekt Google Cloud.
- Skonfiguruj ekran zgody OAuth.
- Włącz i skonfiguruj interfejs Google Chat API, podając nazwę, ikonę i opis aplikacji Google Chat.
- Zainstaluj Python Biblioteka klienta interfejsów API Google.
-
Utwórz dane logowania na podstawie identyfikatora klienta OAuth dla aplikacji komputerowej. Aby uruchomić przykładowy skrypt w tym przewodniku, zapisz dane logowania jako plik JSON o nazwie
client_secrets.json
w katalogu lokalnym.
- Wybierz zakres autoryzacji, który obsługuje uwierzytelnianie użytkowników.
Wyświetlanie reakcji
Aby wyświetlić listę reakcji na wiadomość, w swojej prośbie podaj te informacje:
- Określ zakres autoryzacji
chat.messages.reactions.readonly
,chat.messages.reactions
,chat.messages.readonly
lubchat.messages
. - Wywołaj funkcję
[metoda
list
]/workspace(/chat/api/reference/rest/v1/spaces.messages.reactions/list) wReaction
zasób.
W tym przykładzie wymienione są reakcje na określoną wiadomość:
Python
- W katalogu roboczym utwórz plik o nazwie
chat_reactions_list.py
. Dodaj do pliku
chat_reactions_list.py
ten kod: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()
Zastąp w kodzie następujące elementy:
SPACE
: nazwa pokoju, którą możesz uzyskać z: Metodaspaces.list
w interfejsie Chat API lub z adresu URL pokoju.MESSAGE
: nazwa wiadomości, którą możesz uzyskać z elementu odpowiedzi zwróconego po asynchronicznym utworzeniu wiadomości za pomocą interfejsu Chat API lub przy użyciu nazwanej niestandardowo wiadomości przy jej tworzeniu.
W katalogu roboczym skompiluj i uruchom przykład:
python3 chat_reactions_list.py
Interfejs Chat API zwraca tablicę reakcji podzieloną na strony.