Panduan ini menjelaskan cara menggunakan metode create
pada resource Reaction
Google Chat API untuk menambahkan reaksi terhadap pesan—seperti 👍, widget, dan Tidur.
Tujuan
Referensi Reaction
mewakili emoji yang dapat digunakan pengguna untuk bereaksi terhadap pesan, seperti 👍, widget,
dan pintar.
Prasyarat
Python
- Python 3.6 atau yang lebih baru
- pip alat pengelolaan paket
Library klien Google terbaru untuk Python. Untuk menginstal atau memperbaruinya, 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 Buat aplikasi Google Chat.
Otorisasi yang dikonfigurasi untuk aplikasi Chat. Membuat reaksi membutuhkan Autentikasi pengguna dengan
chat.messages.reactions.create
,chat.messages.reactions
, atau Cakupan otorisasichat.messages
.
Menambahkan reaksi ke pesan
Untuk membuat reaksi terhadap pesan, teruskan kode berikut dalam permintaan:
- Tentukan
chat.messages.reactions.create
,chat.messages.reactions
, atau Cakupan otorisasichat.messages
. - Panggil
Metode
create
di ResourceReaction
. - Setel
parent
ke nama resource pesan yang akan diberi reaksi. - Tetapkan
body
(isi permintaan) ke instanceReaction
dengan kolomunicode
sebagai emoji standar yang direpresentasikan oleh unicode {i>string<i}.
Contoh berikut bereaksi terhadap pesan dengan emoji 😀:
Python
- Di direktori kerja, buat file bernama
chat_reaction_create.py
. Sertakan kode berikut di
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()
Dalam kode, ganti kode berikut:
SPACE
:name
ruang tempat yang diposting, yang dapat Anda peroleh dari Metodespaces.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.
Dalam direktori kerja, build dan jalankan contoh:
python3 chat_reaction_create.py
Chat API menampilkan instance
Reaction
yang merinci reaksi yang dibuat.