คำแนะนำนี้จะอธิบายวิธีใช้เมธอด create
ในทรัพยากร Reaction
ของ Google Chat API เพื่อเพิ่มความรู้สึกที่มีต่อข้อความ เช่น 👍, 🚲 และ 🌞
แหล่งข้อมูล Reaction
แสดงถึงอีโมจิที่ผู้คนใช้เพื่อแสดงความรู้สึกต่อข้อความได้ เช่น 👍, 🚲
และ 🌞
สิ่งที่ต้องดำเนินการก่อน
Python
- Python 3.6 ขึ้นไป
- เครื่องมือจัดการแพ็กเกจ pip
ไลบรารีของไคลเอ็นต์ Google ล่าสุดสำหรับ Python หากต้องการติดตั้งหรืออัปเดต ให้เรียกใช้คำสั่งต่อไปนี้ในอินเทอร์เฟซบรรทัดคำสั่ง
pip3 install --upgrade google-api-python-client google-auth-oauthlib
- โปรเจ็กต์ Google Cloud ที่เปิดใช้และกำหนดค่า Google Chat API โปรดดูขั้นตอนในหัวข้อสร้างแอป Google Chat
การกำหนดค่าการให้สิทธิ์สำหรับแอป Chat การสร้างรีแอ็กชันต้องมีการตรวจสอบสิทธิ์ผู้ใช้ด้วยขอบเขตการให้สิทธิ์
chat.messages.reactions.create
,chat.messages.reactions
หรือchat.messages
เพิ่มรีแอ็กชันต่อข้อความ
หากต้องการสร้างรีแอ็กชันต่อข้อความ ให้ส่งข้อมูลต่อไปนี้ในคำขอ
- ระบุขอบเขตการให้สิทธิ์
chat.messages.reactions.create
,chat.messages.reactions
หรือchat.messages
- เรียกใช้เมธอด
create
ในทรัพยากรReaction
- ตั้งค่า
parent
เป็นชื่อทรัพยากรของข้อความเพื่อแสดงความรู้สึก - ตั้งค่า
body
(เนื้อหาของคำขอ) เป็นอินสแตนซ์ของReaction
โดยที่ช่องunicode
เป็นอีโมจิมาตรฐานที่แสดงด้วยสตริง Unicode
ตัวอย่างต่อไปนี้จะแสดงความรู้สึกต่อข้อความด้วยอีโมจิ 😀
Python
- สร้างไฟล์ชื่อ
chat_reaction_create.py
ในไดเรกทอรีการทำงาน รวมรหัสต่อไปนี้ใน
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()
ในโค้ด ให้แทนที่
SPACE
:name
ของพื้นที่ทำงานที่มีการโพสต์ข้อความ ซึ่งคุณจะรับได้จาก Methodspaces.list
ใน Chat API หรือจาก URL ของพื้นที่ทำงานMESSAGE
: ชื่อข้อความที่ได้รับจากเนื้อหาการตอบกลับที่ส่งคืนหลังจากสร้างข้อความแบบไม่พร้อมกันด้วย Chat API หรือด้วยชื่อที่กำหนดเองที่กำหนดให้กับข้อความขณะสร้าง
ในไดเรกทอรีการทำงาน ให้สร้างและเรียกใช้ตัวอย่างด้วยคำสั่งต่อไปนี้
python3 chat_reaction_create.py
Chat API จะแสดงผลอินสแตนซ์ของ Reaction
ที่แสดงรายละเอียดรีแอ็กชันที่สร้างขึ้น