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

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

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

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

Node.js

  • आपके पास Business या Enterprise वर्शन वाला Google Workspace खाता होना चाहिए. साथ ही, आपके पास Google Chat को ऐक्सेस करने की अनुमति होनी चाहिए.

Python

  • आपके पास Business या Enterprise वर्शन वाला Google Workspace खाता होना चाहिए. साथ ही, आपके पास Google Chat को ऐक्सेस करने की अनुमति होनी चाहिए.

Java

  • आपके पास Business या Enterprise वर्शन वाला Google Workspace खाता होना चाहिए. साथ ही, आपके पास Google Chat को ऐक्सेस करने की अनुमति होनी चाहिए.

Apps Script

  • आपके पास Business या Enterprise वर्शन वाला Google Workspace खाता होना चाहिए. साथ ही, आपके पास Google Chat को ऐक्सेस करने की अनुमति होनी चाहिए.

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

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

  • chat.messages.readonly या chat.messages ऑथराइज़ेशन स्कोप के बारे में बताएं.
  • GetMessage() तरीके को कॉल करें.
  • पाने के लिए मैसेज के संसाधन का नाम name पर सेट करें.

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

Node.js

chat/client-libraries/cloud/get-message-user-cred.js
import {createClientWithUserCredentials} from './authentication-utils.js';

const USER_AUTH_OAUTH_SCOPES = ['https://www.googleapis.com/auth/chat.messages.readonly'];

// This sample shows how to get message with user credential
async function main() {
  // Create a client
  const chatClient = await createClientWithUserCredentials(USER_AUTH_OAUTH_SCOPES);

  // Initialize request argument(s)
  const request = {
    // Replace SPACE_NAME and MESSAGE_NAME here
    name: 'spaces/SPACE_NAME/messages/MESSAGE_NAME'
  };

  // Make the request
  const response = await chatClient.getMessage(request);

  // Handle the response
  console.log(response);
}

main().catch(console.error);

Python

chat/client-libraries/cloud/get_message_user_cred.py
from authentication_utils import create_client_with_user_credentials
import google.oauth2.credentials

from google.apps import chat_v1 as google_chat

SCOPES = ["https://www.googleapis.com/auth/chat.messages.readonly"]

# This sample shows how to get message with user credential
def get_message_with_user_cred():
    # Create a client
    client = create_client_with_user_credentials(SCOPES)

    # Initialize request argument(s)
    request = google_chat.GetMessageRequest(
        # Replace SPACE_NAME and MESSAGE_NAME here
        name = "spaces/SPACE_NAME/messages/MESSAGE_NAME",
    )

    # Make the request
    response = client.get_message(request)

    # Handle the response
    print(response)

get_message_with_user_cred()

Java

chat/client-libraries/cloud/src/main/java/com/google/workspace/api/chat/samples/GetMessageUserCred.java
import com.google.chat.v1.ChatServiceClient;
import com.google.chat.v1.GetMessageRequest;
import com.google.chat.v1.Message;

// This sample shows how to get message with user credential.
public class GetMessageUserCred {

  private static final String SCOPE =
    "https://www.googleapis.com/auth/chat.messages.readonly";

  public static void main(String[] args) throws Exception {
    try (ChatServiceClient chatServiceClient =
        AuthenticationUtils.createClientWithUserCredentials(
          ImmutableList.of(SCOPE))) {
      GetMessageRequest.Builder request = GetMessageRequest.newBuilder()
        // replace SPACE_NAME and MESSAGE_NAME here
        .setName("spaces/SPACE_NAME/members/MESSAGE_NAME");
      Message response = chatServiceClient.getMessage(request.build());

      System.out.println(JsonFormat.printer().print(response));
    }
  }
}

Apps Script

chat/advanced-service/Main.gs
/**
 * This sample shows how to get message with user credential
 * 
 * It relies on the OAuth2 scope 'https://www.googleapis.com/auth/chat.messages.readonly'
 * referenced in the manifest file (appsscript.json).
 */
function getMessageUserCred() {
  // Initialize request argument(s)
  // TODO(developer): Replace SPACE_NAME and MESSAGE_NAME here
  const name = 'spaces/SPACE_NAME/messages/MESSAGE_NAME';

  // Make the request
  const response = Chat.Spaces.Messages.get(name);

  // Handle the response
  console.log(response);
}

इस सैंपल को चलाने के लिए, इन्हें बदलें:

  • SPACE_NAME: स्पेस के name का आईडी. आईडी पाने के लिए, ListSpaces() तरीके को कॉल करें या स्पेस के यूआरएल से आईडी पाएं.
  • MESSAGE_NAME: मैसेज के name से मिला आईडी. Chat API की मदद से, मैसेज को एसिंक्रोनस तरीके से बनाने के बाद, जवाब के मुख्य हिस्से से आईडी पाया जा सकता है. इसके अलावा, मैसेज बनाते समय असाइन किए गए कस्टम नाम से भी आईडी पाया जा सकता है.

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

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

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

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

Node.js

chat/client-libraries/cloud/get-message-app-cred.js
import {createClientWithAppCredentials} from './authentication-utils.js';

// This sample shows how to get message with app credential
async function main() {
  // Create a client
  const chatClient = createClientWithAppCredentials();

  // Initialize request argument(s)
  const request = {
    // Replace SPACE_NAME and MESSAGE_NAME here
    name: 'spaces/SPACE_NAME/messages/MESSAGE_NAME'
  };

  // Make the request
  const response = await chatClient.getMessage(request);

  // Handle the response
  console.log(response);
}

main().catch(console.error);

Python

chat/client-libraries/cloud/get_message_app_cred.py
from authentication_utils import create_client_with_app_credentials
from google.apps import chat_v1 as google_chat

# This sample shows how to get message with app credential
def get_message_with_app_cred():
    # Create a client
    client = create_client_with_app_credentials()

    # Initialize request argument(s)
    request = google_chat.GetMessageRequest(
        # Replace SPACE_NAME and MESSAGE_NAME here
        name = 'spaces/SPACE_NAME/messages/MESSAGE_NAME',
    )

    # Make the request
    response = client.get_message(request=request)

    # Handle the response
    print(response)

get_message_with_app_cred()

Java

chat/client-libraries/cloud/src/main/java/com/google/workspace/api/chat/samples/GetMessageAppCred.java
import com.google.chat.v1.ChatServiceClient;
import com.google.chat.v1.GetMessageRequest;
import com.google.chat.v1.Message;

// This sample shows how to get message with app credential.
public class GetMessageAppCred {

  public static void main(String[] args) throws Exception {
    try (ChatServiceClient chatServiceClient =
        AuthenticationUtils.createClientWithAppCredentials()) {
      GetMessageRequest.Builder request = GetMessageRequest.newBuilder()
        // replace SPACE_NAME and MESSAGE_NAME here
        .setName("spaces/SPACE_NAME/members/MESSAGE_NAME");
      Message response = chatServiceClient.getMessage(request.build());

      System.out.println(JsonFormat.printer().print(response));
    }
  }
}

Apps Script

chat/advanced-service/Main.gs
/**
 * This sample shows how to get message with app credential
 * 
 * It relies on the OAuth2 scope 'https://www.googleapis.com/auth/chat.bot'
 * used by service accounts.
 */
function getMessageAppCred() {
  // Initialize request argument(s)
  // TODO(developer): Replace SPACE_NAME and MESSAGE_NAME here
  const name = 'spaces/SPACE_NAME/messages/MESSAGE_NAME';
  const parameters = {};

  // Make the request
  const response = Chat.Spaces.Messages.get(name, parameters, getHeaderWithAppCredentials());

  // Handle the response
  console.log(response);
}

इस सैंपल को चलाने के लिए, इन्हें बदलें:

  • SPACE_NAME: स्पेस के name का आईडी. आईडी पाने के लिए, ListSpaces() तरीके को कॉल करें या स्पेस के यूआरएल से आईडी पाएं.
  • MESSAGE_NAME: मैसेज के name से मिला आईडी. Chat API की मदद से, मैसेज को एसिंक्रोनस तरीके से बनाने के बाद, जवाब के मुख्य हिस्से से आईडी पाया जा सकता है. इसके अलावा, मैसेज बनाते समय असाइन किए गए कस्टम नाम से भी आईडी पाया जा सकता है.

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

एडमिन की मंज़ूरी के बाद, Chat ऐप्लिकेशन के तौर पर मैसेज पाना

इस प्रोग्राम में शामिल होने पर, आपको कुछ सुविधाओं को रिलीज़ होने से पहले ही इस्तेमाल करने का ऐक्सेस मिलता है.

chat.app.* के लिए पुष्टि करने वाले ऐप्लिकेशन को एडमिन से एक बार अनुमति लेनी होगी.

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

  • GetMessage() तरीके को कॉल करें.
  • chat.app.messages.readonly ऑथराइज़ेशन का स्कोप तय करें.
  • पाने के लिए मैसेज के संसाधन का नाम name पर सेट करें.

एपीआई पासकोड बनाना

डेवलपर प्रीव्यू एपीआई के तरीके को कॉल करने के लिए, आपको एपीआई डिस्कवरी दस्तावेज़ के डेवलपर प्रीव्यू वर्शन का इस्तेमाल करना होगा. यह वर्शन सार्वजनिक नहीं होता. अनुरोध की पुष्टि करने के लिए, आपको एपीआई पासकोड डालना होगा.

एपीआई पासकोड बनाने के लिए, अपने ऐप्लिकेशन का Google Cloud प्रोजेक्ट खोलें और यह तरीका अपनाएं:

  1. Google Cloud console में, मेन्यू > एपीआई और सेवाएं > क्रेडेंशियल पर जाएं.

    क्रेडेंशियल पर जाएं

  2. क्रेडेंशियल बनाएं > एपीआई कुंजी पर क्लिक करें.
  3. आपको नई एपीआई कुंजी दिखेगी.
    • अपने ऐप्लिकेशन के कोड में इस्तेमाल करने के लिए, एपीआई पासकोड कॉपी करने के लिए, कॉपी करें पर क्लिक करें. एपीआई कुंजी, आपके प्रोजेक्ट के क्रेडेंशियल के "एपीआई कुंजियां" सेक्शन में भी देखी जा सकती है.
    • हमारा सुझाव है कि बिना अनुमति के इस्तेमाल को रोकने के लिए, यह तय करें कि एपीआई कुंजी का इस्तेमाल कहां और किन एपीआई के लिए किया जा सकता है. ज़्यादा जानकारी के लिए, एपीआई पर पाबंदियां जोड़ना लेख पढ़ें.

Chat API को कॉल करने वाली स्क्रिप्ट लिखना

ऐप्लिकेशन की पुष्टि करने और एडमिन की मंज़ूरी के साथ-साथ Chat REST API की मदद से भेजे गए मैसेज के बारे में जानकारी पाने का तरीका यहां बताया गया है:

Python

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

    from google.oauth2 import service_account
    from apiclient.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.app.messages.readonly"]
    
    def main():
        '''
        Authenticates with Chat API using app authentication,
        then gets details about a message.
        '''
    
        # Specify service account details.
        creds = (
            service_account.Credentials.from_service_account_file('credentials.json')
            .with_scopes(SCOPES)
        )
    
        # Build a service endpoint for Chat API.
        chat = build('chat', 'v1', credentials=creds, discoveryServiceUrl='https://chat.googleapis.com/$discovery/rest?version=v1&labels=DEVELOPER_PREVIEW&key=API_KEY')
    
        # Use the service endpoint to call Chat API.
        result = chat.spaces().messages().get(
    
            # The message to get details about.
            #
            # Replace SPACE_NAME with a space name.
            # Obtain the space name from the spaces resource of Chat API,
            # or from a space's URL.
            name='spaces/SPACE_NAME/messages/MESSAGE_NAME',
    
        ).execute()
    
        # Print Chat API's response in your command line interface.
        print(result)
    
    if __name__ == '__main__':
        main()
    
  3. कोड में, इनकी जगह ये डालें:

    • API_KEY: वह एपीआई पासकोड जिसे आपने Chat API के लिए सर्विस एंडपॉइंट बनाने के लिए बनाया है.
    • SPACE_NAME: स्पेस के name का आईडी. आईडी पाने के लिए, ListSpaces() तरीके का इस्तेमाल करें या स्पेस के यूआरएल से आईडी पाएं.
    • MESSAGE_NAME: मैसेज के name से मिला आईडी. Chat API की मदद से एसिंक्रोनस तरीके से मैसेज बनाने के बाद, आपको जवाब के मुख्य हिस्से से आईडी मिल सकता है. इसके अलावा, मैसेज बनाते समय असाइन किए गए कस्टम नाम से भी आईडी मिल सकता है.
  4. अपनी वर्किंग डायरेक्ट्री में, सैंपल बनाएं और उसे चलाएं:

    python3 chat_messages_get_admin_app.py

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