MessagePinทรัพยากร
ใน Chat API ช่วยให้แอปปักหมุดข้อความ ยกเลิกการปักหมุดข้อความ และ
รับรายการข้อความที่ปักหมุดทั้งหมดในพื้นที่ทำงานของ Google Chat ได้ ข้อความที่ปักหมุดจะ
แสดงในอินเทอร์เฟซของ Chat สำหรับสมาชิกทุกคนในพื้นที่ทำงาน API นี้ช่วยให้แอปจัดการข้อความที่ปักหมุดในนามของผู้ใช้ได้
ดูข้อมูลเกี่ยวกับวิธีที่ผู้ใช้ปักหมุดข้อความในพื้นที่ทำงานได้ที่หัวข้อปักหมุดข้อความ ไฟล์ และลิงก์ในพื้นที่ทำงานและข้อความ
ข้อกำหนดเบื้องต้น
Node.js
- บัญชี Google Workspace สำหรับธุรกิจหรือองค์กร ที่มีสิทธิ์เข้าถึง Google Chat
- ตั้งค่าสภาพแวดล้อม โดยทำดังนี้
- สร้างโปรเจ็กต์ Google Cloud
- กำหนดค่าหน้าจอขอความยินยอม OAuth
- เปิดใช้และกำหนดค่า Google Chat API พร้อมชื่อ ไอคอน และคำอธิบายสำหรับแอป Chat
- ติดตั้ง Node.js Cloud Client Library
-
สร้างข้อมูลเข้าสู่ระบบรหัสไคลเอ็นต์ OAuth สำหรับแอปพลิเคชันบนเดสก์ท็อป หากต้องการเรียกใช้ตัวอย่างในคำแนะนำนี้
ให้บันทึกข้อมูลเข้าสู่ระบบเป็นไฟล์ JSON ชื่อ
credentials.jsonใน ไดเรกทอรีในเครื่อง
- เลือกขอบเขตการให้สิทธิ์ที่รองรับการตรวจสอบสิทธิ์ผู้ใช้
ข้อควรพิจารณา
- คุณปักหมุดได้เฉพาะข้อความที่มีอยู่ในพื้นที่ทำงานอยู่แล้วเท่านั้น คุณสร้างข้อความใหม่และปักหมุดในคำขอเดียวกันไม่ได้
- คุณปักหมุดข้อความที่แสดงเฉพาะต่อคุณไม่ได้ เช่น คุณปักหมุดข้อความส่วนตัวที่ได้รับจากแอปไม่ได้
- พื้นที่ใน Chat แต่ละแห่งมีข้อความที่ปักหมุดได้สูงสุด 100 รายการ หากแอปพยายามปักหมุดข้อความที่ 101 API จะแสดงข้อผิดพลาด
การปักหมุดข้อความ
หากต้องการปักหมุดข้อความด้วยการตรวจสอบสิทธิ์ผู้ใช้ ให้ส่งข้อมูลต่อไปนี้ในคำขอ
- ระบุขอบเขตการให้สิทธิ์
chat.spaces.pinsหรือchat.spaces - โทรหา
messagePins.create - ระบุ
parent(ชื่อพื้นที่ทำงาน) และระบุbodyพร้อมmessageชื่อทรัพยากรของข้อความต้นฉบับ
ตัวอย่างต่อไปนี้ปักหมุดข้อความในพื้นที่ทำงาน
Python
- สร้างไฟล์ชื่อ
chat_pin_message.pyในไดเรกทอรีการทำงาน ใส่โค้ดต่อไปนี้ใน
chat_pin_message.pyfrom 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()ในโค้ด ให้แทนที่ค่าต่อไปนี้
ในไดเรกทอรีการทำงาน ให้สร้างและเรียกใช้ตัวอย่างโดยทำดังนี้
python3 chat_pin_message.py
เลิกปักหมุดข้อความ
หากต้องการเลิกปักหมุดข้อความด้วยการตรวจสอบสิทธิ์ผู้ใช้ ให้ส่งข้อมูลต่อไปนี้ในคำขอ
- ระบุขอบเขตการให้สิทธิ์
chat.spaces.pinsหรือchat.spaces - โทรหา
messagePins.delete - ตั้งค่า
nameเป็นชื่อทรัพยากรของMessagePinที่จะลบ
วิธีเลิกปักหมุดข้อความมีดังนี้
Python
- สร้างไฟล์ชื่อ
chat_unpin_message.pyในไดเรกทอรีการทำงาน ใส่โค้ดต่อไปนี้ใน
chat_unpin_message.pyfrom 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()ในโค้ด ให้แทนที่ค่าต่อไปนี้
ในไดเรกทอรีการทำงาน ให้สร้างและเรียกใช้ตัวอย่างโดยทำดังนี้
python3 chat_unpin_message.py
ดูรายการข้อความที่ปักหมุด
หากต้องการดูรายการข้อความที่ปักหมุดในพื้นที่ทำงานที่คุณมีสิทธิ์เข้าถึง ให้ใช้การตรวจสอบสิทธิ์ผู้ใช้ และ ส่งข้อมูลต่อไปนี้ในคำขอ
- ระบุขอบเขตการอ่านอย่างใดอย่างหนึ่ง ได้แก่
chat.spaces.pins.readonly,chat.spaces.readonly,chat.spaces.pinsหรือchat.spaces - โทรหา
messagePins.list - ระบุ
parentเป็นชื่อพื้นที่ทำงานเพื่อดึงข้อความที่ปักหมุด
วิธีแสดงข้อความที่ปักหมุดมีดังนี้
Python
- สร้างไฟล์ชื่อ
chat_list_pinned_messages.pyในไดเรกทอรีการทำงาน ใส่โค้ดต่อไปนี้ใน
chat_list_pinned_messages.pyfrom 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()ในโค้ด ให้แทนที่
SPACEด้วยรหัสพื้นที่จากnameของพื้นที่ในไดเรกทอรีการทำงาน ให้สร้างและเรียกใช้ตัวอย่างโดยทำดังนี้
python3 chat_list_pinned_messages.py
หัวข้อที่เกี่ยวข้อง
- จัดรูปแบบข้อความ
- ลบข้อความ
- ดูรายละเอียดเกี่ยวกับข้อความ
- แสดงรายการข้อความในพื้นที่ทำงาน
- อัปเดตข้อความ
- ส่งข้อความ