แสดงรายการกิจกรรมจากพื้นที่ใน Google Chat

คู่มือนี้อธิบายวิธีใช้เมธอด list() ในทรัพยากร SpaceEvent ของ Google Chat API เพื่อแสดงการเปลี่ยนแปลงทรัพยากร ในพื้นที่ทำงาน

SpaceEventทรัพยากร แสดงถึงการเปลี่ยนแปลงพื้นที่เป้าหมาย รวมถึงทรัพยากรย่อยของพื้นที่ เช่น ข้อความ รีแอ็กชัน และการเป็นสมาชิก ดูข้อมูลเพิ่มเติมเกี่ยวกับ รายการประเภทเหตุการณ์และเพย์โหลดเหตุการณ์ที่รองรับได้ที่ฟิลด์ eventType และ payload ของเอกสารประกอบข้อมูลอ้างอิงทรัพยากร SpaceEvent

คุณแสดงกิจกรรมได้สูงสุด 28 วันก่อนเวลาที่ส่งคำขอ เซิร์ฟเวอร์ จะแสดงเหตุการณ์ที่มีทรัพยากรที่ได้รับผลกระทบเวอร์ชันล่าสุด เช่น หากคุณแสดงกิจกรรมเกี่ยวกับสมาชิกใหม่ในพื้นที่ทำงาน เซิร์ฟเวอร์จะแสดงทรัพยากร Membership ที่มีรายละเอียดการเป็นสมาชิกล่าสุด หากมีการนำสมาชิกใหม่ ออกในช่วงระยะเวลาที่ขอ เพย์โหลดของเหตุการณ์จะมีทรัพยากร Membership ที่ว่างเปล่า

หากต้องการแสดงกิจกรรมจากพื้นที่ทำงาน ผู้ใช้ที่ได้รับการตรวจสอบสิทธิ์หรือ แอป Chat ต้องเป็นสมาชิกของพื้นที่ทำงาน

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

Node.js

  • บัญชี Google Workspace สำหรับธุรกิจหรือองค์กร ที่มีสิทธิ์เข้าถึง Google Chat

Python

  • บัญชี Google Workspace สำหรับธุรกิจหรือองค์กร ที่มีสิทธิ์เข้าถึง Google Chat

แสดงรายการเหตุการณ์ในพื้นที่ (การตรวจสอบสิทธิ์ผู้ใช้)

หากต้องการแสดงกิจกรรมในพื้นที่ใน Chat ให้ส่งข้อมูลต่อไปนี้ในคำขอ

  • ระบุขอบเขตการให้สิทธิ์อย่างน้อย 1 รายการเพื่อรองรับเหตุการณ์แต่ละประเภทในคำขอ แนวทางปฏิบัติแนะนำคือเลือกขอบเขตที่จำกัดที่สุดซึ่งยังคง อนุญาตให้แอปทำงานได้ หากต้องการเลือกขอบเขต โปรดดูภาพรวมการตรวจสอบสิทธิ์และการให้สิทธิ์

  • เรียกใช้เมธอด ListSpaceEvents() โดยส่ง filter ของประเภทเหตุการณ์ที่จะแสดง คุณต้องระบุประเภทเหตุการณ์อย่างน้อย 1 ประเภท และยังกรองตามวันที่ได้ด้วย ดูรายการประเภทกิจกรรมที่รองรับได้ในเอกสารอ้างอิงของฟิลด์ eventType ของทรัพยากร SpaceEvent

ตัวอย่างต่อไปนี้แสดงรายการเหตุการณ์เกี่ยวกับการเป็นสมาชิกใหม่และข้อความใน พื้นที่

Node.js

chat/client-libraries/cloud/list-space-events-user-cred.js
import {createClientWithUserCredentials} from './authentication-utils.js';

// Authorization scopes based on the event types
const USER_AUTH_OAUTH_SCOPES = [
  'https://www.googleapis.com/auth/chat.memberships.readonly',
  'https://www.googleapis.com/auth/chat.messages.readonly',
];

// This sample shows how to list space events with user credential
async function main() {
  // Create a client
  const chatClient = await createClientWithUserCredentials(
    USER_AUTH_OAUTH_SCOPES,
  );

  // Initialize request argument(s)
  const request = {
    // Replace SPACE_NAME here
    parent: 'spaces/SPACE_NAME',
    // A required filter. Filters events about new memberships and messages.
    filter:
      'eventTypes:"google.workspace.chat.membership.v1.created" OR eventTypes:"google.workspace.chat.message.v1.created"',
  };

  // Make the request
  const pageResult = chatClient.listSpaceEventsAsync(request);

  // Handle the response. Iterating over pageResult will yield results and
  // resolve additional pages automatically.
  for await (const response of pageResult) {
    console.log(response);
  }
}

await main();

หากต้องการเรียกใช้ตัวอย่างนี้ ให้แทนที่ SPACE_NAME ด้วยรหัสจาก พื้นที่ name คุณรับรหัสได้โดยการเรียกใช้เมธอด ListSpaces() หรือจาก URL ของพื้นที่ทำงาน

Chat API จะแสดงรายการ รายการเหตุการณ์ในพื้นที่ทำงานที่แบ่งหน้า เกี่ยวกับการเป็นสมาชิกและข้อความใหม่

แสดงเหตุการณ์ในพื้นที่ทำงาน (การตรวจสอบสิทธิ์แอป Chat)

การตรวจสอบสิทธิ์แอปต้องมีการอนุมัติจากผู้ดูแลระบบแบบครั้งเดียว

หากต้องการแสดงกิจกรรมในพื้นที่ทำงานจากพื้นที่ทำงานที่มี การตรวจสอบสิทธิ์แอปและ Chat REST API ให้ส่ง ข้อมูลต่อไปนี้ในคำขอ

  • ระบุขอบเขตการให้สิทธิ์อย่างน้อย 1 รายการเพื่อรองรับเหตุการณ์แต่ละประเภทในคำขอ แนวทางปฏิบัติแนะนำคือเลือกขอบเขตที่จำกัดที่สุดซึ่งยังคง อนุญาตให้แอปทำงานได้ ดูข้อมูลเพิ่มเติมเกี่ยวกับการเลือกขอบเขตได้ที่ภาพรวมการตรวจสอบสิทธิ์และการให้สิทธิ์
    • https://www.googleapis.com/auth/chat.app.memberships
    • https://www.googleapis.com/auth/chat.app.memberships.readonly
    • https://www.googleapis.com/auth/chat.app.messages.readonly
    • https://www.googleapis.com/auth/chat.app.spaces
    • https://www.googleapis.com/auth/chat.app.spaces.readonly
  • เรียกใช้เมธอด list ในspaceEventsทรัพยากร
  • ส่ง name ของพื้นที่ทำงานเพื่อแสดงรายการข้อความ
  • ส่ง filter เพื่อค้นหาประเภทเหตุการณ์ที่เฉพาะเจาะจง

เขียนสคริปต์ที่เรียกใช้ Chat API

วิธีแสดงกิจกรรมในพื้นที่โดยใช้การตรวจสอบสิทธิ์ของแอปและ Chat REST API มีดังนี้

Python

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

    from google.oauth2 import service_account
    from apiclient.discovery import build
    
    # Define your app's authorization scopes.
    # Set authorization scopes based on the
    # event type. For example, if you are getting a space event
    # about a new membership, use the `chat.app.memberships` scope.
    #
    # When modifying these scopes, delete the file token.json, if it exists.
    SCOPES = ["https://www.googleapis.com/auth/chat.app.memberships",
              "https://www.googleapis.com/auth/chat.app.memberships.readonly",
              "https://www.googleapis.com/auth/chat.app.messages.readonly",
              "https://www.googleapis.com/auth/chat.app.spaces",
              "https://www.googleapis.com/auth/chat.app.spaces.readonly"]
    
    def main():
        '''
        Authenticates with Chat API using app authentication,
        then lists space events from a specified space.
        '''
    
        # Specify service account details.
        creds = (
            service_account.Credentials.from_service_account_file('credentials.json')
            .with_scopes(SCOPES)
        )
    
        # Build a service endpoint for Chat API.
        chat = build('chat', 'v1', credentials=creds)
    
        # Use the service endpoint to call Chat API.
        result = chat.spaces().spaceEvents().list(
    
            # The space to list events from.
            #
            # Replace SPACE_NAME with a space name.
            # Obtain the space name from the spaces resource of Chat API,
            # or from a space's URL.
            parent='spaces/SPACE_NAME',
    
            # A required filter. Filters events by event type.
            #
            # Update this filter to match your requirements.
            filter='eventTypes:"google.workspace.chat.message.v1.created"'
    
        ).execute()
    
        # Print Chat API's response in your command line interface.
        print(result)
    
    if __name__ == '__main__':
        main()
    
  3. ในโค้ด ให้แทนที่ค่าต่อไปนี้

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

    python3 chat_spaceevents_list_app.py

Chat API จะแสดงรายการ รายการเหตุการณ์ในพื้นที่ทำงานที่แบ่งหน้า เกี่ยวกับการเป็นสมาชิกและข้อความใหม่