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

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

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

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

Node.js

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

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

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

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

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

  • ระบุขอบเขตการให้สิทธิ์ chat.spaces.pins หรือ chat.spaces
  • โทรหา 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
  • โทรหา 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
  • โทรหา 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