Usuwanie reakcji z wiadomości

Z tego przewodnika dowiesz się, jak używać metody delete w zasobie Reaction interfejsu Google Chat API, aby usunąć reakcję na wiadomość, np. 👍, 🚲 i 🌞. Usunięcie reakcji nie spowoduje usunięcia wiadomości.

Zasób Reaction reprezentuje emotikon, którego użytkownicy mogą używać, aby zareagować na wiadomość, np. 👍, 🚲 i 🌞.

Wymagania wstępne

Python

  • Python 3.6 lub nowszy
  • Narzędzie do zarządzania pakietami pip
  • Najnowsze biblioteki klienta Google dla języka Python. Aby je zainstalować lub zaktualizować, uruchom w interfejsie wiersza poleceń to polecenie:

    pip3 install --upgrade google-api-python-client google-auth-oauthlib
    
  • Projekt Google Cloud z włączonym i skonfigurowanym interfejsem Google Chat API. Instrukcje znajdziesz w artykule Tworzenie aplikacji Google Chat.
  • Autoryzacja aplikacji Google Chat została skonfigurowana. Usunięcie reakcji wymaga uwierzytelniania użytkownika z zakresem autoryzacji chat.messages.reactions lub chat.messages.

Usuwanie reakcji

Aby usunąć reakcję z wiadomości, przekaż w prośbie te informacje:

  • Określ zakres autoryzacji chat.messages.reactions lub chat.messages.
  • Wywołaj metodę delete w zasobie Reaction.
  • Ustaw name na nazwę zasobu reakcji, którą chcesz usunąć.

Ten przykład usuwa reakcję 😀 z wiadomością:

Python

  1. W katalogu roboczym utwórz plik o nazwie chat_reaction_delete.py.
  2. Umieść ten kod w elemencie chat_reaction_delete.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"]
    
    def main():
        '''
        Authenticates with Chat API via user credentials,
        then deletes 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().delete(
    
            # The reaction to delete.
            #
            # 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.
            #
            # Replace REACTION with a reaction name.
            # Obtain the reaction name from the reaction resource of Chat API.
            name = 'spaces/SPACE/messages/MESSAGE/reactions/REACTION'
    
        ).execute()
    
    if __name__ == '__main__':
        main()
    
  3. Zastąp w nim ten fragment kodu:

    • SPACE: nazwa pokoju, którą możesz uzyskać za pomocą metody spaces.list w interfejsie Chat API lub z adresu URL pokoju.
    • MESSAGE: nazwa wiadomości, którą można uzyskać z treści odpowiedzi zwróconej po asynchronicznym utworzeniu wiadomości za pomocą interfejsu Chat API lub z niestandardową nazwą przypisaną do wiadomości podczas jej tworzenia.
    • REACTION: nazwa reakcji, którą można uzyskać za pomocą metody spaces.messages.reactions.list w interfejsie Chat API lub z treści odpowiedzi zwracanej po asynchronicznym utworzeniu reakcji za pomocą interfejsu Chat API.
  4. W katalogu roboczym skompiluj i uruchom przykład:

    python3 chat_reaction_delete.py
    

Jeśli operacja się uda, treść odpowiedzi będzie pusta, co oznacza, że reakcja została usunięta.