Mencantumkan reaksi untuk pesan

Panduan ini menjelaskan cara menggunakan metode list pada resource Reaction Google Chat API guna membuat daftar reaksi untuk pesan—seperti 👍, widget, dan ↗.

Tujuan Referensi Reaction mewakili emoji yang dapat digunakan orang untuk bereaksi terhadap pesan, seperti 👍, widget, dan dong.

Prasyarat

Python

  • Python 3.6 atau yang lebih baru
  • Alat pengelolaan paket pip
  • Library klien Google terbaru. Untuk menginstal atau memperbaruinya, jalankan perintah berikut di antarmuka command line Anda:
    pip3 install --upgrade google-api-python-client google-auth-oauthlib
    

Membuat daftar reaksi

Untuk menampilkan daftar reaksi pesan, teruskan hal berikut dalam permintaan Anda:

  • Tentukan chat.messages.reactions.readonly, chat.messages.reactions, chat.messages.readonly, atau chat.messages cakupan otorisasi.
  • Panggil [Metode list]/workspace(/chat/api/reference/rest/v1/spaces.messages.reactions/list) di referensi Reaction.

Contoh berikut mencantumkan reaksi untuk pesan tertentu:

Python

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

    • SPACE: nama ruang, yang bisa Anda dapatkan dari tindakan Metode spaces.list di Chat API, atau dari URL ruang.
    • MESSAGE: nama pesan, yang bisa Anda dapatkan dari isi respons yang ditampilkan setelah membuat pesan secara asinkron dengan Chat API, atau dengan nama khusus ditetapkan ke pesan pada saat pembuatan.
  4. Dalam direktori kerja, build dan jalankan contoh:

    python3 chat_reactions_list.py
    

Chat API menampilkan array reaksi yang diberi nomor halaman.