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

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

تشير رسالة الأشكال البيانية مرجع Membership يمثِّل ما إذا تمت دعوة مستخدِم أو تطبيق Google Chat إلى أو جزء منها أو غير موجود فيه.

المتطلبات الأساسية

Python

  • Python 3.6 أو أعلى
  • أداة إدارة حزم pip
  • أحدث مكتبات عملاء Google. لتثبيت التطبيقات أو تحديثها، قم بتشغيل الأمر التالي في واجهة سطر الأوامر:
    pip3 install --upgrade google-api-python-client google-auth-oauthlib
    

Node.js

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

تعديل اشتراك

لتعديل عضوية في مساحة، أدخِل ما يلي في طلبك:

  • حدِّد نطاق تفويض chat.memberships.
  • عليك استدعاء طريقة patch على مرجع Membership وتمرير name للعضوية لتعديلها، بالإضافة إلى updateMask وbody يحدد سمات العضوية المُعدّلة.
  • تحدّد السمة updateMask جوانب الاشتراك المطلوب تعديلها. ما يلي:
    • role: دور المستخدم ضمن "مساحة Chat" الذي يحدِّد الأذونات المسموح بها من الإجراءات في المساحة. القيم المتاحة:
      • ROLE_MEMBER: عضو في المساحة لدى المستخدم الأذونات الأساسية، مثل إرسال الرسائل إلى المساحة. في مجموعة 1:1 ومجموعة بدون اسم المحادثات، فإن الجميع لديهم هذا الدور.
      • "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. في الرمز، استبدل ما يلي:

  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. في الرمز، استبدل ما يلي:

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

    python3 chat_membership_update.js
    

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

يستدعي هذا المثال واجهة برمجة تطبيقات Chat باستخدام خدمة Chat المتقدّمة

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

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

    /**
     * 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 الاشتراك المحدّد إلى مدير مساحة وتعرض مثال على 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. في الرمز، استبدل ما يلي:

  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. في الرمز، استبدل ما يلي:

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

    python3 chat_membership_update.js
    

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

يستدعي هذا المثال واجهة برمجة تطبيقات Chat باستخدام خدمة Chat المتقدّمة

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

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

    /**
     * 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 الاشتراك المحدّد إلى مدير مساحة وتعرض مثال على Membership بالتفصيل التغيير.