คู่มือนี้อธิบายวิธีใช้เมธอด delete
ในทรัพยากร Reaction
ของ Google Chat API เพื่อลบความรู้สึกออกจากข้อความ เช่น 👍, 🚲 และ 🌞
การลบความรู้สึกไม่ได้เป็นการลบข้อความ
แหล่งข้อมูล Reaction
แสดงถึงอีโมจิที่ผู้คนใช้แสดงความรู้สึกต่อข้อความได้ เช่น 👍, 🚲 และ 🌞
ข้อกำหนดเบื้องต้น
Python
- Python 3.6 ขึ้นไป
- เครื่องมือการจัดการแพ็กเกจ pip
ไลบรารีของไคลเอ็นต์ Google ล่าสุดสำหรับ Python หากต้องการติดตั้งหรืออัปเดต ให้เรียกใช้คำสั่งต่อไปนี้ในอินเทอร์เฟซบรรทัดคำสั่ง
pip3 install --upgrade google-api-python-client google-auth-httplib2 google-auth-oauthlib oauth2client
แอป Chat ที่เผยแพร่แล้ว หากต้องการสร้างและเผยแพร่ แอป Chat โปรดดูที่ สร้างแอป Google Chat
มีการกำหนดค่าการให้สิทธิ์สำหรับแอป Chat แล้ว การลบความรู้สึกจะต้องมีการตรวจสอบสิทธิ์ผู้ใช้ที่มีขอบเขตการให้สิทธิ์
chat.messages.reactions
หรือchat.messages
ลบความรู้สึก
หากต้องการลบความรู้สึกออกจากข้อความ ให้ส่งข้อมูลต่อไปนี้ในคำขอ
- ระบุขอบเขตการให้สิทธิ์
chat.messages.reactions
หรือchat.messages
- เรียกใช้เมธอด
delete
ในทรัพยากรReaction
- กำหนด
name
เป็นชื่อทรัพยากรของความรู้สึกที่จะลบ
ตัวอย่างต่อไปนี้จะลบความรู้สึก 😀 ออกจากข้อความ:
Python
- ในไดเรกทอรีการทำงาน ให้สร้างไฟล์ชื่อ
chat_reaction_delete.py
ใส่รหัสต่อไปนี้ใน
chat_reaction_delete.py
:import os.path from google.auth.transport.requests import Request from google.oauth2.credentials import Credentials from google_auth_oauthlib.flow import InstalledAppFlow from googleapiclient.discovery import build from googleapiclient.errors import HttpError # 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()
ในโค้ด ให้แทนที่:
SPACE
: ชื่อพื้นที่ทำงาน ซึ่งหาได้จากเมธอดspaces.list
ใน Chat API หรือจาก URL ของพื้นที่ทำงานMESSAGE
: ชื่อข้อความที่คุณจะได้รับจากส่วนเนื้อหาการตอบกลับหลังจากที่สร้างข้อความแบบไม่พร้อมกันด้วย Chat API หรือด้วยชื่อที่กำหนดเองที่กำหนดให้กับข้อความเมื่อสร้างREACTION
: ชื่อความรู้สึกซึ่งใช้ได้จากเมธอดspaces.messages.reactions.list
ใน Chat API หรือจากส่วนเนื้อหาของการตอบกลับที่แสดงขึ้นหลังจากสร้างความรู้สึกแบบไม่พร้อมกันด้วย Chat API
ในไดเรกทอรีการทำงาน ให้สร้างและเรียกใช้ตัวอย่าง:
python3 chat_reaction_delete.py
หากทำสำเร็จ เนื้อหาการตอบกลับจะว่างเปล่าซึ่งจะระบุว่าระบบลบความรู้สึกแล้ว