Google Chat API का इस्तेमाल करके मैसेज भेजना

यह गाइड बताती है कि Google Chat API की messages.create() में से कोई भी तरीका अपनाएं:

  • ऐसे मैसेज भेजें जिनमें टेक्स्ट, कार्ड, और इंटरैक्टिव विजेट हों.
  • Chat का इस्तेमाल करने वाले किसी खास व्यक्ति को निजी तौर पर मैसेज भेजें.
  • मैसेज थ्रेड शुरू करें या उसका जवाब दें.
  • मैसेज को नाम दें, ताकि आप अन्य Chat API में उसके बारे में बता सकें अनुरोध.

मैसेज का साइज़ ज़्यादा से ज़्यादा 32,000 बाइट होना चाहिए. इसमें कोई भी टेक्स्ट या कार्ड शामिल है. इससे बड़े साइज़ का मैसेज भेजने के लिए, आपका Chat ऐप्लिकेशन इसके बजाय, कई मैसेज भेजने होंगे.

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

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

messages.create() तरीके को कॉल करने के लिए, इनका इस्तेमाल करें ऐप्लिकेशन की पुष्टि करना और उपयोगकर्ता की पुष्टि करना. Chat, मैसेज भेजने वाले को अलग तरह से एट्रिब्यूट करता है यह तय किया जा सकता है कि पुष्टि करने का कौनसा तरीका इस्तेमाल किया जा रहा है.

Chat ऐप्लिकेशन के तौर पर पुष्टि करने पर, Chat ऐप्लिकेशन मैसेज भेजता है.

ऐप्लिकेशन की पुष्टि करने की सुविधा के साथ, messages.create() तरीके को कॉल करना.
पहली इमेज: ऐप्लिकेशन की पुष्टि करने पर, Chat ऐप्लिकेशन तो मैसेज दिखाई दे. यह ध्यान देने के लिए कि मैसेज भेजने वाला व्यक्ति नहीं है, Chat में उसके नाम के आगे App दिखता है.

उपयोगकर्ता के तौर पर पुष्टि करने पर, Chat ऐप्लिकेशन उपयोगकर्ता को जोड़ा गया हो. Chat ऐप्लिकेशन में Chat ऐप्लिकेशन का नाम दिखाकर मैसेज भेजें.

messages.create() तरीके को उपयोगकर्ता की पुष्टि करने की सुविधा के साथ कॉल करना.
दूसरी इमेज: उपयोगकर्ता की पुष्टि करने से, उपयोगकर्ता मैसेज भेजता है और Chat उपयोगकर्ता के नाम के बगल में, Chat ऐप्लिकेशन का नाम दिखेगा.

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

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

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

Node.js

  • कारोबार या एंटरप्राइज़ Google Workspace खाता, जिसके पास इसका ऐक्सेस है Google Chat.

Python

  • कारोबार या एंटरप्राइज़ Google Workspace खाता, जिसके पास इसका ऐक्सेस है Google Chat.

Java

  • कारोबार या एंटरप्राइज़ Google Workspace खाता, जिसके पास इसका ऐक्सेस है Google Chat.

Apps Script

  • कारोबार या एंटरप्राइज़ Google Workspace खाता, जिसके पास इसका ऐक्सेस है Google Chat.

Chat ऐप्लिकेशन से मैसेज भेजें

इस सेक्शन में मैसेज भेजने का तरीका बताया गया है. साथ ही, यह भी बताया गया है कि मैसेज कैसे भेजे जाते हैं. इनका इस्तेमाल करके, इंटरैक्टिव ऐक्सेसरी विजेट बनाए जा सकते हैं ऐप्लिकेशन की पुष्टि करना.

ऐप्लिकेशन की पुष्टि करने के साथ मैसेज भेजा गया
चौथी इमेज. कोई Chat ऐप्लिकेशन, इसकी मदद से मैसेज भेजता है टेक्स्ट, कार्ड, और ऐक्सेसरी बटन.

ऐप्लिकेशन की पुष्टि करने की सुविधा का इस्तेमाल करके messages.create() को कॉल करने के लिए, आपको यह बताना होगा कि अनुरोध में ये फ़ील्ड शामिल करें:

  • chat.bot अनुमति का दायरा.
  • Space संसाधन जिसमें तो आपको मैसेज पोस्ट करना है. Chat ऐप्लिकेशन स्पेस का सदस्य है.
  • Message संसाधन नहीं है. मैसेज का कॉन्टेंट तय करने के लिए, रिच टेक्स्ट (text), एक या ज़्यादा कार्ड इंटरफ़ेस (cardsV2), या दोनों इस्तेमाल कर सकते हैं.

इसके अलावा, यहां दी गई जानकारी भी शामिल की जा सकती है:

नीचे दिए गए कोड में, उदाहरण के तौर पर दिखाया गया है कि Chat ऐप्लिकेशन कैसे उस Chat ऐप्लिकेशन के तौर पर पोस्ट किया जा सकता है जिसमें टेक्स्ट, एक कार्ड, और मैसेज में सबसे नीचे मौजूद, क्लिक किया जा सकने वाला बटन:

Node.js

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

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

  // Initialize request argument(s)
  const request = {
    // Replace SPACE_NAME here.
    parent: 'spaces/SPACE_NAME',
    message: {
      text: '👋🌎 Hello world! I created this message by calling ' +
            'the Chat API\'s `messages.create()` method.',
      cardsV2 : [{ card: {
        header: {
          title: 'About this message',
          imageUrl: 'https://fonts.gstatic.com/s/i/short-term/release/googlesymbols/info/default/24px.svg'
        },
        sections: [{
          header: 'Contents',
          widgets: [{ textParagraph: {
              text: '🔡 <b>Text</b> which can include ' +
                    'hyperlinks 🔗, emojis 😄🎉, and @mentions 🗣️.'
            }}, { textParagraph: {
              text: '🖼️ A <b>card</b> to display visual elements' +
                    'and request information such as text 🔤, ' +
                    'dates and times 📅, and selections ☑️.'
            }}, { textParagraph: {
              text: '👉🔘 An <b>accessory widget</b> which adds ' +
                    'a button to the bottom of a message.'
            }}
          ]}, {
            header: "What's next",
            collapsible: true,
            widgets: [{ textParagraph: {
                text: "❤️ <a href='https://developers.google.com/workspace/chat/api/reference/rest/v1/spaces.messages.reactions/create'>Add a reaction</a>."
              }}, { textParagraph: {
                text: "🔄 <a href='https://developers.google.com/workspace/chat/api/reference/rest/v1/spaces.messages/patch'>Update</a> " +
                      "or  <a href='https://developers.google.com/workspace/chat/api/reference/rest/v1/spaces.messages/delete'>delete</a> " +
                      "the message."
              }
            }]
          }
        ]
      }}],
      accessoryWidgets: [{ buttonList: { buttons: [{
        text: 'View documentation',
        icon: { materialIcon: { name: 'link' }},
        onClick: { openLink: {
          url: 'https://developers.google.com/workspace/chat/create-messages'
        }}
      }]}}]
    }
  };

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

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

main().catch(console.error);

Python

chat/client-libraries/cloud/create_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 create message with app credential
def create_message_with_app_cred():
    # Create a client
    client = create_client_with_app_credentials()

    # Initialize request argument(s)
    request = google_chat.CreateMessageRequest(
        # Replace SPACE_NAME here.
        parent = "spaces/SPACE_NAME",
        message = {
            "text": '👋🌎 Hello world! I created this message by calling ' +
                    'the Chat API\'s `messages.create()` method.',
            "cards_v2" : [{ "card": {
                "header": {
                    "title": 'About this message',
                    "image_url": 'https://fonts.gstatic.com/s/i/short-term/release/googlesymbols/info/default/24px.svg'
                },
                "sections": [{
                    "header": "Contents",
                    "widgets": [{ "text_paragraph": {
                            "text": '🔡 <b>Text</b> which can include ' +
                                    'hyperlinks 🔗, emojis 😄🎉, and @mentions 🗣️.'
                        }}, { "text_paragraph": {
                            "text": '🖼️ A <b>card</b> to display visual elements' +
                                    'and request information such as text 🔤, ' +
                                    'dates and times 📅, and selections ☑️.'
                        }}, { "text_paragraph": {
                            "text": '👉🔘 An <b>accessory widget</b> which adds ' +
                                    'a button to the bottom of a message.'
                        }}
                    ]}, {
                        "header": "What's next",
                        "collapsible": True,
                        "widgets": [{ "text_paragraph": {
                                "text": "❤️ <a href='https://developers.google.com/workspace/chat/api/reference/rest/v1/spaces.messages.reactions/create'>Add a reaction</a>."
                            }}, { "text_paragraph": {
                                "text": "🔄 <a href='https://developers.google.com/workspace/chat/api/reference/rest/v1/spaces.messages/patch'>Update</a> " +
                                        "or  <a href='https://developers.google.com/workspace/chat/api/reference/rest/v1/spaces.messages/delete'>delete</a> " +
                                        "the message."
                            }
                        }]
                    }
                ]
            }}],
            "accessory_widgets": [{ "button_list": { "buttons": [{
                "text": 'View documentation',
                "icon": { "material_icon": { "name": 'link' }},
                "on_click": { "open_link": {
                    "url": 'https://developers.google.com/workspace/chat/create-messages'
                }}
            }]}}]
        }
    )

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

    # Handle the response
    print(response)

create_message_with_app_cred()

Java

chat/client-libraries/cloud/src/main/java/com/google/workspace/api/chat/samples/CreateMessageAppCred.java
import com.google.apps.card.v1.Button;
import com.google.apps.card.v1.ButtonList;
import com.google.apps.card.v1.Card;
import com.google.apps.card.v1.Icon;
import com.google.apps.card.v1.MaterialIcon;
import com.google.apps.card.v1.OnClick;
import com.google.apps.card.v1.OpenLink;
import com.google.apps.card.v1.TextParagraph;
import com.google.apps.card.v1.Widget;
import com.google.apps.card.v1.Card.CardHeader;
import com.google.apps.card.v1.Card.Section;
import com.google.chat.v1.AccessoryWidget;
import com.google.chat.v1.CardWithId;
import com.google.chat.v1.ChatServiceClient;
import com.google.chat.v1.CreateMessageRequest;
import com.google.chat.v1.Message;

// This sample shows how to create message with app credential.
public class CreateMessageAppCred {

  public static void main(String[] args) throws Exception {
    try (ChatServiceClient chatServiceClient =
        AuthenticationUtils.createClientWithAppCredentials()) {
      CreateMessageRequest.Builder request = CreateMessageRequest.newBuilder()
        // Replace SPACE_NAME here.
        .setParent("spaces/SPACE_NAME")
        .setMessage(Message.newBuilder()
          .setText( "👋🌎 Hello world! I created this message by calling " +
                    "the Chat API\'s `messages.create()` method.")
          .addCardsV2(CardWithId.newBuilder().setCard(Card.newBuilder()
            .setHeader(CardHeader.newBuilder()
              .setTitle("About this message")
              .setImageUrl("https://fonts.gstatic.com/s/i/short-term/release/googlesymbols/info/default/24px.svg"))
            .addSections(Section.newBuilder()
              .setHeader("Contents")
              .addWidgets(Widget.newBuilder().setTextParagraph(TextParagraph.newBuilder().setText(
                "🔡 <b>Text</b> which can include " +
                "hyperlinks 🔗, emojis 😄🎉, and @mentions 🗣️.")))
              .addWidgets(Widget.newBuilder().setTextParagraph(TextParagraph.newBuilder().setText(
                "🖼️ A <b>card</b> to display visual elements " +
                "and request information such as text 🔤, " +
                "dates and times 📅, and selections ☑️.")))
              .addWidgets(Widget.newBuilder().setTextParagraph(TextParagraph.newBuilder().setText(
                "👉🔘 An <b>accessory widget</b> which adds " +
                "a button to the bottom of a message."))))
            .addSections(Section.newBuilder()
              .setHeader("What's next")
              .setCollapsible(true)
              .addWidgets(Widget.newBuilder().setTextParagraph(TextParagraph.newBuilder().setText(
                "❤️ <a href='https://developers.google.com/workspace/chat/api/reference/rest/v1/spaces.messages.reactions/create'>Add a reaction</a>.")))
              .addWidgets(Widget.newBuilder().setTextParagraph(TextParagraph.newBuilder().setText(
                "🔄 <a href='https://developers.google.com/workspace/chat/api/reference/rest/v1/spaces.messages/patch'>Update</a> " +
                "or  <a href='https://developers.google.com/workspace/chat/api/reference/rest/v1/spaces.messages/delete'>delete</a> " +
                "the message."))))))
          .addAccessoryWidgets(AccessoryWidget.newBuilder()
            .setButtonList(ButtonList.newBuilder()
              .addButtons(Button.newBuilder()
                .setText("View documentation")
                .setIcon(Icon.newBuilder()
                  .setMaterialIcon(MaterialIcon.newBuilder().setName("link")))
                .setOnClick(OnClick.newBuilder()
                  .setOpenLink(OpenLink.newBuilder()
                    .setUrl("https://developers.google.com/workspace/chat/create-messages")))))));
      Message response = chatServiceClient.createMessage(request.build());

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

Apps Script

chat/advanced-service/Main.gs
/**
 * This sample shows how to create message with app credential
 * 
 * It relies on the OAuth2 scope 'https://www.googleapis.com/auth/chat.bot'
 * used by service accounts.
 */
function createMessageAppCred() {
  // Initialize request argument(s)
  // TODO(developer): Replace SPACE_NAME here.
  const parent = 'spaces/SPACE_NAME';
  const message = {
    text: '👋🌎 Hello world! I created this message by calling ' +
          'the Chat API\'s `messages.create()` method.',
    cardsV2 : [{ card: {
      header: {
        title: 'About this message',
        imageUrl: 'https://fonts.gstatic.com/s/i/short-term/release/googlesymbols/info/default/24px.svg'
      },
      sections: [{
        header: 'Contents',
        widgets: [{ textParagraph: {
            text: '🔡 <b>Text</b> which can include ' +
                  'hyperlinks 🔗, emojis 😄🎉, and @mentions 🗣️.'
          }}, { textParagraph: {
            text: '🖼️ A <b>card</b> to display visual elements' +
                  'and request information such as text 🔤, ' +
                  'dates and times 📅, and selections ☑️.'
          }}, { textParagraph: {
            text: '👉🔘 An <b>accessory widget</b> which adds ' +
                  'a button to the bottom of a message.'
          }}
        ]}, {
          header: "What's next",
          collapsible: true,
          widgets: [{ textParagraph: {
              text: "❤️ <a href='https://developers.google.com/workspace/chat/api/reference/rest/v1/spaces.messages.reactions/create'>Add a reaction</a>."
            }}, { textParagraph: {
              text: "🔄 <a href='https://developers.google.com/workspace/chat/api/reference/rest/v1/spaces.messages/patch'>Update</a> " +
                    "or  <a href='https://developers.google.com/workspace/chat/api/reference/rest/v1/spaces.messages/delete'>delete</a> " +
                    "the message."
            }
          }]
        }
      ]
    }}],
    accessoryWidgets: [{ buttonList: { buttons: [{
      text: 'View documentation',
      icon: { materialIcon: { name: 'link' }},
      onClick: { openLink: {
        url: 'https://developers.google.com/workspace/chat/create-messages'
      }}
    }]}}]
  };
  const parameters = {};

  // Make the request
  const response = Chat.Spaces.Messages.create(
    message, parent, parameters, getHeaderWithAppCredentials()
  );

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

इस सैंपल को चलाने के लिए, SPACE_NAME को इस आईडी से बदलें स्पेस का name फ़ील्ड. आईडी पाने के लिए, spaces.list() तरीका या स्पेस के यूआरएल से.

मैसेज के निचले हिस्से में इंटरैक्टिव विजेट जोड़ें

इस गाइड के पहले कोड सैंपल में, Chat ऐप्लिकेशन के मैसेज में, क्लिक किया जा सकने वाला बटन मैसेज के निचले हिस्से में रखा हो, जिसे ऐक्सेसरी विजेट कहा जाता है. ऐक्सेसरी विजेट दिखाई देता है. अनुरोध करने के लिए आप इन विजेट का इस्तेमाल कर सकते हैं उपयोगकर्ता आपके मैसेज के साथ कई तरीकों से इंटरैक्ट कर सकते हैं. इनमें ये तरीके शामिल हैं:

  • बताएं कि कोई मैसेज कितना सटीक या सही है.
  • मैसेज या Chat ऐप्लिकेशन से जुड़ी समस्या की शिकायत करें.
  • मिलते-जुलते कॉन्टेंट, जैसे कि दस्तावेज़ का लिंक खोलें.
  • Chat ऐप्लिकेशन से मिलते-जुलते मैसेज को खारिज करना या स्नूज़ करना समय कम या ज़्यादा करते हैं.

ऐक्सेसरी विजेट जोड़ने के लिए, accessoryWidgets[] फ़ील्ड में अपने अनुरोध के मुख्य भाग में दर्ज करें और अपने मनचाहे एक या ज़्यादा विजेट दर्ज करें शामिल करने के लिए.

नीचे दी गई इमेज में, जोड़ा जा रहा Chat ऐप्लिकेशन दिखाया गया है ऐक्सेसरी विजेट के साथ मैसेज, ताकि उपयोगकर्ता अपने अनुभव को रेटिंग दे सकें के साथ चैट ऐप्लिकेशन में साइन इन करें.

ऐक्सेसरी विजेट.
पांचवी इमेज: Chat ऐप्लिकेशन का एक मैसेज, जिसमें टेक्स्ट और ऐक्सेसरी विजेट.

नीचे अनुरोध का मुख्य भाग दिखाया गया है, जो नीचे दिए गए मैसेज के साथ मैसेज बनाता है दो ऐक्सेसरी बटन. जब कोई उपयोगकर्ता किसी बटन पर क्लिक करता है, तो फ़ंक्शन (जैसे कि doUpvote) इंटरैक्शन प्रोसेस करता है:

{
  text: "Rate your experience with this Chat app.",
  accessoryWidgets: [{ buttonList: { buttons: [{
    icon: { material_icon: {
      name: "thumb_up"
    }},
    color: { red: 0, blue: 255, green: 0 },
    onClick: { action: {
      function: "doUpvote"
    }}
  }, {
    icon: { material_icon: {
      name: "thumb_down"
    }},
    color: { red: 0, blue: 255, green: 0 },
    onClick: { action: {
      function: "doDownvote"
    }}
  }]}}]
}

निजी तौर पर मैसेज भेजें

चैट ऐप्लिकेशन निजी तौर पर मैसेज भेज सकते हैं. मैसेज, स्पेस में सिर्फ़ किसी खास उपयोगकर्ता को दिखता हो. जब Chat ऐप्लिकेशन एक निजी मैसेज भेजता है, एक लेबल दिखाता है, जिससे उपयोगकर्ता को यह सूचना मिलती है कि मैसेज सिर्फ़ उन्हें दिखेगा.

Chat API का इस्तेमाल करके निजी तौर पर मैसेज भेजने के लिए, privateMessageViewer फ़ील्ड में इस फ़ील्ड का इस्तेमाल करें. उपयोगकर्ता की जानकारी देने के लिए, इसका मान User संसाधन जो Chat उपयोगकर्ता का प्रतिनिधित्व करता है. Google आपके यूआरएल पैरामीटर को कैसे इस्तेमाल करेगा, यह तय करने के लिए name फ़ील्ड User संसाधन, जैसा कि इस उदाहरण में दिखाया गया है:

{
  text: "Hello private world!",
  privateMessageViewer: {
    name: "users/USER_ID"
  }
}

इस सैंपल का इस्तेमाल करने के लिए, USER_ID बदलें से अलग है, जैसे कि 12345678987654321 या [email protected]. उपयोगकर्ताओं की जानकारी देने के बारे में ज़्यादा जानने के लिए, देखें Google Chat इस्तेमाल करने वाले लोगों की पहचान करें और उनके बारे में बताएं.

किसी मैसेज को निजी तौर पर भेजने के लिए, आपको अपने अनुरोध में इन चीज़ों को शामिल नहीं करना होगा:

किसी उपयोगकर्ता की ओर से मैसेज भेजें

इस सेक्शन में बताया गया है कि इसका इस्तेमाल करने वाले उपयोगकर्ता की ओर से मैसेज कैसे भेजें उपयोगकर्ता की पुष्टि करना. उपयोगकर्ता की पुष्टि करने की सुविधा से, मैसेज के कॉन्टेंट में सिर्फ़ टेक्स्ट शामिल किया जा सकता है साथ ही, इसमें उन मैसेज सेवा की सुविधाओं को शामिल नहीं किया जाना चाहिए जो सिर्फ़ चैट ऐप्लिकेशन, जिनमें कार्ड इंटरफ़ेस और इंटरैक्टिव विजेट शामिल हैं.

उपयोगकर्ता की पुष्टि करने के साथ मैसेज भेजा गया
तीसरी इमेज. Chat ऐप्लिकेशन इस पर मैसेज भेजता है पर ऐक्सेस किया जा सकता है.

उपयोगकर्ता की पुष्टि करने की सुविधा का इस्तेमाल करके messages.create() को कॉल करने के लिए, आपको अनुरोध में ये फ़ील्ड शामिल करें:

  • अनुमति का दायरा जो इस तरीके के लिए उपयोगकर्ता की पुष्टि करने की सुविधा देता है. नीचे दिया गया सैंपल, chat.messages.create स्कोप.
  • Space संसाधन जिसमें तो आपको मैसेज पोस्ट करना है. प्रमाणित उपयोगकर्ता स्पेस.
  • Message संसाधन नहीं है. मैसेज का कॉन्टेंट तय करने के लिए, आपको text फ़ील्ड.

इसके अलावा, यहां दी गई जानकारी भी शामिल की जा सकती है:

  • messageId फ़ील्ड की मदद से, मैसेज का नाम डालें, ताकि उसे अन्य एपीआई अनुरोधों में इस्तेमाल किया जा सके.
  • thread.threadKey और messageReplyOption फ़ील्ड थ्रेड शुरू करें या उसका जवाब दें. अगर स्पेस नहीं है, तो थ्रेडिंग का इस्तेमाल करते हैं, तो इस फ़ील्ड को अनदेखा कर दिया जाता है.

नीचे दिए गए कोड में, उदाहरण के तौर पर दिखाया गया है कि Chat ऐप्लिकेशन कैसे किसी पुष्टि किए गए उपयोगकर्ता की ओर से, दिए गए स्पेस में मैसेज भेज सकता है:

Node.js

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

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

// This sample shows how to create 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 here.
    parent: 'spaces/SPACE_NAME',
    message: {
      text: '👋🌎 Hello world!' +
            'Text messages can contain things like:\n\n' +
            '* Hyperlinks 🔗\n' +
            '* Emojis 😄🎉\n' +
            '* Mentions of other Chat users `@` \n\n' +
            'For details, see the ' +
            '<https://developers.google.com/workspace/chat/format-messages' +
            '|Chat API developer documentation>.'
    }
  };

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

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

main().catch(console.error);

Python

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

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

def create_message_with_user_cred():
    # Create a client
    client = create_client_with_user_credentials(SCOPES)

    # Initialize request argument(s)
    request = google_chat.CreateMessageRequest(
        # Replace SPACE_NAME here.
        parent = "spaces/SPACE_NAME",
        message = {
            "text": '👋🌎 Hello world!' +
                    'Text messages can contain things like:\n\n' +
                    '* Hyperlinks 🔗\n' +
                    '* Emojis 😄🎉\n' +
                    '* Mentions of other Chat users `@` \n\n' +
                    'For details, see the ' +
                    '<https://developers.google.com/workspace/chat/format-messages' +
                    '|Chat API developer documentation>.'
        }
    )

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

    # Handle the response
    print(response)

create_message_with_user_cred()

Java

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

// This sample shows how to create message with user credential.
public class CreateMessageUserCred {

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

  public static void main(String[] args) throws Exception {
    try (ChatServiceClient chatServiceClient =
        AuthenticationUtils.createClientWithUserCredentials(
          ImmutableList.of(SCOPE))) {
      CreateMessageRequest.Builder request = CreateMessageRequest.newBuilder()
        // Replace SPACE_NAME here.
        .setParent("spaces/SPACE_NAME")
        .setMessage(Message.newBuilder()
          .setText( "👋🌎 Hello world!" +
                    "Text messages can contain things like:\n\n" +
                    "* Hyperlinks 🔗\n" +
                    "* Emojis 😄🎉\n" +
                    "* Mentions of other Chat users `@` \n\n" +
                    "For details, see the " +
                    "<https://developers.google.com/workspace/chat/format-messages" +
                    "|Chat API developer documentation>."));
      Message response = chatServiceClient.createMessage(request.build());

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

Apps Script

chat/advanced-service/Main.gs
/**
 * This sample shows how to create message with user credential
 * 
 * It relies on the OAuth2 scope 'https://www.googleapis.com/auth/chat.messages.create'
 * referenced in the manifest file (appsscript.json).
 */
function createMessageUserCred() {
  // Initialize request argument(s)
  // TODO(developer): Replace SPACE_NAME here.
  const parent = 'spaces/SPACE_NAME';
  const message = {
    text: '👋🌎 Hello world!' +
          'Text messages can contain things like:\n\n' +
          '* Hyperlinks 🔗\n' +
          '* Emojis 😄🎉\n' +
          '* Mentions of other Chat users `@` \n\n' +
          'For details, see the ' +
          '<https://developers.google.com/workspace/chat/format-messages' +
          '|Chat API developer documentation>.'
  };

  // Make the request
  const response = Chat.Spaces.Messages.create(message, parent);

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

इस सैंपल को चलाने के लिए, SPACE_NAME को इस आईडी से बदलें स्पेस का name फ़ील्ड. आईडी पाने के लिए, spaces.list() तरीका या स्पेस के यूआरएल से.

थ्रेड शुरू करें या उसका जवाब दें

थ्रेड का इस्तेमाल करने वाले स्पेस के लिए, आप यह तय कर सकते हैं कि नया मैसेज, थ्रेड शुरू करेगा या उस पर जवाब देगा पहले से मौजूद थ्रेड.

Chat API का इस्तेमाल करके जो मैसेज बनाए जाते हैं वे डिफ़ॉल्ट रूप से थ्रेड. थ्रेड की पहचान करने और बाद में उसका जवाब देने के लिए, आपके अनुरोध में थ्रेड कुंजी:

  • अपने अनुरोध के मुख्य हिस्से में यह जानकारी दें: thread.threadKey फ़ील्ड.
  • क्वेरी पैरामीटर तय करें messageReplyOption ताकि यह पता लगाया जा सके कि कुंजी पहले से मौजूद होने पर क्या होता है.

मौजूदा थ्रेड पर जवाब देने वाला मैसेज बनाने के लिए:

  • अपने अनुरोध के मुख्य हिस्से में thread फ़ील्ड को शामिल करें. अगर यह नीति सेट है, तो यह तय करें threadKey जिन्हें आपने बनाया है. अगर ऐसा नहीं है, तो आपको name शामिल किया है.
  • क्वेरी पैरामीटर messageReplyOption तय करें.

नीचे दिए गए कोड में, उदाहरण के तौर पर दिखाया गया है कि Chat ऐप्लिकेशन कैसे के ज़रिए पहचाने गए थ्रेड को शुरू करने या जवाब देने पर किसी प्रमाणित उपयोगकर्ता की ओर से दिए गए स्पेस की कुंजी:

Node.js

chat/client-libraries/cloud/create-message-user-cred-thread-key.js
import {createClientWithUserCredentials} from './authentication-utils.js';
const {MessageReplyOption} = require('@google-apps/chat').protos.google.chat.v1.CreateMessageRequest;

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

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

  // Initialize request argument(s)
  const request = {
    // Replace SPACE_NAME here.
    parent: 'spaces/SPACE_NAME',
    // Creates the message as a reply to the thread specified by thread_key
    // If it fails, the message starts a new thread instead
    messageReplyOption: MessageReplyOption.REPLY_MESSAGE_FALLBACK_TO_NEW_THREAD,
    message: {
      text: 'Hello with user credential!',
      thread: {
        // Thread key specifies a thread and is unique to the chat app
        // that sets it
        threadKey: 'THREAD_KEY'
      }
    }
  };

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

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

main().catch(console.error);

Python

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

import google.apps.chat_v1.CreateMessageRequest.MessageReplyOption

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

# This sample shows how to create message with user credential with thread key
def create_message_with_user_cred_thread_key():
    # Create a client
    client = create_client_with_user_credentials(SCOPES)

    # Initialize request argument(s)
    request = google_chat.CreateMessageRequest(
        # Replace SPACE_NAME here
        parent = "spaces/SPACE_NAME",
        # Creates the message as a reply to the thread specified by thread_key.
        # If it fails, the message starts a new thread instead.
        message_reply_option = MessageReplyOption.REPLY_MESSAGE_FALLBACK_TO_NEW_THREAD,
        message = {
            "text": "Hello with user credential!",
            "thread": {
                # Thread key specifies a thread and is unique to the chat app
                # that sets it.
                "thread_key": "THREAD_KEY"
            }
        }
    )

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

    # Handle the response
    print(response)

create_message_with_user_cred_thread_key()

Java

chat/client-libraries/cloud/src/main/java/com/google/workspace/api/chat/samples/CreateMessageUserCredThreadKey.java
import com.google.chat.v1.ChatServiceClient;
import com.google.chat.v1.CreateMessageRequest;
import com.google.chat.v1.CreateMessageRequest.MessageReplyOption;
import com.google.chat.v1.Message;
import com.google.chat.v1.Thread;

// This sample shows how to create message with a thread key with user
// credential.
public class CreateMessageUserCredThreadKey {

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

  public static void main(String[] args) throws Exception {
    try (ChatServiceClient chatServiceClient =
        AuthenticationUtils.createClientWithUserCredentials(
          ImmutableList.of(SCOPE))) {
      CreateMessageRequest.Builder request = CreateMessageRequest.newBuilder()
        // Replace SPACE_NAME here.
        .setParent("spaces/SPACE_NAME")
        // Creates the message as a reply to the thread specified by thread_key.
        // If it fails, the message starts a new thread instead.
        .setMessageReplyOption(
          MessageReplyOption.REPLY_MESSAGE_FALLBACK_TO_NEW_THREAD)
        .setMessage(Message.newBuilder()
          .setText("Hello with user credentials!")
          // Thread key specifies a thread and is unique to the chat app
          // that sets it.
          .setThread(Thread.newBuilder().setThreadKey("THREAD_KEY")));
      Message response = chatServiceClient.createMessage(request.build());

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

Apps Script

chat/advanced-service/Main.gs
/**
 * This sample shows how to create message with user credential with thread key
 * 
 * It relies on the OAuth2 scope 'https://www.googleapis.com/auth/chat.messages.create'
 * referenced in the manifest file (appsscript.json).
 */
function createMessageUserCredThreadKey() {
  // Initialize request argument(s)
  // TODO(developer): Replace SPACE_NAME here.
  const parent = 'spaces/SPACE_NAME';
  // Creates the message as a reply to the thread specified by thread_key
  // If it fails, the message starts a new thread instead
  const messageReplyOption = 'REPLY_MESSAGE_FALLBACK_TO_NEW_THREAD';
  const message = {
    text: 'Hello with user credential!',
    thread: {
      // Thread key specifies a thread and is unique to the chat app
      // that sets it
      threadKey: 'THREAD_KEY'
    }
  };

  // Make the request
  const response = Chat.Spaces.Messages.create(message, parent, {
    messageReplyOption: messageReplyOption
  });

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

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

  • THREAD_KEY: स्पेस में मौजूदा थ्रेड कुंजी या का इस्तेमाल करें.
  • SPACE_NAME: स्पेस के आईडी name फ़ील्ड. आईडी पाने के लिए, spaces.list() तरीका या स्पेस के यूआरएल से.

मैसेज को नाम देना

आगे के एपीआई कॉल में मैसेज को वापस पाने या उसके बारे में बताने के लिए, मैसेज को नाम दिया जा सकता है अपने messages.create() अनुरोध में messageId फ़ील्ड को सेट करके. अपने संदेश को नाम देने पर आपको मैसेज के संसाधन के नाम से, सिस्टम से असाइन किया गया आईडी (जिसको name फ़ील्ड में डालें).

उदाहरण के लिए, get() तरीके का इस्तेमाल करके मैसेज वापस पाने के लिए, किस मैसेज को वापस लाना है, यह तय करने के लिए रिसॉर्स का नाम. संसाधन का नाम है spaces/{space}/messages/{message} के तौर पर फ़ॉर्मैट किया गया है, जहां {message} से पता चलता है कि आपके द्वारा दिखाई देगा.

किसी मैसेज को नाम देने के लिए, कस्टम आईडी डालें messageId फ़ील्ड में फिर से शामिल करें. messageId फ़ील्ड clientAssignedMessageId Message संसाधन का फ़ील्ड.

मैसेज बनाने के बाद ही उसे नाम दिया जा सकता है. आप नाम नहीं रख सकते या मौजूदा मैसेज के कस्टम आईडी में बदलाव कर सकते हैं. कस्टम आईडी इन शर्तों के मुताबिक होना चाहिए ज़रूरतें:

  • client- से शुरू होता है. उदाहरण के लिए, client-custom-name एक मान्य कस्टम वैल्यू है आईडी है, लेकिन custom-name नहीं है.
  • इसमें 63 से ज़्यादा वर्ण नहीं होने चाहिए और सिर्फ़ अंग्रेज़ी के छोटे अक्षर, अंक, और हाइफ़न.
  • हर स्पेस का यूनीक स्पेस होता है. Chat ऐप्लिकेशन, अलग-अलग मैसेज के लिए एक ही कस्टम आईडी.

नीचे दिए गए कोड में, उदाहरण के तौर पर दिखाया गया है कि Chat ऐप्लिकेशन कैसे किसी स्पेस में, आईडी के साथ टेक्स्ट मैसेज भेज सकता है पुष्टि किया गया उपयोगकर्ता:

Node.js

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

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

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

  // Initialize request argument(s)
  const request = {
    // Replace SPACE_NAME here.
    parent: 'spaces/SPACE_NAME',
    // Message id lets chat apps get, update or delete a message without needing
    // to store the system assigned ID in the message's resource name
    messageId: 'client-MESSAGE-ID',
    message: { text: 'Hello with user credential!' }
  };

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

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

main().catch(console.error);

Python

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

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

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

    # Initialize request argument(s)
    request = google_chat.CreateMessageRequest(
        # Replace SPACE_NAME here
        parent = "spaces/SPACE_NAME",
        # Message id let chat apps get, update or delete a message without needing
        # to store the system assigned ID in the message's resource name.
        message_id = "client-MESSAGE-ID",
        message = {
            "text": "Hello with user credential!"
        }
    )

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

    # Handle the response
    print(response)

create_message_with_user_cred_message_id()

Java

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

// This sample shows how to create message with message id specified with user
// credential.
public class CreateMessageUserCredMessageId {

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

  public static void main(String[] args) throws Exception {
    try (ChatServiceClient chatServiceClient =
        AuthenticationUtils.createClientWithUserCredentials(
          ImmutableList.of(SCOPE))) {
      CreateMessageRequest.Builder request = CreateMessageRequest.newBuilder()
        // Replace SPACE_NAME here.
        .setParent("spaces/SPACE_NAME")
        .setMessage(Message.newBuilder()
          .setText("Hello with user credentials!"))
        // Message ID lets chat apps get, update or delete a message without
        // needing to store the system assigned ID in the message's resource
        // name.
        .setMessageId("client-MESSAGE-ID");
      Message response = chatServiceClient.createMessage(request.build());

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

Apps Script

chat/advanced-service/Main.gs
/**
 * This sample shows how to create message with user credential with message id
 * 
 * It relies on the OAuth2 scope 'https://www.googleapis.com/auth/chat.messages.create'
 * referenced in the manifest file (appsscript.json).
 */
function createMessageUserCredMessageId() {
  // Initialize request argument(s)
  // TODO(developer): Replace SPACE_NAME here.
  const parent = 'spaces/SPACE_NAME';
  // Message id lets chat apps get, update or delete a message without needing
  // to store the system assigned ID in the message's resource name
  const messageId = 'client-MESSAGE-ID';
  const message = { text: 'Hello with user credential!' };

  // Make the request
  const response = Chat.Spaces.Messages.create(message, parent, {
    messageId: messageId
  });

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

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

  • SPACE_NAME: स्पेस के आईडी name फ़ील्ड. आईडी पाने के लिए, spaces.list() तरीका या स्पेस के यूआरएल से.
  • MESSAGE-ID: शुरू होने वाले मैसेज के लिए कोई नाम custom- के साथ. यह ईमेल पता, चुनी गई जगह में चैट ऐप्लिकेशन.

समस्या हल करें

जब Google Chat ऐप्लिकेशन या card कोई गड़बड़ी दिखाता है, Chat के इंटरफ़ेस पर, "कोई गड़बड़ी हुई" मैसेज दिखता है. या "आपका अनुरोध प्रोसेस नहीं किया जा सका." कभी-कभी Chat यूज़र इंटरफ़ेस (यूआई) गड़बड़ी का कोई मैसेज नहीं दिखाता है. हालांकि, Chat ऐप्लिकेशन या कार्ड से कोई अनचाहा नतीजा मिलता है; उदाहरण के लिए, कार्ड मैसेज शायद दिखाना चाहते हैं.

हालांकि, Chat के यूज़र इंटरफ़ेस (यूआई) में गड़बड़ी का मैसेज शायद न दिखे, गड़बड़ी ठीक करने में आपकी मदद करने के लिए, जानकारी देने वाले गड़बड़ी के मैसेज और लॉग डेटा उपलब्ध है जब चैट ऐप्लिकेशन के लिए गड़बड़ी लॉग करने की सेटिंग चालू हो. मदद के लिए, डीबग करने और गड़बड़ियां ठीक करने के लिए, देखें Google Chat से जुड़ी गड़बड़ियां हल करना और गड़बड़ियां ठीक करना.