Google Chat स्पेस में उपयोगकर्ता की सदस्यता अपडेट करना

इस गाइड में बताया गया है कि सदस्यता से जुड़े एट्रिब्यूट बदलने के लिए, Google Chat API के membership संसाधन पर patch तरीके का इस्तेमाल कैसे किया जाता है. जैसे, स्पेस के सदस्य को स्पेस मैनेजर बनाना या स्पेस मैनेजर को स्पेस का सदस्य बनाना.

Membership के रिसॉर्स से पता चलता है कि किसी स्पेस में शामिल होने के लिए, किसी उपयोगकर्ता या Google Chat ऐप्लिकेशन को न्योता भेजा गया है या वह स्पेस में शामिल नहीं है.

Python

  • Python 3.6 या इससे नया वर्शन
  • pip पैकेज मैनेजमेंट टूल
  • Python के लिए नई Google क्लाइंट लाइब्रेरी. उन्हें इंस्टॉल या अपडेट करने के लिए, अपने कमांड-लाइन इंटरफ़ेस में नीचे दिया गया कमांड चलाएं:

    pip3 install --upgrade google-api-python-client google-auth-oauthlib
    
  • ऐसा Google Cloud प्रोजेक्ट जिसमें Google Chat API चालू हो और उसे कॉन्फ़िगर किया गया हो. तरीका जानने के लिए, Google Chat ऐप्लिकेशन बनाना देखें.
  • Chat ऐप्लिकेशन के लिए अनुमति कॉन्फ़िगर की गई. सदस्यता अपडेट करने के लिए, chat.memberships की अनुमति के दायरे के साथ उपयोगकर्ता की पुष्टि करना ज़रूरी है. अगर Chat में डेटा इंपोर्ट किया जा रहा है, तो पुष्टि करने का स्कोप chat.import का होना चाहिए.

Node.js

  • Node.js और npm
  • Node.js के लिए नई Google क्लाइंट लाइब्रेरी. उन्हें इंस्टॉल करने के लिए, अपने कमांड-लाइन इंटरफ़ेस में इस कमांड को चलाएं:

    npm install @google-cloud/local-auth @googleapis/chat
    
  • ऐसा Google Cloud प्रोजेक्ट जिसमें Google Chat API चालू हो और उसे कॉन्फ़िगर किया गया हो. तरीका जानने के लिए, Google Chat ऐप्लिकेशन बनाना देखें.
  • Chat ऐप्लिकेशन के लिए अनुमति कॉन्फ़िगर की गई. सदस्यता अपडेट करने के लिए, chat.memberships की अनुमति के दायरे के साथ उपयोगकर्ता की पुष्टि करना ज़रूरी है. अगर Chat में डेटा इंपोर्ट किया जा रहा है, तो पुष्टि करने का स्कोप chat.import का होना चाहिए.

Apps Script

पैसे चुकाकर ली गई सदस्यता अपडेट करना

स्पेस की सदस्यता अपडेट करने के लिए, अपने अनुरोध में यह जानकारी दें:

  • chat.memberships अनुमति का स्कोप बताएं.
  • Membership रिसॉर्स पर patch वाला तरीका इस्तेमाल करें और अपडेट करने के लिए, सदस्यता का name पास करें. साथ ही, updateMask और body भी पास करें, जो अपडेट किए गए सदस्यता एट्रिब्यूट की जानकारी देता है.
  • updateMask में, पैसे चुकाकर ली जाने वाली सदस्यता के उन पहलुओं की जानकारी दी जाती है जिन्हें अपडेट करना है. इसमें ये चीज़ें भी शामिल हैं:
    • role: चैट स्पेस में उपयोगकर्ता की भूमिका से तय होता है कि वे उस स्पेस में किन कार्रवाइयों को अनुमति देंगे. आपको ये वैल्यू दिख सकती हैं:
      • ROLE_MEMBER: स्पेस का सदस्य. उपयोगकर्ता के पास बुनियादी अनुमतियां होती हैं, जैसे स्पेस में मैसेज भेजना. 1:1 और बिना नाम वाली ग्रुप बातचीत में, सभी की यह भूमिका होती है.
      • ROLE_MANAGER: स्पेस मैनेजर. उपयोगकर्ता के पास सभी बुनियादी अनुमतियां और एडमिन की अनुमतियां होती हैं. इनकी मदद से, वे स्पेस को मैनेज कर सकते हैं, जैसे कि सदस्यों को जोड़ना या हटाना. यह सुविधा सिर्फ़ उन स्पेस में काम करती है जहां spaceType, SPACE (नाम वाले स्पेसेज़) है.

स्पेस के किसी सदस्य को स्पेस मैनेजर बनाना

इस उदाहरण में, स्पेस के किसी सदस्य को स्पेस मैनेजर बनाया जाता है. इसके लिए, body में role को ROLE_MANAGER के तौर पर सेट करना होता है. इससे स्पेस के अपडेट किए गए सदस्यों के एट्रिब्यूट की जानकारी मिलती है:

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: स्पेस का नाम, जिसे Chat API में spaces.list तरीके से या स्पेस के यूआरएल से पाया जा सकता है.

    • MEMBERSHIP: पैसे चुकाकर ली जाने वाली सदस्यता का नाम, जिसे Chat API में spaces.members.list तरीके से लिया जा सकता है.

  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: स्पेस का नाम, जिसे Chat API में spaces.list तरीके से या स्पेस के यूआरएल से पाया जा सकता है.

    • MEMBERSHIP: पैसे चुकाकर ली जाने वाली सदस्यता का नाम, जिसे Chat API में spaces.members.list तरीके से लिया जा सकता है.

  4. अपनी वर्किंग डायरेक्ट्री में, यह सैंपल बनाएं और चलाएं:

    python3 chat_membership_update.js
    

Apps Script

इस उदाहरण में, बेहतर चैट सेवा का इस्तेमाल करके Chat API को कॉल किया जाता है.

  1. Apps Script प्रोजेक्ट की appsscript.json फ़ाइल में, chat.memberships अनुमति का स्कोप जोड़ें:

    "oauthScopes": [
      "https://www.googleapis.com/auth/chat.memberships"
    ]
    
  2. Apps Script प्रोजेक्ट के कोड में, इस तरह का कोई फ़ंक्शन जोड़ें:

    /**
     * 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 का इंस्टेंस दिखाता है.

स्पेस मैनेजर को नियमित सदस्य बनाना

इस उदाहरण में, स्पेस मैनेजर को स्पेस का सामान्य सदस्य बनाया गया है. इसके लिए, body में role को ROLE_MEMBER के तौर पर सेट किया गया है, ताकि स्पेस के अपडेट किए गए एट्रिब्यूट की जानकारी दी जा सके:

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: स्पेस का नाम, जिसे Chat API में spaces.list तरीके से या स्पेस के यूआरएल से पाया जा सकता है.

    • MEMBERSHIP: पैसे चुकाकर ली जाने वाली सदस्यता का नाम, जिसे Chat API में spaces.members.list तरीके से लिया जा सकता है.

  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: स्पेस का नाम, जिसे Chat API में spaces.list तरीके से या स्पेस के यूआरएल से पाया जा सकता है.

    • MEMBERSHIP: पैसे चुकाकर ली जाने वाली सदस्यता का नाम, जिसे Chat API में spaces.members.list तरीके से लिया जा सकता है.

  4. अपनी वर्किंग डायरेक्ट्री में, यह सैंपल बनाएं और चलाएं:

    python3 chat_membership_update.js
    

Apps Script

इस उदाहरण में, बेहतर चैट सेवा का इस्तेमाल करके Chat API को कॉल किया जाता है.

  1. Apps Script प्रोजेक्ट की appsscript.json फ़ाइल में, chat.memberships अनुमति का स्कोप जोड़ें:

    "oauthScopes": [
      "https://www.googleapis.com/auth/chat.memberships"
    ]
    
  2. Apps Script प्रोजेक्ट के कोड में, इस तरह का कोई फ़ंक्शन जोड़ें:

    /**
     * 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 का इंस्टेंस दिखाता है.