किसी उपयोगकर्ता, Google ग्रुप या Google Chat ऐप्लिकेशन को स्पेस में जोड़ना या न्योता भेजना

इस गाइड में, membership संसाधन पर create तरीके को इस्तेमाल करने का तरीका बताया गया है किसी उपयोगकर्ता, Google ग्रुप को न्योता भेजने या जोड़ने के लिए Google Chat API या चैट ऐप्लिकेशन को स्पेस में जोड़ना की सदस्यता. सदस्यता बनाते समय, अगर बताए गए सदस्य के पास नीति को अपने-आप स्वीकार करने की सुविधा बंद करने के बाद, उन्हें न्योता भेजा जाता है और उन्हें स्पेस स्वीकार करना होगा शामिल होने से पहले आमंत्रण. अगर ऐसा नहीं है, तो सदस्यता बनाने पर सदस्य को जोड़ दिया जाता है तो सीधे उस स्पेस में ले जाएं.

कॉन्टेंट बनाने Membership संसाधन यह बताता है कि किसी उपयोगकर्ता या Google Chat ऐप्लिकेशन को किसी स्पेस का हिस्सा है या नहीं है.

ज़रूरी शर्तें

Python

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

    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 और एनपीएम
  • Node.js के लिए Google की नई क्लाइंट लाइब्रेरी. उन्हें इंस्टॉल करने के लिए, कमांड-लाइन इंटरफ़ेस में नीचे दिया गया लिंक डालें:

    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 तक.
    • आईडी उपयोगकर्ता को डायरेक्ट्री एपीआई में डालें.
    • उपयोगकर्ता का ईमेल पता. उदाहरण के लिए, 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 तरीका या स्पेस के यूआरएल से मिलेगी.

    • 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',
      ];
    
      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 तरीका या स्पेस के यूआरएल से मिलेगी.

    • USER: यूज़र आईडी.

  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 नाम वाला ग्रुप लौटाता है, फिर सेट करता है groups/123456789 के लिए membership.groupMember.name.

Google Groups, किसी ग्रुप चैट या डायरेक्ट मैसेज में नहीं, बल्कि सिर्फ़ नाम वाला स्पेस. यहां दिए गए उदाहरण में, ग्रुप को नाम वाले स्पेस में जोड़ा गया है:

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. कोड में, इन्हें बदलें:

    • SPACE: स्पेस का नाम, जो तो आपको यहां से spaces.list तरीका या स्पेस के यूआरएल से मिलेगी.

    • GROUP: ग्रुप आईडी.

  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. कोड में, इन्हें बदलें:

    • SPACE: स्पेस का नाम होता है. इसे यहां से हासिल किया जा सकता है spaces.list तरीका या स्पेस के यूआरएल से मिलेगी.

    • GROUP: ग्रुप आईडी.

  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 तरीका या स्पेस के यूआरएल से मिलेगी.

  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 तरीका या स्पेस के यूआरएल से मिलेगी.

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

    node add-app-to-space.js
    

Chat API, membership जो बनाई गई ऐप्लिकेशन सदस्यता की जानकारी देता है.