ปักหมุดหรือเลิกปักหมุดข้อความในพื้นที่ใน Google Chat

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

ดูข้อมูลเกี่ยวกับวิธีที่ผู้ใช้ปักหมุดข้อความในพื้นที่ทำงานได้ที่หัวข้อปักหมุดข้อความ ไฟล์ & ลิงก์ในพื้นที่ทำงานและข้อความ

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

Node.js

ข้อควรพิจารณา

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

ปักหมุดข้อความ

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

  • ระบุขอบเขตการให้สิทธิ์ chat.spaces.pins หรือ chat.spaces
  • Gọi messagePins.create.
  • ระบุ parent (ชื่อพื้นที่ทำงาน) และระบุ body ที่มีชื่อทรัพยากร message ของข้อความต้นฉบับ

ตัวอย่างต่อไปนี้จะปักหมุดข้อความในพื้นที่ทำงาน

Python

  1. สร้างไฟล์ชื่อ chat_pin_message.py ในไดเรกทอรีงาน
  2. ใส่โค้ดต่อไปนี้ใน chat_pin_message.py

    from google_auth_oauthlib.flow import InstalledAppFlow
    from googleapiclient.discovery import build
    
    # Define your app's authorization scopes.
    SCOPES = ["https://www.googleapis.com/auth/chat.spaces.pins"]
    
    def main():
        '''
        Authenticates with Chat API via user credentials,
        then pins a message in a space.
        '''
    
        # Authenticate with Google Workspace and get user authorization.
        flow = InstalledAppFlow.from_client_secrets_file('credentials.json', SCOPES)
        creds = flow.run_local_server()
    
        # Build a service endpoint for Chat API.
        service = build('chat', 'v1', credentials=creds)
    
        # Pin a message.
        result = service.spaces().messagePins().create(
            # The space to pin the message in.
            #
            # Replace SPACE with a space ID or name.
            # Obtain the space name from the spaces resource of Chat API,
            # or from a space's URL.
            parent='spaces/SPACE',
    
            # The message to pin.
            body={
                'message': 'spaces/SPACE/messages/MESSAGE'
            }
        ).execute()
    
        # Print Chat API's response in your command line interface.
        print(result)
    
    if __name__ == '__main__':
        main()
    
  3. ในโค้ด ให้แทนที่ข้อมูลต่อไปนี้

    • SPACE: รหัสจาก name ของพื้นที่ทำงาน
    • MESSAGE: รหัสจาก name ของข้อความ
  4. สร้างและเรียกใช้ตัวอย่างในไดเรกทอรีงานโดยทำดังนี้

    python3 chat_pin_message.py

เลิกปักหมุดข้อความ

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

  • ระบุขอบเขตการให้สิทธิ์ chat.spaces.pins หรือ chat.spaces
  • Gọi messagePins.delete.
  • ตั้งค่า name เป็นชื่อทรัพยากรของ MessagePin ที่จะลบ

วิธีเลิกปักหมุดข้อความ

Python

  1. สร้างไฟล์ชื่อ chat_unpin_message.py ในไดเรกทอรีงาน
  2. ใส่โค้ดต่อไปนี้ใน chat_unpin_message.py

    from google_auth_oauthlib.flow import InstalledAppFlow
    from googleapiclient.discovery import build
    
    # Define your app's authorization scopes.
    SCOPES = ["https://www.googleapis.com/auth/chat.spaces.pins"]
    
    def main():
        '''
        Authenticates with Chat API via user credentials,
        then unpins a message from a space.
        '''
    
        # Authenticate with Google Workspace and get user authorization.
        flow = InstalledAppFlow.from_client_secrets_file('credentials.json', SCOPES)
        creds = flow.run_local_server()
    
        # Build a service endpoint for Chat API.
        service = build('chat', 'v1', credentials=creds)
    
        # Unpin a message.
        result = service.spaces().messagePins().delete(
            # The resource name of the message pin to delete.
            #
            # Replace SPACE with a space ID or name, and MESSAGE with the message ID.
            name='spaces/SPACE/messagePins/MESSAGE'
        ).execute()
    
        print(result)
    
    if __name__ == '__main__':
        main()
    
  3. ในโค้ด ให้แทนที่ข้อมูลต่อไปนี้

    • SPACE: รหัสจาก name ของพื้นที่ทำงาน
    • MESSAGE: รหัสจาก name ของข้อความ
  4. สร้างและเรียกใช้ตัวอย่างในไดเรกทอรีงานโดยทำดังนี้

    python3 chat_unpin_message.py

รับรายการข้อความที่ปักหมุดไว้

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

  • ระบุขอบเขตการอ่านรายการใดรายการหนึ่งต่อไปนี้ chat.spaces.pins.readonly, chat.spaces.readonly, chat.spaces.pins, หรือ chat.spaces
  • Gọi messagePins.list.
  • ระบุ parent เป็นชื่อพื้นที่ทำงานที่จะดึงข้อความที่ปักหมุดไว้

วิธีแสดงรายการข้อความที่ปักหมุดไว้

Python

  1. สร้างไฟล์ชื่อ chat_list_pinned_messages.py ในไดเรกทอรีงาน
  2. ใส่โค้ดต่อไปนี้ใน chat_list_pinned_messages.py

    from google_auth_oauthlib.flow import InstalledAppFlow
    from googleapiclient.discovery import build
    
    # Define your app's authorization scopes.
    SCOPES = ["https://www.googleapis.com/auth/chat.spaces.pins.readonly"]
    
    def main():
        '''
        Authenticates with Chat API via user credentials,
        then lists pinned messages in a space.
        '''
    
        # Authenticate with Google Workspace and get user authorization.
        flow = InstalledAppFlow.from_client_secrets_file('credentials.json', SCOPES)
        creds = flow.run_local_server()
    
        # Build a service endpoint for Chat API.
        service = build('chat', 'v1', credentials=creds)
    
        # List pinned messages.
        result = service.spaces().messagePins().list(
            # The space to list pinned messages from.
            #
            # Replace SPACE with a space ID or name.
            parent='spaces/SPACE'
        ).execute()
    
        print(result)
    
    if __name__ == '__main__':
        main()
    
  3. ในโค้ด ให้แทนที่ SPACE ด้วยรหัสพื้นที่ทำงานจาก name ของพื้นที่ทำงาน

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

    python3 chat_list_pinned_messages.py