Menghapus reaksi dari pesan

Panduan ini menjelaskan cara menggunakan metode delete pada resource Reaction di Google Chat API untuk menghapus reaksi dari pesan—seperti 👍, 🚨, dan 🔥. Menghapus reaksi tidak akan menghapus pesan.

Sumber daya Reaction mewakili emoji yang dapat digunakan orang untuk bereaksi terhadap pesan, seperti 👍, &nbsp, dan Pencapaian.

Prasyarat

Python

  • Python 3.6 atau yang lebih baru
  • Alat pengelolaan paket pip
  • Library klien Google terbaru untuk Python. Untuk menginstal atau mengupdatenya, jalankan perintah berikut di antarmuka command line Anda:

    pip3 install --upgrade google-api-python-client google-auth-oauthlib
    
  • Project Google Cloud dengan Google Chat API yang diaktifkan dan dikonfigurasi. Untuk mengetahui langkah-langkahnya, lihat Mem-build aplikasi Google Chat.
  • Otorisasi dikonfigurasi untuk aplikasi Chat. Penghapusan reaksi memerlukan Autentikasi pengguna dengan cakupan otorisasi chat.messages.reactions atau chat.messages.

Menghapus reaksi

Untuk menghapus reaksi dari pesan, teruskan hal berikut dalam permintaan Anda:

  • Tentukan cakupan otorisasi chat.messages.reactions atau chat.messages.
  • Panggil metode delete di resource Reaction.
  • Setel name ke nama resource reaksi yang akan dihapus.

Contoh berikut menghapus reaksi Tasks dari pesan:

Python

  1. Di direktori kerja Anda, buat file bernama chat_reaction_delete.py.
  2. Sertakan kode berikut di 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. Dalam kode, ganti kode berikut:

    • SPACE: nama ruang, yang dapat Anda peroleh dari metode spaces.list di Chat API, atau dari URL ruang.
    • MESSAGE: nama pesan, yang dapat Anda peroleh dari isi respons yang ditampilkan setelah membuat pesan secara asinkron dengan Chat API, atau dengan nama khusus yang ditetapkan ke pesan pada saat pembuatan.
    • REACTION: nama reaksi, yang dapat Anda peroleh dari metode spaces.messages.reactions.list di Chat API atau dari isi respons yang ditampilkan setelah membuat reaksi secara asinkron dengan Chat API.
  4. Di direktori kerja Anda, buat dan jalankan contoh:

    python3 chat_reaction_delete.py
    

Jika berhasil, isi respons kosong, yang menunjukkan bahwa reaksi dihapus.