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

इस गाइड में बताया गया है कि किसी टेक्स्ट या कार्ड मैसेज की जानकारी देने के लिए, Google Chat API के Message संसाधन पर get तरीके का इस्तेमाल कैसे किया जाता है.

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 Cloud प्रोजेक्ट जिसमें Google Chat API चालू हो और उसे कॉन्फ़िगर किया गया हो. तरीका जानने के लिए, Google Chat ऐप्लिकेशन बनाना देखें.
  • Chat ऐप्लिकेशन के लिए अनुमति देने की सुविधा कॉन्फ़िगर की गई. मैसेज पाने के लिए, पुष्टि करने के इन दोनों तरीकों का इस्तेमाल किया जा सकता है:

    • उपयोगकर्ता की पुष्टि करने से, chat.messages.readonly या chat.messages के अनुमति वाले दायरे में वे मैसेज मिल सकते हैं जिनका ऐक्सेस उपयोगकर्ता के पास है.
    • ऐप्लिकेशन की पुष्टि करने की सुविधा से, ऐप्लिकेशन पर भेजे गए मैसेज पाए जा सकते हैं. साथ ही, chat.bot के लिए अनुमति का यह स्कोप भी इस्तेमाल किया जा सकता है.

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

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

  • अनुमति देने के लिए, chat.messages.readonly या chat.messages का स्कोप बताएं.
  • Message संसाधन पर get वाला तरीका कॉल करें.
  • मैसेज पाने के लिए, संसाधन के नाम के तौर पर name को सेट करें.

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

Python

  1. अपनी वर्किंग डायरेक्ट्री में, chat_message_get_user.py नाम की एक फ़ाइल बनाएं.
  2. chat_message_get_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.readonly"]
    
    def main():
        '''
        Authenticates with Chat API via user credentials,
        then gets 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)
    
        # Use the service endpoint to call Chat API.
        result = chat.spaces().messages().get(
    
            # The message to get.
            #
            # 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'
    
        ).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_message_get_user.py
    

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

ऐप्लिकेशन की पुष्टि करने वाला मैसेज पाएं

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

  • chat.bot अनुमति का स्कोप बताएं.
  • Message संसाधन पर get वाला तरीका कॉल करें.
  • मैसेज पाने के लिए, संसाधन के नाम के तौर पर name को सेट करें.

नीचे दिए गए उदाहरण में, ऐप्लिकेशन की पुष्टि करने वाला मैसेज मिला है:

Python

  1. अपनी वर्किंग डायरेक्ट्री में, chat_get_message_app.py नाम की एक फ़ाइल बनाएं.
  2. chat_get_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)
    
    # Get a Chat message.
    result = chat.spaces().messages().get(
    
        # The message to get.
        #
        # 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'
    
      ).execute()
    
    # Print Chat API's response in your command line interface.
    print(result)
    
  3. कोड में, इन्हें बदलें:

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

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

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

    python3 chat_get_message_app.py
    

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