איך מזמינים משתמשים, קבוצות ב-Google או אפליקציית Google Chat למרחבים משותפים

במדריך הזה מוסבר איך להשתמש ב-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.
  • קוראים לפונקציה אמצעי תשלום אחד (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. צריך להשתמש בחשבון שלו או שלה כתובת אימייל.

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

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. בקוד, מחליפים את מה שכתוב בשדות הבאים:

  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',
      ];
    
      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. בקוד, מחליפים את מה שכתוב בשדות הבאים:

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

    node add-user-to-space.js
    

Chat API מחזיר מופע של membership שמפרטת את פרטי המינוי שנוצר.

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

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

  • מציינים את היקף ההרשאה chat.memberships.
  • קוראים לפונקציה אמצעי תשלום אחד (create) ב משאב membership.
  • מגדירים את parent כשם המשאב של המרחב המשותף שבו רוצים ליצור חברות.
  • מגדירים את groupMember לערך groups/{group} כאשר {group} הוא מזהה הקבוצה שרוצים ליצור מינוי אליו. אפשר לאחזר את המזהה של הקבוצה באמצעות Cloud Identity API. לדוגמה, אם Cloud Identity API מחזירה קבוצה בשם groups/123456789, ואז מגדירים membership.groupMember.name אל groups/123456789.

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

Python

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

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

    python3 chat_membership_group_create.py
    

Node.js

  1. בספריית העבודה, יוצרים קובץ בשם add-group-to-space.js.
  2. צריך לכלול את הקוד הבא ב-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);
    
  3. בקוד, מחליפים את מה שכתוב בשדות הבאים:

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

    node add-group-to-space.js
    

Chat API מחזיר מופע של membership שמפרטת את החברות בקבוצה שנוצרה.

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

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

  • מציינים את היקף ההרשאה chat.memberships.app.
  • קוראים לפונקציה אמצעי תשלום אחד (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 בשם של מרחב משותף. שאפשר לקבל אמצעי תשלום אחד (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 בשם של מרחב משותף. שאפשר לקבל אמצעי תשלום אחד (spaces.list) מ-Chat API או מכתובת ה-URL של מרחב משותף.

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

    node add-app-to-space.js
    

Chat API מחזיר מופע של membership שבה מפורט המינוי שנוצר לאפליקציה.