คู่มือนี้อธิบายวิธีใช้เมธอด create
ในทรัพยากร membership
ของ Google Chat API เพื่อเชิญหรือเพิ่มผู้ใช้หรือแอป Chat ไปยังพื้นที่ทำงานหรือที่เรียกว่าการสร้างการเป็นสมาชิก เมื่อสร้างการเป็นสมาชิก หากสมาชิกที่ระบุปิดใช้นโยบายการยอมรับอัตโนมัติไว้ ก็จะได้รับเชิญและต้องตอบรับคำเชิญเข้าร่วมพื้นที่ทำงานก่อนจึงจะเข้าร่วมได้ ไม่เช่นนั้น การสร้างการเป็นสมาชิกจะเพิ่มสมาชิกไปยังพื้นที่ทำงานที่ระบุโดยตรง
แหล่งข้อมูลของ Membership
แสดงให้เห็นว่าผู้ใช้ที่เป็นมนุษย์หรือแอป Google Chat ได้รับเชิญหรือไม่เข้าร่วม บ้าง หรือไม่ได้อยู่ในพื้นที่ทำงาน
ข้อกำหนดเบื้องต้น
Python
- Python 3.6 ขึ้นไป
- เครื่องมือการจัดการแพ็กเกจ pip
ไลบรารีของไคลเอ็นต์ Google ล่าสุดสำหรับ Python หากต้องการติดตั้งหรืออัปเดต ให้เรียกใช้คำสั่งต่อไปนี้ในอินเทอร์เฟซบรรทัดคำสั่ง
pip3 install --upgrade google-api-python-client google-auth-oauthlib
- โปรเจ็กต์ Google Cloud ที่เปิดใช้และกำหนดค่า Google Chat API โปรดดูขั้นตอนที่หัวข้อสร้างแอป Google Chat
กำหนดค่าการให้สิทธิ์สำหรับแอป Chat แล้ว การสร้างการเป็นสมาชิกต้องมีการตรวจสอบสิทธิ์ผู้ใช้ที่มีขอบเขตการให้สิทธิ์
chat.memberships
หรือchat.memberships.app
Node.js
- Node.js และ npm
ไลบรารีของไคลเอ็นต์ Google ล่าสุดสำหรับ Node.js หากต้องการติดตั้ง ให้เรียกใช้คำสั่งต่อไปนี้ในอินเทอร์เฟซบรรทัดคำสั่ง
npm install @google-cloud/local-auth @googleapis/chat
- โปรเจ็กต์ Google Cloud ที่เปิดใช้และกำหนดค่า Google Chat API โปรดดูขั้นตอนที่หัวข้อสร้างแอป Google Chat
กำหนดค่าการให้สิทธิ์สำหรับแอป Chat แล้ว การสร้างการเป็นสมาชิกต้องมีการตรวจสอบสิทธิ์ผู้ใช้ที่มีขอบเขตการให้สิทธิ์
chat.memberships
หรือchat.memberships.app
เชิญหรือเพิ่มผู้ใช้ในพื้นที่ทำงาน
หากต้องการเชิญหรือเพิ่มผู้ใช้ไปยังพื้นที่ทำงาน ให้ส่งข้อมูลต่อไปนี้ในคำขอ
- ระบุขอบเขตการให้สิทธิ์
chat.memberships
- เรียกเมธอด
create
ในทรัพยากรmembership
- ตั้งค่า
parent
เป็นชื่อทรัพยากรของพื้นที่ทำงานที่จะสร้างการเป็นสมาชิก - ตั้งค่า
member
เป็นusers/{user}
โดยที่{user}
คือบุคคลที่คุณต้องการสร้างการเป็นสมาชิกให้ และเป็นบุคคลใดบุคคลหนึ่งต่อไปนี้- รหัสสำหรับบุคคลใน People API เช่น หากบุคคล
resourceName
ของ People API คือpeople/123456789
ให้กำหนดmembership.member.name
เป็นusers/123456789
- รหัสของผู้ใช้ใน Directory API
- อีเมลของผู้ใช้ ตัวอย่างเช่น
users/222larabrown@gmail.com
หรือusers/larabrown@cymbalgroup.com
หากผู้ใช้ใช้บัญชี Google หรือเป็นองค์กร Google Workspace อื่น คุณต้องใช้อีเมลของบุคคลนั้น
- รหัสสำหรับบุคคลใน People API เช่น หากบุคคล
ตัวอย่างต่อไปนี้จะเพิ่มผู้ใช้ลงในพื้นที่ทำงาน
Python
- สร้างไฟล์ชื่อ
chat_membership_user_create.py
ในไดเรกทอรีการทำงาน ใส่รหัสต่อไปนี้ใน
chat_membership_user_create.py
:from google_auth_oauthlib.flow import InstalledAppFlow from googleapiclient.discovery import build # Define your app's authorization scopes. # When modifying these scopes, delete the file token.json, if it exists. SCOPES = ["https://www.googleapis.com/auth/chat.memberships"] def main(): ''' Authenticates with Chat API via user credentials, then adds a user to a Chat space by creating a membership. ''' # Authenticate with Google Workspace # and get user authorization. flow = InstalledAppFlow.from_client_secrets_file( 'client_secrets.json', SCOPES) creds = flow.run_local_server() # Build a service endpoint for Chat API. chat = build('chat', 'v1', credentials=creds) # Use the service endpoint to call Chat API. result = chat.spaces().members().create( # The space in which to create a membership. parent = 'spaces/SPACE', # Specify which user the membership is for. body = { 'member': { 'name':'users/USER', 'type': 'HUMAN' } } ).execute() # Prints details about the created membership. print(result) if __name__ == '__main__': main()
ในโค้ด ให้แทนที่ส่วนต่อไปนี้
SPACE
: ชื่อพื้นที่ทำงาน ซึ่งคุณดูได้จากเมธอดspaces.list
ใน Chat API หรือจาก URL ของพื้นที่ทำงานUSER
: รหัสผู้ใช้
สร้างและเรียกใช้ตัวอย่างในไดเรกทอรีการทำงาน
python3 chat_membership_user_create.py
Node.js
- สร้างไฟล์ชื่อ
add-user-to-space.js
ในไดเรกทอรีการทำงาน ใส่รหัสต่อไปนี้ใน
add-user-to-space.js
:const chat = require('@googleapis/chat'); const {authenticate} = require('@google-cloud/local-auth'); /** * Adds the user to the Chat space. * @return {!Promise<!Object>} */ async function addUserToSpace() { const scopes = [ 'https://www.googleapis.com/auth/chat.memberships', ]; const authClient = await authenticate({scopes, keyfilePath: 'client_secrets.json'}); const chatClient = await chat.chat({version: 'v1', auth: authClient}); return await chatClient.spaces.members.create({ parent: 'spaces/SPACE', requestBody: {member: {name: 'users/USER', type: 'HUMAN'}} }); } addUserToSpace().then(console.log);
ในโค้ด ให้แทนที่ส่วนต่อไปนี้
SPACE
: ชื่อพื้นที่ทำงาน ซึ่งจะดูได้จากเมธอดspaces.list
ใน Chat API หรือจาก URL ของพื้นที่ทำงานUSER
: รหัสผู้ใช้
เรียกใช้ตัวอย่างในไดเรกทอรีการทำงาน
node add-user-to-space.js
Chat API จะส่งกลับอินสแตนซ์ของ membership
ที่มีรายละเอียดการเป็นสมาชิกที่สร้างขึ้น
เพิ่มแอป Chat ไปยังพื้นที่ทำงาน
แอป Chat จะเพิ่มแอปอื่นเป็นสมาชิกในพื้นที่ทำงานไม่ได้ หากต้องการเพิ่มแอป Chat ในพื้นที่ทำงานหรือข้อความส่วนตัวระหว่างผู้ใช้ 2 คน ให้ส่งข้อมูลต่อไปนี้ในคำขอ
- ระบุขอบเขตการให้สิทธิ์
chat.memberships.app
- เรียกเมธอด
create
ในทรัพยากรmembership
- ตั้งค่า
parent
เป็นชื่อทรัพยากรของพื้นที่ทำงานที่จะสร้างการเป็นสมาชิก - ตั้งค่า
member
เป็นusers/app
ซึ่งเป็นชื่อแทนที่แทนแอปที่เรียกใช้ Chat API
ตัวอย่างต่อไปนี้เพิ่มแอป Chat ไปยังพื้นที่ทำงาน
Python
- สร้างไฟล์ชื่อ
chat_membership_app_create.py
ในไดเรกทอรีการทำงาน ใส่รหัสต่อไปนี้ใน
chat_membership_app_create.py
:from google_auth_oauthlib.flow import InstalledAppFlow from googleapiclient.discovery import build # Define your app's authorization scopes. # When modifying these scopes, delete the file token.json, if it exists. SCOPES = ["https://www.googleapis.com/auth/chat.memberships.app"] def main(): ''' Authenticates with Chat API via user credentials, then adds the Chat app to a Chat space. ''' # Authenticate with Google Workspace # and get user authorization. flow = InstalledAppFlow.from_client_secrets_file( 'client_secrets.json', SCOPES) creds = flow.run_local_server() # Build a service endpoint for Chat API. chat = build('chat', 'v1', credentials=creds) # Use the service endpoint to call Chat API. result = chat.spaces().members().create( # The space in which to create a membership. parent = 'spaces/SPACE', # Set the Chat app as the entity that gets added to the space. # 'app' is an alias for the Chat app calling the API. body = { 'member': { 'name':'users/app', 'type': 'BOT' } } ).execute() # Prints details about the created membership. print(result) if __name__ == '__main__': main()
ในโค้ด ให้แทนที่
SPACE
ด้วยชื่อพื้นที่ทำงานซึ่งคุณดูได้จากเมธอดspaces.list
ใน Chat API หรือจาก URL ของพื้นที่ทำงานสร้างและเรียกใช้ตัวอย่างในไดเรกทอรีการทำงาน
python3 chat_membership_app_create.py
Node.js
- สร้างไฟล์ชื่อ
add-app-to-space.js
ในไดเรกทอรีการทำงาน ใส่รหัสต่อไปนี้ใน
add-app-to-space.js
:const chat = require('@googleapis/chat'); const {authenticate} = require('@google-cloud/local-auth'); /** * Adds the app to the Chat space. * @return {!Promise<!Object>} */ async function addAppToSpace() { const scopes = [ 'https://www.googleapis.com/auth/chat.memberships.app', ]; const authClient = await authenticate({scopes, keyfilePath: 'client_secrets.json'}); const chatClient = await chat.chat({version: 'v1', auth: authClient}); return await chatClient.spaces.members.create({ parent: 'spaces/SPACE', requestBody: {member: {name: 'users/app', type: 'BOT'}} }); } addAppToSpace().then(console.log);
ในโค้ด ให้แทนที่
SPACE
ด้วยชื่อพื้นที่ทำงานซึ่งคุณดูได้จากเมธอดspaces.list
ใน Chat API หรือจาก URL ของพื้นที่ทำงานเรียกใช้ตัวอย่างในไดเรกทอรีการทำงาน
node add-app-to-space.js
Chat API จะส่งกลับอินสแตนซ์ของ membership
ที่มีรายละเอียดการเป็นสมาชิกที่สร้างขึ้น
หัวข้อที่เกี่ยวข้อง
- ดูรายละเอียดเกี่ยวกับการเป็นสมาชิกของผู้ใช้หรือแอป Chat
- แสดงรายชื่อสมาชิกในพื้นที่ทำงาน
- อัปเดตการเป็นสมาชิกของผู้ใช้ในพื้นที่ของ Google Chat
- นำผู้ใช้หรือแอป Chat ออกจากพื้นที่ทำงาน