במדריך הזה מוסבר איך משתמשים ב-method create
במשאב membership
ב-Google Chat API כדי להזמין או להוסיף משתמשים, קבוצות Google או אפליקציית 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
. - קוראים ל-method
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
: שם המרחב, שאפשר לקבל באמצעות ה-methodspaces.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
: שם המרחב, שאפשר לקבל באמצעות ה-methodspaces.list
ב-Chat API או מכתובת ה-URL של המרחב.USER
: מזהה משתמש
בספריית העבודה, מריצים את הדוגמה:
node add-user-to-space.js
ה-Chat API מחזיר מופע של membership
שמציין את פרטי המינוי של המשתמש שנוצר.
איך מזמינים או מוסיפים קבוצות מ'קבוצות Google' למרחבים משותפים
כדי להזמין או להוסיף קבוצות מ-Google למרחבים משותפים, צריך להעביר את הפרטים הבאים בבקשה:
- מציינים את היקף ההרשאה
chat.memberships
. - קוראים ל-method
create
במשאבmembership
. - מגדירים את
parent
כשם המשאב של המרחב המשותף שבו רוצים ליצור חברות. - מגדירים את
groupMember
לערךgroups/{group}
.{group}
הוא מזהה הקבוצה שאתם רוצים ליצור בה חברות. אפשר לאחזר את מזהה הקבוצה באמצעות Cloud Identity API. לדוגמה, אם Cloud Identity API מחזיר קבוצה בשםgroups/123456789
, צריך להגדיר אתmembership.groupMember.name
ל-groups/123456789
.
אי אפשר לצרף קבוצות Google לצ'אטים קבוצתיים או לצ'אטים אישיים, אלא רק למרחב משותף עם שם. בדוגמה הבאה מוסיפים קבוצה למרחב משותף עם שם:
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
: שם המרחב, שאפשר לקבל באמצעות ה-methodspaces.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
: שם המרחב, שאפשר לקבל באמצעות ה-methodspaces.list
ב-Chat API או מכתובת ה-URL של המרחב.GROUP
: מזהה קבוצה.
בספריית העבודה, מריצים את הדוגמה:
node add-group-to-space.js
ה-Chat API מחזיר מופע של membership
שמציין את פרטי החברות בקבוצה שנוצרה.
איך מצרפים אפליקציית Chat למרחבים משותפים
אי אפשר לצרף למרחב המשותף אפליקציות של Chat באמצעות אפליקציות אחרות. כדי להוסיף אפליקציית Chat למרחב משותף או לצ'אט אישי בין שני משתמשים אנושיים, צריך לבצע את הדברים הבאים בבקשה:
- מציינים את היקף ההרשאה
chat.memberships.app
. - קוראים ל-method
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
בשם של מרחב משותף, שאפשר לקבל באמצעות ה-methodspaces.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
בשם של מרחב משותף, שאפשר לקבל באמצעות ה-methodspaces.list
ב-Chat API או מכתובת ה-URL של המרחב.בספריית העבודה, מריצים את הדוגמה:
node add-app-to-space.js
Chat API מחזיר מופע של membership
שמציין את פרטי המינוי שנוצר עבור האפליקציה.
נושאים קשורים
- פרטים על המינוי של משתמש או של אפליקציית Chat.
- הצגת רשימת המשתתפים במרחבים משותפים
- איך מעדכנים את פרטי החברות של משתמשים במרחב משותף ב-Google Chat
- איך מסירים משתמשים או אפליקציית Chat ממרחבים משותפים