मैसेज अपडेट करना

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

Chat API में भी update वाला तरीका इस्तेमाल किया जा सकता है. हालांकि, हमारा सुझाव है कि आप patch तरीके का इस्तेमाल करें, क्योंकि यह PATCH एचटीटीपी अनुरोध का इस्तेमाल करता है, जबकि update एचटीटीपी अनुरोध का इस्तेमाल करता है.PUT ज़्यादा जानने के लिए, AIP-134 का PATCH और PUT सेक्शन देखें.

Message रिसॉर्स, Google Chat में टेक्स्ट या कार्ड मैसेज दिखाता है. आप इनसे जुड़े तरीकों का इस्तेमाल करके, Google Chat API में create, get, update या delete को मैसेज भेज सकते हैं. मैसेज और कार्ड मैसेज के बारे में ज़्यादा जानने के लिए, Google Chat मैसेज की खास जानकारी देखें.

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

Python

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

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

उपयोगकर्ता की पुष्टि करने के लिए, मैसेज अपडेट करें या कार्ड मैसेज से पहले टेक्स्ट मैसेज जोड़ें

किसी टेक्स्ट मैसेज को उपयोगकर्ता की पुष्टि करने के साथ अपडेट करने के लिए, अपने अनुरोध में इन्हें पास करें:

  • chat.messages की अनुमति का दायरा.
  • अपडेट करने के लिए मैसेज का name.
  • updateMask='text'
  • ऐसा body जो अपडेट किए गए मैसेज के बारे में बताता है.

अगर अपडेट किया गया मैसेज एक कार्ड मैसेज है, तो टेक्स्ट मैसेज कार्ड मैसेज (जो दिखता रहता है) के आगे जुड़ जाता है.

यहाँ टेक्स्ट मैसेज को अपडेट करने या उपयोगकर्ता की पुष्टि करने वाले कार्ड मैसेज में टेक्स्ट मैसेज जोड़ने का तरीका बताया गया है:

Python

  1. अपनी वर्किंग डायरेक्ट्री में, chat_update_text_message_user.py नाम की फ़ाइल बनाएं.
  2. chat_update_text_message_user.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.messages"]
    
    def main():
        '''
        Authenticates with Chat API via user credentials,
        then updates a message.
        '''
    
        # 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)
    
        # Update a Chat message.
        result = chat.spaces().messages().patch(
    
          # The message to update, and the updated message.
          #
          # Replace SPACE with a space name.
          # Obtain the space name from the spaces resource of Chat API,
          # or from a space's URL.
          #
          # Replace MESSAGE with a message name.
          # Obtain the message name from the response body returned
          # after creating a message asynchronously with Chat REST API.
          name='spaces/SPACE/messages/MESSAGE',
          updateMask='text',
          body={'text': 'Updated message!'}
    
        ).execute()
    
        # Prints details about the created membership.
        print(result)
    
    if __name__ == '__main__':
        main()
    
  3. कोड में, इन्हें बदलें:

    • SPACE: स्पेस का नाम, जिसे Chat API में spaces.list तरीके से या स्पेस के यूआरएल से पाया जा सकता है.
    • MESSAGE: मैसेज का नाम, जिसे Chat API की मदद से एसिंक्रोनस तरीके से मैसेज बनाने के बाद रिस्पॉन्स के मुख्य हिस्से से पाया जा सकता है या मैसेज बनाते समय इसे असाइन किए गए कस्टम नाम का इस्तेमाल किया जा सकता है.
  4. अपनी वर्किंग डायरेक्ट्री में, यह सैंपल बनाएं और चलाएं:

    python3 chat_update_text_message_user.py
    

ऐप्लिकेशन की पुष्टि करके, मैसेज अपडेट करें या कार्ड मैसेज के आगे टेक्स्ट मैसेज जोड़ें

किसी टेक्स्ट मैसेज को ऐप्लिकेशन की पुष्टि के साथ अपडेट करने के लिए, अपने अनुरोध में इन्हें पास करें:

  • chat.bot की अनुमति का दायरा.
  • अपडेट करने के लिए मैसेज का name.
  • updateMask='text'
  • ऐसा body जो अपडेट किए गए मैसेज के बारे में बताता है.

अगर अपडेट किया गया मैसेज एक कार्ड मैसेज है, तो कार्ड मैसेज (जो दिखता रहता है) के साथ मैसेज जुड़ जाता है.

यहाँ बताया गया है कि टेक्स्ट मैसेज को मैसेज में कैसे अपडेट करें या ऐप्लिकेशन की पुष्टि से, कार्ड मैसेज में टेक्स्ट मैसेज से पहले जोड़ें:

Python

  1. अपनी वर्किंग डायरेक्ट्री में, chat_update_text_message_app.py नाम की फ़ाइल बनाएं.
  2. chat_update_text_message_app.py में यह कोड शामिल करें:

    from google.oauth2 import service_account
    from apiclient.discovery import build
    
    # Specify required scopes.
    SCOPES = ['https://www.googleapis.com/auth/chat.bot']
    
    # Specify service account details.
    CREDENTIALS = (
        service_account.Credentials.from_service_account_file('credentials.json')
        .with_scopes(SCOPES)
    )
    
    # Build the URI and authenticate with the service account.
    chat = build('chat', 'v1', credentials=CREDENTIALS)
    
    # Update a Chat message.
    result = chat.spaces().messages().patch(
    
      # The message to update, and the updated message.
      #
      # Replace SPACE with a space name.
      # Obtain the space name from the spaces resource of Chat API,
      # or from a space's URL.
      #
      # Replace MESSAGE with a message name.
      # Obtain the message name from the response body returned
      # after creating a message asynchronously with Chat REST API.
      name='spaces/SPACE/messages/MESSAGE',
      updateMask='text',
      body={'text': 'Updated message!'}
    
    ).execute()
    
    # Print Chat API's response in your command line interface.
    print(result)
    
  3. कोड में, इन्हें बदलें:

    • SPACE: स्पेस का नाम, जिसे Chat API में spaces.list तरीके से या स्पेस के यूआरएल से पाया जा सकता है.
    • MESSAGE: मैसेज का नाम, जिसे Chat API की मदद से एसिंक्रोनस तरीके से मैसेज बनाने के बाद रिस्पॉन्स के मुख्य हिस्से से पाया जा सकता है या मैसेज बनाते समय इसे असाइन किए गए कस्टम नाम का इस्तेमाल किया जा सकता है.
  4. अपनी वर्किंग डायरेक्ट्री में, यह सैंपल बनाएं और चलाएं:

    python3 chat_update_text_message_app.py
    

कार्ड मैसेज अपडेट करें या किसी मैसेज में कार्ड मैसेज जोड़ें

किसी कार्ड मैसेज को अपडेट करने के लिए, अपने अनुरोध में इसे पास करें:

  • chat.bot की अनुमति का दायरा. कार्ड मैसेज को अपडेट करने के लिए, ऐप्लिकेशन की पुष्टि करना ज़रूरी है.
  • अपडेट करने के लिए मैसेज का name.
  • updateMask='cardsV2'
  • ऐसा body जो अपडेट किए गए मैसेज के बारे में बताता है.

अगर अपडेट किया गया मैसेज एक टेक्स्ट मैसेज है, तो टेक्स्ट मैसेज में एक कार्ड जुड़ जाता है (जो दिखता रहता है). अगर अपडेट किया गया मैसेज अपने-आप एक कार्ड है, तो दिखाया गया कार्ड अपडेट किया जाता है.

यहां मैसेज को कार्ड मैसेज में अपडेट करने का तरीका बताया गया है:

Python

  1. अपनी वर्किंग डायरेक्ट्री में, chat_update_card_message.py नाम की फ़ाइल बनाएं.
  2. chat_update_card_message.py में यह कोड शामिल करें:

    from google.oauth2 import service_account
    from apiclient.discovery import build
    
    # Specify required scopes.
    SCOPES = ['https://www.googleapis.com/auth/chat.bot']
    
    # Specify service account details.
    CREDENTIALS = (
        service_account.Credentials.from_service_account_file('credentials.json')
        .with_scopes(SCOPES)
    )
    
    # Build the URI and authenticate with the service account.
    chat = build('chat', 'v1', credentials=CREDENTIALS)
    
    # Update a Chat message.
    result = chat.spaces().messages().patch(
    
      # The message to update, and the updated message.
      #
      # Replace SPACE with a space name.
      # Obtain the space name from the spaces resource of Chat API,
      # or from a space's URL.
      #
      # Replace MESSAGE with a message name.
      # Obtain the message name from the response body returned
      # after creating a message asynchronously with Chat REST API.
      name='spaces/SPACE/messages/MESSAGE',
      updateMask='cardsV2',
      body=
      {
        'cardsV2': [{
          'cardId': 'updateCardMessage',
          'card': {
            'header': {
              'title': 'An Updated Card Message!',
              'subtitle': 'Updated with Chat REST API',
              'imageUrl': 'https://developers.google.com/chat/images/chat-product-icon.png',
              'imageType': 'CIRCLE'
            },
            'sections': [
              {
                'widgets': [
                  {
                    'buttonList': {
                      'buttons': [
                        {
                          'text': 'Read the docs!',
                          'onClick': {
                            'openLink': {
                              'url': 'https://developers.google.com/chat'
                            }
                          }
                        }
                      ]
                    }
                  }
                ]
              }
            ]
          }
        }]
      }
    
    ).execute()
    
    # Print Chat API's response in your command line interface.
    print(result)
    
  3. कोड में, इन्हें बदलें:

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

    • MESSAGE: मैसेज का नाम, जिसे Chat API की मदद से एसिंक्रोनस तरीके से मैसेज बनाने के बाद रिस्पॉन्स के मुख्य हिस्से से पाया जा सकता है या मैसेज बनाते समय इसे असाइन किए गए कस्टम नाम का इस्तेमाल किया जा सकता है.

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

    python3 chat_update_card_message.py
    

Chat API Message का एक इंस्टेंस दिखाता है, जिसमें अपडेट किए गए मैसेज की जानकारी दी जाती है.

कई फ़ील्ड पाथ वाले मैसेज को एक साथ अपडेट करना

जब किसी मैसेज को अपडेट किया जाता है, तो एक बार में एक से ज़्यादा मैसेज फ़ील्ड पाथ को अपडेट किया जा सकता है. उदाहरण के लिए, मैसेज अपडेट करने के अनुरोध में, एक ही समय पर text और cardsv2 फ़ील्ड पाथ में बदलाव किया जा सकता है. इससे मैसेज का टेक्स्ट और कार्ड, दोनों अपडेट हो जाते हैं. अगर मैसेज में सिर्फ़ टेक्स्ट शामिल है और कोई कार्ड नहीं है, तो मैसेज में एक कार्ड जोड़ा जाता है. इस्तेमाल किए जा सकने वाले फ़ील्ड पाथ के बारे में ज़्यादा जानकारी के लिए, updateMask पैरामीटर देखें.

उपयोगकर्ता की पुष्टि वाले मैसेज के text और card, दोनों को अपडेट करने के लिए, अपने अनुरोध में यह जानकारी शामिल करें:

  • chat.messages की अनुमति का दायरा.
  • अपडेट करने के लिए मैसेज का name.
  • ऐसा updateMask जो अपडेट करने के लिए मैसेज फ़ील्ड पाथ तय करता है. इन्हें कॉमा लगाकर अलग किया जाता है: updateMask='text', 'cardsV2'.

  • body, जो अपडेट किए गए मैसेज की जानकारी देता है. इसमें अपडेट किए गए सभी फ़ील्ड पाथ शामिल होते हैं.

उपयोगकर्ता की पुष्टि करने की सुविधा वाले मैसेज में, text और cardsV2 फ़ील्ड पाथ को अपडेट करने का तरीका यहां बताया गया है:

Python

  1. अपनी वर्किंग डायरेक्ट्री में, chat_update_text_message_user.py नाम की फ़ाइल बनाएं.
  2. chat_update_text_message_user.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.messages"]
    
    def main():
        '''
        Authenticates with Chat API via user credentials,
        then updates a message.
        '''
    
        # 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)
    
        # Update a Chat message.
        result = chat.spaces().messages().patch(
    
          # The message to update, and the updated message.
          #
          # Replace SPACE with a space name.
          # Obtain the space name from the spaces resource of Chat API,
          # or from a space's URL.
          #
          # Replace MESSAGE with a message name.
          # Obtain the message name from the response body returned
          # after creating a message asynchronously with Chat REST API.
          name='spaces/SPACE/messages/MESSAGE',
          updateMask='text,cardsV2',
          body=
          {'text': 'Updated message!',
                'cardsV2': [{
                  'cardId': 'updateCardMessage',
                  'card': {
                    'header': {
                      'title': 'An Updated Card Message!',
                      'subtitle': 'Updated with Chat REST API',
                      'imageUrl': 'https://developers.google.com/chat/images/chat-product-icon.png',
                      'imageType': 'CIRCLE'
                    },
                    'sections': [
                      {
                        'widgets': [
                          {
                            'buttonList': {
                              'buttons': [
                                {
                                  'text': 'Read the docs!',
                                  'onClick': {
                                    'openLink': {
                                      'url': 'https://developers.google.com/chat'
                                    }
                                  }
                                }
                              ]
                            }
                          }
                        ]
                      }
                    ]
                  }
                }]
          }
    
        ).execute()
    
        # Prints details about the created membership.
        print(result)
    
    if __name__ == '__main__':
        main()
    
  3. कोड में, इन्हें बदलें:

    • SPACE: स्पेस का नाम, जिसे Chat API में spaces.list तरीके से या स्पेस के यूआरएल से पाया जा सकता है.
    • MESSAGE: मैसेज का नाम, जिसे Chat API की मदद से एसिंक्रोनस तरीके से मैसेज बनाने के बाद रिस्पॉन्स के मुख्य हिस्से से पाया जा सकता है या मैसेज बनाते समय इसे असाइन किए गए कस्टम नाम का इस्तेमाल किया जा सकता है.
  4. अपनी वर्किंग डायरेक्ट्री में, यह सैंपल बनाएं और चलाएं:

    python3 chat_update_text_message_user.py