แสดงรายการข้อความ

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

แหล่งข้อมูล 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 Cloud ที่เปิดใช้และกำหนดค่า Google Chat API โปรดดูขั้นตอนที่หัวข้อสร้างแอป Google Chat
  • กำหนดค่าการให้สิทธิ์สำหรับแอป Chat แล้ว ข้อความที่แสดงต้องมีการตรวจสอบสิทธิ์ผู้ใช้ที่มีขอบเขตการให้สิทธิ์ chat.messages.readonly หรือ chat.messages

แสดงข้อความ

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

ตัวอย่างต่อไปนี้แสดงข้อความจากพื้นที่ใน Chat ที่ส่งหลังวันที่ 16 มีนาคม 2023

Python

  1. สร้างไฟล์ชื่อ chat_messages_list.py ในไดเรกทอรีการทำงาน
  2. ใส่รหัสต่อไปนี้ใน chat_messages_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.readonly"]
    
    def main():
        '''
        Authenticates with Chat API via user credentials,
        then lists messages in a space sent after March 16, 2023.
        '''
    
        # 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().list(
    
              # The space for which to list messages.
              parent = 'spaces/SPACE',
    
              # An optional filter that returns messages
              # created after March 16, 2023.
              filter = 'createTime > "2023-03-16T00:00:00-00:00"'
    
          ).execute()
    
        # Prints details about the created membership.
        print(result)
    
    if __name__ == '__main__':
        main()
    
  3. ในโค้ด ให้แทนที่ SPACE ด้วยชื่อพื้นที่ทำงานซึ่งคุณดูได้จากเมธอด spaces.list ใน Chat API หรือจาก URL ของพื้นที่ทำงาน

  4. สร้างและเรียกใช้ตัวอย่างในไดเรกทอรีการทำงาน

    python3 chat_messages_list.py
    

Google Chat API จะแสดงรายการข้อความที่ส่งในพื้นที่ทำงานที่ระบุหลังวันที่ 16 มีนาคม 2023