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

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

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

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

Python

  • ธุรกิจหรือองค์กร บัญชี Google Workspace ที่มีสิทธิ์เข้าถึง Google Chat
  • Python 3.6 ขึ้นไป
  • เครื่องมือจัดการแพ็กเกจ pip
  • ไลบรารีของไคลเอ็นต์ Google ล่าสุด หากต้องการติดตั้งหรืออัปเดตส่วนขยาย เรียกใช้คำสั่งต่อไปนี้ในอินเทอร์เฟซบรรทัดคำสั่ง
    pip3 install --upgrade google-api-python-client google-auth-oauthlib
    

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

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

ตัวอย่างต่อไปนี้แสดงข้อความในพื้นที่ใน 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 the list of messages.
        print(result)
    
    if __name__ == '__main__':
        main()
    
  3. ในโค้ด ให้แทนที่ SPACE ด้วยชื่อพื้นที่ทำงาน ซึ่ง ที่คุณจะได้รับจาก spaces.list วิธี ใน Chat API หรือจาก URL ของพื้นที่ทำงาน

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

    python3 chat_messages_list.py
    

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