คำแนะนำนี้จะอธิบายวิธีใช้เมธอด create
ในแหล่งข้อมูล membership
Google Chat API เพื่อเชิญหรือเพิ่มผู้ใช้, Google Group หรือ
ส่งแอปแชทไปยังพื้นที่ทำงาน หรือที่เรียกกันว่าการสร้าง
การเป็นสมาชิก เมื่อสร้างการเป็นสมาชิก หากสมาชิกที่ระบุมี
ปิดนโยบายการยอมรับอัตโนมัติแล้ว บุคคลดังกล่าวจะได้รับเชิญและต้องยอมรับพื้นที่ทำงาน
ก่อนเข้าร่วม มิเช่นนั้นการสร้างการเป็นสมาชิกจะเป็นการเพิ่มสมาชิก
ไปยังพื้นที่ทำงานที่ระบุโดยตรง
แหล่งข้อมูล Membership
รายการ
จะแสดงว่ามีการเชิญผู้ใช้ที่เป็นมนุษย์หรือแอป Google Chat หรือไม่
เพียงบางส่วน หรือไม่ปรากฏในพื้นที่ทำงาน
ข้อกำหนดเบื้องต้น
Python
- ธุรกิจหรือองค์กร บัญชี Google Workspace ที่มีสิทธิ์เข้าถึง Google Chat
- Python 3.6 ขึ้นไป
- เครื่องมือจัดการแพ็กเกจ pip
- ไลบรารีของไคลเอ็นต์ Google ล่าสุด หากต้องการติดตั้งหรืออัปเดตส่วนขยาย
เรียกใช้คำสั่งต่อไปนี้ในอินเทอร์เฟซบรรทัดคำสั่ง
pip3 install --upgrade google-api-python-client google-auth-oauthlib
- ตั้งค่าสภาพแวดล้อมโดยทำดังนี้
- สร้างโปรเจ็กต์ Google Cloud
- กำหนดค่าหน้าจอขอความยินยอม OAuth
- เปิดใช้และกำหนดค่า Google Chat API โดยใช้ชื่อ ไอคอนและคำอธิบายสำหรับแอป Chat ของคุณ
-
สร้างข้อมูลเข้าสู่ระบบรหัสไคลเอ็นต์ OAuth สำหรับแอปพลิเคชันบนเดสก์ท็อป หากต้องการเรียกใช้ตัวอย่างใน
ให้บันทึกข้อมูลเข้าสู่ระบบเป็นไฟล์ JSON (
client_secrets.json
) ลงใน ไดเรกทอรีในเครื่อง
- เลือกขอบเขตการให้สิทธิ์ที่รองรับการตรวจสอบสิทธิ์ผู้ใช้
Node.js
- ธุรกิจหรือองค์กร บัญชี Google Workspace ที่มีสิทธิ์เข้าถึง Google Chat
- Node.js 14 ขึ้นไป
- npm เครื่องมือจัดการแพ็กเกจ
- ไลบรารีของไคลเอ็นต์ Google ล่าสุด หากต้องการติดตั้งหรืออัปเดตส่วนขยาย
เรียกใช้คำสั่งต่อไปนี้ในอินเทอร์เฟซบรรทัดคำสั่ง
npm install @google-cloud/local-auth @googleapis/chat
- ตั้งค่าสภาพแวดล้อมโดยทำดังนี้
- สร้างโปรเจ็กต์ Google Cloud
- กำหนดค่าหน้าจอขอความยินยอม OAuth
- เปิดใช้และกำหนดค่า Google Chat API โดยใช้ชื่อ ไอคอนและคำอธิบายสำหรับแอป Chat
-
สร้างข้อมูลเข้าสู่ระบบรหัสไคลเอ็นต์ OAuth สำหรับแอปพลิเคชันบนเดสก์ท็อป หากต้องการเรียกใช้ตัวอย่างใน
ให้บันทึกข้อมูลเข้าสู่ระบบเป็นไฟล์ JSON (
client_secrets.json
) ลงใน ไดเรกทอรีในเครื่อง
- เลือกขอบเขตการให้สิทธิ์ที่รองรับการตรวจสอบสิทธิ์ผู้ใช้
เชิญหรือเพิ่มผู้ใช้ไปยังพื้นที่ทำงาน
หากต้องการเชิญหรือเพิ่มผู้ใช้ไปยังพื้นที่ทำงาน ให้ส่งข้อมูลต่อไปนี้ใน คำขอ:
- ระบุขอบเขตการให้สิทธิ์
chat.memberships
- เรียกใช้
create
วิธี ใน แหล่งข้อมูลmembership
- ตั้งค่า
parent
เป็นชื่อทรัพยากรของพื้นที่ทำงานที่จะสร้างการเป็นสมาชิก - ตั้งค่า
member
เป็นusers/{user}
โดยที่{user}
คือบุคคลที่คุณต้องการ สร้างการเป็นสมาชิกสำหรับ ซึ่งอาจเป็นอย่างใดอย่างหนึ่งต่อไปนี้- รหัสสำหรับฟิลด์
คน
ใน People API ตัวอย่างเช่น หาก People API
คน
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
คน
ตัวอย่างต่อไปนี้เพิ่มผู้ใช้ไปยังพื้นที่ทำงาน
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 ออกจากพื้นที่ทำงาน