تعديل عضوية المستخدم في مساحة Google Chat

يوضِّح هذا الدليل كيفية استخدام طريقة patch على مورد membership في Google Chat API لتغيير السمات المتعلقة باشتراك، مثل تغيير عضو في مساحة إلى مدير مساحة أو تغيير مدير مساحة إلى عضو في مساحة.

يوضّح المرجع 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.import في حال استيراد البيانات إلى Chat.

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.import في حال استيراد البيانات إلى Chat.

برمجة تطبيقات

تعديل اشتراك

لتعديل عضوية في مساحة، يُرجى تقديم ما يلي في طلبك:

  • حدِّد نطاق تفويض chat.memberships.
  • يمكنك استدعاء طريقة patch على المورد Membership وضبط name الخاصة بالعضوية لتعديلها، بالإضافة إلى updateMask وbody يحدّد سمات الاشتراك المعدَّلة.
  • تحدّد السمة updateMask جوانب الاشتراك المطلوب تعديلها، وتتضمّن ما يلي:
    • role: دور المستخدم في "مساحة Chat"، والذي يحدّد الإجراءات المسموح بها في المساحة. القيم المتاحة:
      • ROLE_MEMBER: أحد أعضاء المساحة لدى المستخدم أذونات أساسية، مثل إرسال رسائل إلى المساحة. في المحادثات بين شخصين والمحادثات الجماعية بدون اسم، يتمتع كل شخص بهذا الدور.
      • ROLE_MANAGER: مدير مساحة يمتلك المستخدم جميع الأذونات الأساسية بالإضافة إلى الأذونات الإدارية التي تسمح له بإدارة المساحة، مثل إضافة الأعضاء أو إزالتهم. لا يمكن استخدامها إلا في المساحات التي يكون spaceType فيها SPACE (المسافات المُسمّاة).

منح عضو عادي إلى مساحة دور مدير مساحة

في المثال التالي، يتم تحويل المستخدم العادي إلى مساحة من خلال تحديد role على أنّه ROLE_MANAGER في body الذي يحدّد سمات الاشتراك المعدَّلة:

Python

  1. في دليل العمل، أنشِئ ملفًا باسم chat_membership_update.py.
  2. ضمِّن الرمز التالي في chat_membership_update.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 updates a specified space member to change
        it from a regular member to a space manager.
        '''
    
        # 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().patch(
    
            # The membership to update, and the updated role.
            #
            # Replace SPACE with a space name.
            # Obtain the space name from the spaces resource of Chat API,
            # or from a space's URL.
            #
            # Replace MEMBERSHIP with a membership name.
            # Obtain the membership name from the membership of Chat API.
            name='spaces/SPACE/members/MEMBERSHIP',
            updateMask='role',
            body={'role': 'ROLE_MANAGER'}
    
          ).execute()
    
        # Prints details about the updated membership.
        print(result)
    
    if __name__ == '__main__':
        main()
    
  3. في التعليمة البرمجية، استبدل ما يلي:

    • SPACE: اسم مساحة يمكنك الحصول عليه من خلال طريقة spaces.list في Chat API أو من عنوان URL الخاص بالمساحة

    • MEMBERSHIP: اسم اشتراك يمكنك الحصول عليه من طريقة spaces.members.list في Chat API.

  4. في دليل العمل، أنشئ النموذج وقم بتشغيله:

    python3 chat_membership_update.py
    

Node.js

  1. في دليل العمل، أنشِئ ملفًا باسم chat_membership_update.js.
  2. ضمِّن الرمز التالي في chat_membership_update.js:

    const chat = require('@googleapis/chat');
    const {authenticate} = require('@google-cloud/local-auth');
    
    /**
    * Updates a membership in a Chat space to change it from
    * a space member to a space manager.
    * @return {!Promise<!Object>}
    */
    async function updateSpace() {
    
      /**
      * Authenticate with Google Workspace
      * and get user authorization.
      */
      const scopes = [
        'https://www.googleapis.com/auth/chat.memberships',
      ];
    
      const authClient =
          await authenticate({scopes, keyfilePath: 'client_secrets.json'});
    
      /**
      * Build a service endpoint for Chat API.
      */
      const chatClient = await chat.chat({version: 'v1', auth: authClient});
    
      /**
      * Use the service endpoint to call Chat API.
      */
      return await chatClient.spaces.patch({
    
        /**
        * The membership to update, and the updated role.
        *
        * Replace SPACE with a space name.
        * Obtain the space name from the spaces resource of Chat API,
        * or from a space's URL.
        *
        * Replace MEMBERSHIP with a membership name.
        * Obtain the membership name from the membership of Chat API.
        */
        name: 'spaces/SPACE/members/MEMBERSHIP',
        updateMask: 'role',
        requestBody: {
          role: 'ROLE_MANAGER'
        }
      });
    }
    
    /**
    * Use the service endpoint to call Chat API.
    */
    updateSpace().then(console.log);
    
  3. في التعليمة البرمجية، استبدل ما يلي:

    • SPACE: اسم مساحة يمكنك الحصول عليه من خلال طريقة spaces.list في Chat API أو من عنوان URL الخاص بالمساحة

    • MEMBERSHIP: اسم اشتراك يمكنك الحصول عليه من طريقة spaces.members.list في Chat API.

  4. في دليل العمل، أنشئ النموذج وقم بتشغيله:

    python3 chat_membership_update.js
    

برمجة تطبيقات

يستدعي هذا المثال واجهة Chat API باستخدام Advanced Chat Service.

  1. أضِف نطاق تفويض chat.memberships إلى ملف appsscript.json لمشروع "برمجة تطبيقات Google":

    "oauthScopes": [
      "https://www.googleapis.com/auth/chat.memberships"
    ]
    
  2. أضِف دالة مثل هذه إلى رمز مشروع برمجة التطبيقات:

    /**
     * Updates a membership from space member to space manager.
     * @param {string} memberName The resource name of the membership.
    */
    function updateMembershipToSpaceManager(memberName) {
      try {
        const body = {'role': 'ROLE_MANAGER'};
        Chat.Spaces.Members.patch(memberName, body);
      } catch (err) {
        // TODO (developer) - Handle exception
        console.log('Failed to create message with error %s', err.message);
      }
    }
    

تغيِّر واجهة Google Chat API الاشتراك المحدّد إلى مدير مساحة وتعرض مثيل من Membership يفصّل التغيير.

منح مدير مساحة دور عضو منتظم

في ما يلي مثال يجعل مدير مساحة عضوًا عاديًا من خلال تحديد role على أنّه ROLE_MEMBER في body الذي يحدّد سمات الاشتراك المعدَّلة:

Python

  1. في دليل العمل، أنشِئ ملفًا باسم chat_membership_update.py.
  2. ضمِّن الرمز التالي في chat_membership_update.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 updates a specified space member to change
        it from a regular member to a space manager.
        '''
    
        # 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().patch(
    
            # The membership to update, and the updated role.
            #
            # Replace SPACE with a space name.
            # Obtain the space name from the spaces resource of Chat API,
            # or from a space's URL.
            #
            # Replace MEMBERSHIP with a membership name.
            # Obtain the membership name from the membership of Chat API.
            name='spaces/SPACE/members/MEMBERSHIP',
            updateMask='role',
            body={'role': 'ROLE_MEMBER'}
    
          ).execute()
    
        # Prints details about the updated membership.
        print(result)
    
    if __name__ == '__main__':
        main()
    
  3. في التعليمة البرمجية، استبدل ما يلي:

    • SPACE: اسم مساحة يمكنك الحصول عليه من خلال طريقة spaces.list في Chat API أو من عنوان URL الخاص بالمساحة

    • MEMBERSHIP: اسم اشتراك يمكنك الحصول عليه من طريقة spaces.members.list في Chat API.

  4. في دليل العمل، أنشئ النموذج وقم بتشغيله:

    python3 chat_membership_update.py
    

Node.js

  1. في دليل العمل، أنشِئ ملفًا باسم chat_membership_update.js.
  2. ضمِّن الرمز التالي في chat_membership_update.js:

    const chat = require('@googleapis/chat');
    const {authenticate} = require('@google-cloud/local-auth');
    
    /**
    * Updates a membership in a Chat space to change it from
    * a space manager to a space member.
    * @return {!Promise<!Object>}
    */
    async function updateSpace() {
    
      /**
      * Authenticate with Google Workspace
      * and get user authorization.
      */
      const scopes = [
        'https://www.googleapis.com/auth/chat.memberships',
      ];
    
      const authClient =
          await authenticate({scopes, keyfilePath: 'client_secrets.json'});
    
      /**
      * Build a service endpoint for Chat API.
      */
      const chatClient = await chat.chat({version: 'v1', auth: authClient});
    
      /**
      * Use the service endpoint to call Chat API.
      */
      return await chatClient.spaces.patch({
    
        /**
        * The membership to update, and the updated role.
        *
        * Replace SPACE with a space name.
        * Obtain the space name from the spaces resource of Chat API,
        * or from a space's URL.
        *
        * Replace MEMBERSHIP with a membership name.
        * Obtain the membership name from the membership of Chat API.
        */
        name: 'spaces/SPACE/members/MEMBERSHIP',
        updateMask: 'role',
        requestBody: {
          role: 'ROLE_MEMBER'
        }
      });
    }
    
    /**
    * Use the service endpoint to call Chat API.
    */
    updateSpace().then(console.log);
    
  3. في التعليمة البرمجية، استبدل ما يلي:

    • SPACE: اسم مساحة يمكنك الحصول عليه من خلال طريقة spaces.list في Chat API أو من عنوان URL الخاص بالمساحة

    • MEMBERSHIP: اسم اشتراك يمكنك الحصول عليه من طريقة spaces.members.list في Chat API.

  4. في دليل العمل، أنشئ النموذج وقم بتشغيله:

    python3 chat_membership_update.js
    

برمجة تطبيقات

يستدعي هذا المثال واجهة Chat API باستخدام Advanced Chat Service.

  1. أضِف نطاق تفويض chat.memberships إلى ملف appsscript.json لمشروع "برمجة تطبيقات Google":

    "oauthScopes": [
      "https://www.googleapis.com/auth/chat.memberships"
    ]
    
  2. أضِف دالة مثل هذه إلى رمز مشروع برمجة التطبيقات:

    /**
     * Updates a membership from space manager to space member.
     * @param {string} memberName The resource name of the membership.
    */
    function updateMembershipToSpaceMember(memberName) {
      try {
        const body = {'role': 'ROLE_MEMBER'};
        Chat.Spaces.Members.patch(memberName, body);
      } catch (err) {
        // TODO (developer) - Handle exception
        console.log('Failed to create message with error %s', err.message);
      }
    }
    

تغيِّر واجهة Google Chat API الاشتراك المحدّد إلى مدير مساحة وتعرض مثيل من Membership يفصّل التغيير.