ดูรายละเอียดเกี่ยวกับกิจกรรมในพื้นที่ใน Google Chat

คู่มือนี้อธิบายวิธีใช้ get() เมธอดในทรัพยากร SpaceEvent ของ Google Chat API เพื่อดูรายละเอียดเกี่ยวกับ เหตุการณ์จากพื้นที่ใน Google Chat

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

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

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

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

Node.js

  • บัญชี Google Workspace for Business หรือ Enterprise ที่มีสิทธิ์เข้าถึง Google Chat

Python

  • บัญชี Google Workspace for Business หรือ Enterprise ที่มีสิทธิ์เข้าถึง Google Chat

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

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

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

ตัวอย่างต่อไปนี้แสดงวิธีดูเหตุการณ์ในพื้นที่ทำงาน

Node.js

ตัวอย่างโค้ด Node.js นี้ใช้ Chat RPC API

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

// Replace SCOPE_NAME here with an authorization scope based on the event type
const USER_AUTH_OAUTH_SCOPES = ['SCOPE_NAME'];

// This sample shows how to get space event 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 and SPACE_EVENT_NAME here
    name: 'spaces/SPACE_NAME/spaceEvents/SPACE_EVENT_NAME',
  };

  // Make the request
  const response = await chatClient.getSpaceEvent(request);

  // Handle the response
  console.log(response);
}

await main();

หากต้องการเรียกใช้ตัวอย่างนี้ ให้แทนที่ข้อมูลต่อไปนี้

  • SCOPE_NAME: ขอบเขตการให้สิทธิ์ตามประเภทเหตุการณ์ ตัวอย่างเช่น หากคุณกำลังดูเหตุการณ์ในพื้นที่ทำงาน เกี่ยวกับการเป็นสมาชิกใหม่ ให้ใช้ขอบเขต chat.memberships.readonly ที่จัดรูปแบบเป็น https://www.googleapis.com/auth/chat.memberships.readonly คุณสามารถรับประเภทเหตุการณ์จาก ListSpaceEvents() เมธอด หากต้องการดูวิธีใช้เมธอดนี้ โปรดดูหัวข้อ แสดงเหตุการณ์จากพื้นที่ทำงาน
  • SPACE_NAME: รหัสจาก name ของพื้นที่ทำงาน คุณสามารถรับรหัสได้โดยเรียก ListSpaces() เมธอดหรือจาก URL ของพื้นที่ทำงาน
  • SPACE_EVENT_NAME: รหัสจากเหตุการณ์ในพื้นที่ทำงาน name คุณสามารถรับรหัสได้จาก ListSpaceEvents() เมธอด หากต้องการดูวิธีใช้เมธอดนี้ โปรดดูหัวข้อ แสดงเหตุการณ์จากพื้นที่ทำงาน

Chat API จะแสดงอินสแตนซ์ของ SpaceEvent พร้อมรายละเอียดเกี่ยวกับเหตุการณ์

ดูรายละเอียดเกี่ยวกับเหตุการณ์ในพื้นที่ทำงาน (การตรวจสอบสิทธิ์แอป 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
  • เรียกเมธอด get ในทรัพยากร spaceEvents
  • ส่ง name ของพื้นที่ทำงานที่ต้องการดูรายละเอียดเหตุการณ์

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

วิธีดูรายละเอียดเกี่ยวกับเหตุการณ์ในพื้นที่ทำงานด้วย การตรวจสอบสิทธิ์แอปและ Chat REST API:

Python

ตัวอย่างโค้ด Python นี้ใช้ Chat REST API

  1. สร้างไฟล์ชื่อ chat_spaceevents_get_app.py ในไดเรกทอรีงาน
  2. ใส่โค้ดต่อไปนี้ใน chat_spaceevents_get_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().get(
    
            # The space to get event details 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.
            name='spaces/SPACE_NAME/spaceEvents/SPACE_EVENT_NAME',
    
        ).execute()
    
        # Print Chat API's response in your command line interface.
        print(result)
    
    if __name__ == '__main__':
        main()
    
  3. ในโค้ด ให้แทนที่ข้อมูลต่อไปนี้

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

    python3 chat_spaceevents_get_app.py

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