ลบข้อความ

คำแนะนำนี้จะอธิบายวิธีใช้เมธอด delete ในแหล่งข้อมูล Message ของ Google Chat API เพื่อลบ SMS หรือข้อความในการ์ด

แหล่งข้อมูล Message รายการ แสดงถึง ข้อความ หรือ การ์ด ใน Google Chat คุณสามารถ create, get, update หรือ delete ข้อความใน Google Chat API โดยการเรียกใช้ ที่สัมพันธ์กัน หากต้องการเรียนรู้เพิ่มเติมเกี่ยวกับข้อความ SMS และการ์ด โปรดดูที่ ภาพรวมข้อความ Google Chat

ข้อกำหนดเบื้องต้น

Python

  • Python 3.6 ขึ้นไป
  • PIP เครื่องมือจัดการแพ็กเกจ
  • ไลบรารีของไคลเอ็นต์ Google ล่าสุดสำหรับ Python หากต้องการติดตั้งหรืออัปเดตส่วนขยาย เรียกใช้คำสั่งต่อไปนี้ในอินเทอร์เฟซบรรทัดคำสั่ง

    pip3 install --upgrade google-api-python-client google-auth-oauthlib google-auth
    
  • โปรเจ็กต์ Google Cloud ที่เปิดใช้และกำหนดค่า Google Chat API โปรดดูขั้นตอนในหัวข้อ สร้างแอป Google Chat
  • กำหนดค่าการให้สิทธิ์สำหรับแอป Chat แล้ว กำลังลบ ข้อความจะรองรับการตรวจสอบสิทธิ์ทั้ง 2 วิธีต่อไปนี้

ลบข้อความที่มีการตรวจสอบสิทธิ์ผู้ใช้

หากต้องการลบข้อความที่มีการตรวจสอบสิทธิ์ผู้ใช้ ให้ทำดังนี้ ส่งต่อสิ่งต่อไปนี้ในคำขอของคุณ

  • ระบุขอบเขตการให้สิทธิ์ chat.messages
  • เรียกใช้ delete วิธี ใน แหล่งข้อมูล Message
  • ตั้งค่า name เป็นชื่อทรัพยากรของข้อความที่จะลบ

ตัวอย่างต่อไปนี้ลบข้อความที่มี การตรวจสอบสิทธิ์ผู้ใช้:

Python

  1. ในไดเรกทอรีการทำงาน ให้สร้างไฟล์ชื่อ chat_message_delete_user.py
  2. รวมรหัสต่อไปนี้ใน chat_message_delete_user.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"]
    
    def main():
        '''
        Authenticates with Chat API via user credentials,
        then deletes 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().delete(
    
            # The message 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.
            name = 'spaces/SPACE/messages/MESSAGE'
    
        ).execute()
    
        # Prints response to the Chat API call.
        # When deleting a message, the response body is empty.
        print(result)
    
    if __name__ == '__main__':
        main()
    
  3. ในโค้ด ให้แทนที่

    • SPACE: ชื่อพื้นที่ทำงานซึ่งดูได้จาก เวลา spaces.list วิธี ใน Chat API หรือจาก URL ของพื้นที่ทำงาน
    • MESSAGE: ชื่อข้อความซึ่งคุณจะดูได้ จากเนื้อหาการตอบกลับที่ส่งคืนหลังจากสร้างข้อความแบบไม่พร้อมกัน ด้วย Chat API หรือ ชื่อที่กำหนดเอง ที่กำหนดให้กับข้อความขณะสร้าง
  4. ในไดเรกทอรีการทำงาน ให้สร้างและเรียกใช้ตัวอย่างด้วยคำสั่งต่อไปนี้

    python3 chat_message_delete_user.py
    

หากสำเร็จ เนื้อหาการตอบกลับจะว่างเปล่า ซึ่งระบุว่าข้อความ ลบแล้ว

ลบข้อความที่มีการตรวจสอบสิทธิ์แอป

เมื่อต้องการลบข้อความที่มี การตรวจสอบสิทธิ์แอป ให้ผ่านการตรวจสอบ ต่อไปนี้ในคำขอของคุณ

ตัวอย่างต่อไปนี้ลบข้อความที่มี การตรวจสอบสิทธิ์แอป:

Python

  1. ในไดเรกทอรีการทำงาน ให้สร้างไฟล์ชื่อ chat_delete_message_app.py
  2. รวมรหัสต่อไปนี้ใน chat_delete_message_app.py:

    from google.oauth2 import service_account
    from apiclient.discovery import build
    
    # Specify required scopes.
    SCOPES = ['https://www.googleapis.com/auth/chat.bot']
    
    # Specify service account details.
    CREDENTIALS = (
        service_account.Credentials.from_service_account_file('credentials.json')
        .with_scopes(SCOPES)
    )
    
    # Build the URI and authenticate with the service account.
    chat = build('chat', 'v1', credentials=CREDENTIALS)
    
    # Delete a Chat message.
    result = chat.spaces().messages().delete(
    
      # The message 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.
      name='spaces/SPACE/messages/MESSAGE'
    
    ).execute()
    
    # Print Chat API's response in your command line interface.
    # When deleting a message, the response body is empty.
    print(result)
    
  3. ในโค้ด ให้แทนที่

    • SPACE: name ของพื้นที่ทำงานที่ โพสต์ข้อความไปแล้ว ซึ่งคุณสามารถดูได้จาก เมธอด spaces.list ใน Chat API หรือจาก URL ของพื้นที่ทำงาน
    • MESSAGE: ชื่อข้อความ ซึ่งคุณสามารถดูได้จาก จากเนื้อหาการตอบกลับที่ส่งคืนหลังจากสร้างข้อความแบบไม่พร้อมกัน ด้วย Chat API หรือ ชื่อที่กำหนดเอง ที่กำหนดให้กับข้อความขณะสร้าง
  4. ในไดเรกทอรีการทำงาน ให้สร้างและเรียกใช้ตัวอย่างด้วยคำสั่งต่อไปนี้

    python3 chat_delete_message_app.py
    

หากสำเร็จ เนื้อหาการตอบกลับจะว่างเปล่า ซึ่งระบุว่าข้อความ ลบแล้ว