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

यह गाइड बताती है कि इसके Message संसाधन पर patch तरीके का इस्तेमाल कैसे किया जाए किसी स्पेस में टेक्स्ट या कार्ड मैसेज अपडेट करने के लिए, Google Chat API का इस्तेमाल करें. पब्लिशर आईडी के साथ अपनी साइट की जैसे कि मैसेज की विशेषताओं को बदलने के लिए मैसेज. उदाहरण के लिए, मैसेज में क्या बताया गया है या कार्ड. आप कोई पाठ संदेश भी आगे जोड़ सकते हैं या टेक्स्ट मैसेज में कार्ड जोड़ें.

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

Chat API में, Chat मैसेज को Message संसाधन. Chat का इस्तेमाल करने वाले लोग सिर्फ़ ऐसे मैसेज भेज सकते हैं जिनमें टेक्स्ट हो. चैट ऐप्लिकेशन, मैसेज की कई अन्य सुविधाओं का इस्तेमाल कर सकते हैं. इनमें ये सुविधाएं शामिल हैं स्टैटिक या इंटरैक्टिव यूज़र इंटरफ़ेस दिखाना, ताकि जानकारी इकट्ठा की जा सके साथ ही, मैसेज को निजी तौर पर शेयर करने की सुविधा मिलती है. मैसेज सेवा के बारे में ज़्यादा जानने के लिए Chat API के लिए उपलब्ध सुविधाएं इस्तेमाल करने के लिए, Google Chat मैसेज की खास जानकारी.

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

Python

  • कारोबार या एंटरप्राइज़ Google Workspace खाता, जिसके पास इसका ऐक्सेस है Google Chat.
  • Python 3.6 या इससे नया वर्शन
  • पीआईपी पैकेज मैनेजमेंट टूल
  • Google की नई क्लाइंट लाइब्रेरी. उन्हें इंस्टॉल या अपडेट करने के लिए, अपने कमांड-लाइन इंटरफ़ेस में यह कमांड चलाएं:
    pip3 install --upgrade google-api-python-client google-auth-oauthlib
    

उपयोगकर्ता की पुष्टि करने की सुविधा की मदद से, कोई मैसेज अपडेट करें या कार्ड मैसेज के साथ टेक्स्ट मैसेज जोड़ें

अपडेट करने के लिए मैसेज के साथ उपयोगकर्ता की पुष्टि, पास नीचे दी गई शर्तों को पूरा करें:

  • 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 updated message.
        print(result)
    
    if __name__ == '__main__':
        main()
    
  3. कोड में, इन्हें बदलें:

    • SPACE: स्पेस का नाम, जिसे यहां से ऐक्सेस किया जा सकता है यह spaces.list तरीका या स्पेस के यूआरएल से मिलेगी.
    • MESSAGE: मैसेज का नाम, जिसे आप पा सकते हैं एसिंक्रोनस तरीके से मैसेज बनाने के बाद, रिस्पॉन्स वाले मुख्य हिस्से से का इस्तेमाल करें या पसंद के मुताबिक बनाया गया नाम बनाते समय एक मैसेज असाइन किया गया.
  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: स्पेस का नाम, जिसे यहां से ऐक्सेस किया जा सकता है यह spaces.list तरीका या स्पेस के यूआरएल से मिलेगी.
    • MESSAGE: मैसेज का नाम, जिसे आप पा सकते हैं एसिंक्रोनस तरीके से मैसेज बनाने के बाद, रिस्पॉन्स वाले मुख्य हिस्से से का इस्तेमाल करें या पसंद के मुताबिक बनाया गया नाम बनाते समय एक मैसेज असाइन किया गया.
  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: स्पेस का नाम, जिसे यहां से ऐक्सेस किया जा सकता है यह spaces.list तरीका या स्पेस के यूआरएल से मिलेगी.

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

  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 updated message.
        print(result)
    
    if __name__ == '__main__':
        main()
    
  3. कोड में, इन्हें बदलें:

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

    python3 chat_update_text_message_user.py