הזמנה או הוספה של משתמשים או אפליקציות של Google Chat למרחבים משותפים

מדריך זה מסביר איך משתמשים בשיטה 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.
  • קוראים ל-method create במשאב membership.
  • משנים את parent לשם המשאב של המרחב המשותף שבו יוצרים חבר.
  • מגדירים את member לערך users/{user}, כאשר {user} הוא האדם שרוצים ליצור בשבילו חברות, והוא:
    • המזהה של האדם ב-People API. לדוגמה, במקרה ש-resourceName API person הוא people/123456789, צריך להגדיר את membership.member.name לערך users/123456789.
    • המזהה של המשתמש ב-Directory API.
    • זוהי כתובת האימייל של המשתמש. לדוגמה, users/222larabrown@gmail.com או users/larabrown@cymbalgroup.com. אם המשתמש משתמש בחשבון Google או שייך לארגון אחר ב-Google Workspace, עליכם להשתמש בכתובת האימייל שלו.

בדוגמה הבאה משתמשים מתווספים למרחבים משותפים:

Python

  1. בספריית העבודה, יוצרים קובץ בשם chat_membership_user_create.py.
  2. יש לכלול את הקוד הבא ב-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()
    
  3. בקוד, מחליפים את מה שכתוב בשדות הבאים:

    • SPACE: שם של מרחב משותף, שאפשר לקבל מה-method spaces.list ב-Chat API או מכתובת ה-URL של המרחב המשותף.

    • USER: מזהה משתמש.

  4. בספריית העבודה, יוצרים ומריצים את הדוגמה:

    python3 chat_membership_user_create.py
    

Node.js

  1. בספריית העבודה, יוצרים קובץ בשם add-user-to-space.js.
  2. יש לכלול את הקוד הבא ב-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.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/USER', type: 'HUMAN'}}
      });
    }
    
    addUserToSpace().then(console.log);
    
  3. בקוד, מחליפים את מה שכתוב בשדות הבאים:

    • SPACE: שם למרחב המשותף, שאותו אפשר לקבל דרך ה-method spaces.list ב-Chat API או מכתובת ה-URL של המרחב המשותף.

    • USER: מזהה משתמש.

  4. בספריית העבודה, מריצים את הדוגמה:

    node add-user-to-space.js
    

ה-Chat API מחזיר מופע של membership שכולל פרטים על המינוי שנוצר.

איך מוסיפים אפליקציות של Chat למרחבים משותפים

אפליקציית Chat לא יכולה להוסיף אפליקציות למרחבים משותפים. כדי לצרף אפליקציות ל-Chat למרחב משותף או לצ'אט אישי בין שני משתמשים, צריך להעביר לבקשה את הפרטים הבאים:

  • צריך לציין את היקף ההרשאה של chat.memberships.app.
  • קוראים ל-method create במשאב membership.
  • משנים את parent לשם המשאב של המרחב המשותף שבו יוצרים חבר.
  • מגדירים את member לערך users/app, כתובת אימייל חלופית שמייצגת את האפליקציה שקוראת ל-Chat API.

בדוגמה הבאה, אפליקציית Chat מתווספת למרחב משותף:

Python

  1. בספריית העבודה, יוצרים קובץ בשם chat_membership_app_create.py.
  2. יש לכלול את הקוד הבא ב-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()
    
  3. בקוד, מחליפים את SPACE בשם של מרחב משותף, שאותו אפשר לקבל מה-method spaces.list ב-Chat API או מכתובת ה-URL של המרחב המשותף.

  4. בספריית העבודה, יוצרים ומריצים את הדוגמה:

    python3 chat_membership_app_create.py
    

Node.js

  1. בספריית העבודה, יוצרים קובץ בשם add-app-to-space.js.
  2. יש לכלול את הקוד הבא ב-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);
    
  3. בקוד, מחליפים את SPACE בשם של מרחב משותף, שאותו אפשר לקבל מה-method spaces.list ב-Chat API או מכתובת ה-URL של המרחב המשותף.

  4. בספריית העבודה, מריצים את הדוגמה:

    node add-app-to-space.js
    

ה-Chat API מחזיר מופע של membership שכולל פרטים על המינוי שנוצר.