คำแนะนำนี้จะอธิบายวิธีใช้เมธอด create
ในทรัพยากร membership
ของ Google Chat API เพื่อเชิญหรือเพิ่มผู้ใช้ Google Group หรือแอป 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 เช่น หาก People API
person
resourceName
คือpeople/123456789
ให้ตั้งmembership.member.name
เป็นusers/123456789
- รหัสสำหรับผู้ใช้ใน Directory API
- อีเมลของผู้ใช้ เช่น
users/222larabrown@gmail.com
หรือusers/larabrown@cymbalgroup.com
หากผู้ใช้ใช้บัญชี Google หรือเป็นขององค์กร Google Workspace อื่น คุณต้องใช้อีเมลของผู้ใช้นั้น
- รหัสสำหรับบุคคลใน People API เช่น หาก People API
person
ตัวอย่างต่อไปนี้เพิ่มผู้ใช้ไปยังพื้นที่ทำงาน
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
ที่แสดงรายละเอียดการเป็นสมาชิกของผู้ใช้ที่สร้างขึ้น
เชิญหรือเพิ่ม Google Group ในพื้นที่ทำงาน
หากต้องการเชิญหรือเพิ่ม Google Group ในพื้นที่ทำงาน ให้ส่งข้อมูลต่อไปนี้ในคำขอ
- ระบุขอบเขตการให้สิทธิ์
chat.memberships
- เรียกใช้เมธอด
create
ในทรัพยากรmembership
- ตั้งค่า
parent
เป็นชื่อทรัพยากรของพื้นที่ทำงานที่จะสร้างการเป็นสมาชิก - ตั้งค่า
groupMember
เป็นgroups/{group}
โดย{group}
คือรหัสกลุ่มที่คุณต้องการสร้างการเป็นสมาชิก คุณจะดึงข้อมูลรหัสของกลุ่มได้โดยใช้ Cloud Identity API ตัวอย่างเช่น หาก Cloud Identity API แสดงผลกลุ่มชื่อgroups/123456789
ให้กำหนดmembership.groupMember.name
เป็นgroups/123456789
คุณไม่สามารถเพิ่ม Google Groups ลงในแชทเป็นกลุ่มหรือข้อความส่วนตัวได้ แต่จะเพิ่มได้เฉพาะในพื้นที่ทำงานที่มีชื่อเท่านั้น ตัวอย่างต่อไปนี้เพิ่มกลุ่มไปยังพื้นที่ทำงานที่มีชื่อ
Python
- สร้างไฟล์ชื่อ
chat_membership_group_create.py
ในไดเรกทอรีการทำงาน รวมรหัสต่อไปนี้ใน
chat_membership_group_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 group 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 named space in which to create a membership. parent = 'spaces/SPACE', # Specify which group the membership is for. body = { 'groupMember': { 'name':'groups/GROUP', } } ).execute() # Prints details about the created membership. print(result) if __name__ == '__main__': main()
ในโค้ด ให้แทนที่
SPACE
: ชื่อพื้นที่ทำงานซึ่งคุณได้รับจากเมธอดspaces.list
ใน Chat API หรือจาก URL ของพื้นที่ทำงานGROUP
: รหัสกลุ่ม
ในไดเรกทอรีการทำงาน ให้สร้างและเรียกใช้ตัวอย่างด้วยคำสั่งต่อไปนี้
python3 chat_membership_group_create.py
Node.js
- สร้างไฟล์ชื่อ
add-group-to-space.js
ในไดเรกทอรีการทำงาน รวมรหัสต่อไปนี้ใน
add-group-to-space.js
:const chat = require('@googleapis/chat'); const {authenticate} = require('@google-cloud/local-auth'); /** * Adds the group 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: {groupMember: {name: 'groups/GROUP'}} }); } addUserToSpace().then(console.log);
ในโค้ด ให้แทนที่
SPACE
: ชื่อพื้นที่ทำงานซึ่งคุณได้รับจากเมธอดspaces.list
ใน Chat API หรือจาก URL ของพื้นที่ทำงานGROUP
: รหัสกลุ่ม
ในไดเรกทอรีการทำงาน ให้เรียกใช้ตัวอย่าง
node add-group-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 ออกจากพื้นที่ทำงาน