دعوة مستخدم أو تطبيق 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.
  • استدعِ طريقة create في المورد membership.
  • اضبط parent على اسم المورد الخاص بالمساحة التي تريد إنشاء الاشتراك فيها.
  • اضبط السمة member على users/{user} حيث يكون {user} هو المستخدم الذي تريد إنشاء اشتراك له، ويكون إما:
    • تمثّل هذه السمة رقم تعريف الشخص في People API. على سبيل المثال، إذا كان person resourceName في واجهة برمجة تطبيقات People API هو 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: اسم مساحة يمكنك الحصول عليه من خلال طريقة 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: اسم مساحة يمكنك الحصول عليه من خلال طريقة spaces.list في Chat API أو من عنوان URL الخاص بالمساحة

    • USER: رقم تعريف المستخدِم

  4. في دليل العمل، شغِّل النموذج:

    node add-user-to-space.js
    

تعرض Chat API مثيل membership الذي يوضِّح تفاصيل الاشتراك الذي تم إنشاؤه.

إضافة تطبيق Chat إلى مساحة

لا يمكن لتطبيق Chat إضافة تطبيق آخر كعضو إلى مساحة. لإضافة تطبيق في Chat إلى مساحة أو رسالة مباشرة بين مستخدمَين، أدخِل ما يلي في طلبك:

  • حدِّد نطاق تفويض chat.memberships.app.
  • استدعِ طريقة create في المورد membership.
  • اضبط parent على اسم المورد الخاص بالمساحة التي تريد إنشاء الاشتراك فيها.
  • اضبط السمة member على users/app، وهي عبارة عن عنوان بديل يمثّل التطبيق الذي يتصل بواجهة برمجة التطبيقات Chat.

يضيف المثال التالي تطبيق 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 الذي يوضِّح تفاصيل الاشتراك الذي تم إنشاؤه.