लिखने के लिए कार्रवाइयों वाले यूज़र इंटरफ़ेस (यूआई) को बढ़ाना

जब कोई उपयोगकर्ता Gmail पढ़ रहा हो, तब उसे कार्ड-आधारित इंटरफ़ेस उपलब्ध कराने के अलावा मैसेज, Google Workspace के ऐड-ऑन, जो Gmail का इस्तेमाल करते हैं जब उपयोगकर्ता नया मैसेज लिख रहा हो, तब उसे दूसरा इंटरफ़ेस मिल सकता है या तो मौजूदा सवालों के जवाब दें. इससे Google Workspace ऐड-ऑन, उपयोगकर्ता के लिए ईमेल लिखने का काम अपने-आप कर पाता है.

ऐड-ऑन लिखने के यूज़र इंटरफ़ेस (यूआई) को ऐक्सेस करना

ऐड-ऑन के लिखने के यूज़र इंटरफ़ेस (यूआई) को देखने के दो तरीके हैं. पहला तरीक़ा यह है कि ऐड-ऑन के चालू होने पर, नया ड्राफ़्ट बनाएँ या जवाब दें. दूसरा तरीका है, ड्राफ़्ट बनाते समय ऐड-ऑन शुरू करना.

दोनों में से किसी भी स्थिति की वजह से ऐड-ऑन, संबंधित गड़बड़ी को लागू करता है ऐड-ऑन में तय किया गया ट्रिगर फ़ंक्शन बनाएं मेनिफ़ेस्ट. लिखें ट्रिगर फ़ंक्शन उस लिखें के लिए लिखें यूज़र इंटरफ़ेस (यूआई) बनाता है कार्रवाई होती है, जिसे Gmail उपयोगकर्ता को दिखाता है.

कंपोज़ ऐड-ऑन बनाना

आप इन सामान्य चरणों का पालन करके किसी ऐड-ऑन में लिखने की सुविधा जोड़ सकते हैं:

  1. gmail.composeTrigger जोड़ें ऐड-ऑन स्क्रिप्ट प्रोजेक्ट के लिए फ़ील्ड मेनिफ़ेस्ट और मेनिफ़ेस्ट को अपडेट करें शामिल करने के लिए दायरा जो कंपोज़िशन कार्रवाइयों के लिए ज़रूरी हैं.
  2. एक ऐसा कंपोज़ ट्रिगर फ़ंक्शन लागू करें जो एक लिखने वाला यूज़र इंटरफ़ेस (यूआई) बनाता है जब ट्रिगर नहीं होता है. 'लिखें' ट्रिगर फ़ंक्शन, नतीजे के तौर पर Card ऑब्जेक्ट या इसका कलेक्शन Card ऐसे ऑब्जेक्ट जिनमें शामिल हो लिखें यूज़र इंटरफ़ेस (यूआई) की इमेज.
  3. उपयोगकर्ता की क्वेरी पर प्रतिक्रिया देने के लिए, संबंधित कॉलबैक फ़ंक्शन लागू करें यूज़र इंटरफ़ेस (यूआई) इंटरैक्शन लिखें. ये फ़ंक्शन खुद लिखने की कार्रवाई नहीं हैं (जिसकी वजह से सिर्फ़ 'लिखें' यूज़र इंटरफ़ेस (यूआई) दिखता है; बल्कि, ये हैं वे अलग-अलग फ़ंक्शन जो यह कंट्रोल करते हैं कि लिखने के लिए यूज़र इंटरफ़ेस (यूआई) चुना गया. उदाहरण के लिए, एक बटन वाला यूआई कार्ड आम तौर पर, इसमें एक कॉलबैक फ़ंक्शन होता है, जो उपयोगकर्ता के क्लिक करने पर काम करता है वह बटन. उन विजेट के लिए कॉलबैक फ़ंक्शन जो ड्राफ़्ट मैसेज को अपडेट करते हैं सामग्री को UpdateDraftActionResponse ऑब्जेक्ट है.

ट्रिगर फ़ंक्शन लिखें

ऐड-ऑन का लिखने के यूज़र इंटरफ़ेस (यूआई) को ऐड-ऑन के मैसेज की तरह ही बनाया जाता है यूज़र इंटरफ़ेस (यूआई)—ऐसा करने के लिए, Apps Script कार्ड सेवा का इस्तेमाल किया जा रहा है कार्ड जोड़ें और उनमें विजेट.

आपको gmail.composeTrigger.selectActions[].runFunction जिसे आपने अपने मेनिफ़ेस्ट में तय किया है. लिखें ट्रिगर फ़ंक्शन वापस आना चाहिए एक Card ऑब्जेक्ट या Card ऑब्जेक्ट का कलेक्शन शामिल हैं, जिनमें उस कार्रवाई के लिए लिखें यूज़र इंटरफ़ेस (यूआई) शामिल है. ये फ़ंक्शन बहुत मिलते-जुलते हैं से संदर्भ के हिसाब से ट्रिगर फ़ंक्शन में और इसी तरह से कार्ड बनाए जाने चाहिए.

ट्रिगर इवेंट ऑब्जेक्ट लिखें

जब कोई कंपोज़ कार्रवाई चुनी जाती है, तब वह उससे जुड़े कंपोज़ ट्रिगर को एक्ज़ीक्यूट करती है फ़ंक्शन करता है और फ़ंक्शन को इवेंट ऑब्जेक्ट पास करता है पैरामीटर के तौर पर. इवेंट ऑब्जेक्ट में, ऐड-ऑन के कॉन्टेक्स्ट के बारे में जानकारी मौजूद हो सकती है और ट्रिगर फ़ंक्शन के लिए बनाया जा रहा ड्राफ़्ट.

इवेंट ऑब्जेक्ट का स्ट्रक्चर देखें का इस्तेमाल करें. जानकारी इवेंट ऑब्जेक्ट में शामिल, कुछ हद तक gmail.composeTrigger.draftAccess मेनिफ़ेस्ट फ़ील्ड:

  • अगर gmail.composeTrigger.draftAccess मेनिफ़ेस्ट फ़ील्ड NONE है या इसे शामिल नहीं किया गया है, इवेंट ऑब्जेक्ट में सिर्फ़ कम से कम जानकारी.

  • अगर gmail.composeTrigger.draftAccess METADATA पर सेट है, यह इवेंट ऑब्जेक्ट को लिखें ट्रिगर फ़ंक्शन को पास किया जाता है बनाए जा रहे ईमेल के पाने वालों की सूची से अपने-आप भर जाता है.

चालू ड्राफ़्ट में कॉन्टेंट शामिल किया जा रहा है

आम तौर पर, Google Workspace ऐड-ऑन लिखने के यूज़र इंटरफ़ेस (यूआई) से यह सुविधा मिलती है मैसेज लिखने में मदद करने के लिए, उपयोगकर्ता के विकल्पों और कंट्रोल को कंट्रोल करती हैं. इस्तेमाल के इन उदाहरणों में, जब उपयोगकर्ता यूज़र इंटरफ़ेस (यूआई) में विकल्प चुन लेता है, तो ऐड-ऑन चुने गए विकल्पों को समझ लेता है और इसके हिसाब से ईमेल के मौजूदा ड्राफ़्ट को अपडेट करता है.

मौजूदा ड्राफ़्ट ईमेल को अपडेट करना आसान बनाने के लिए, कार्ड सेवा को इन क्लास के साथ बढ़ा दिया गया है:

  • ContentType—एक enum जो तय करता है कि बदलाव किए जा सकने वाले एचटीएमएल को जोड़ना है या नहीं, न बदले जा सकने वाले एचटीएमएल (जो Gmail कॉन्टेंट में बदलाव नहीं कर सकते) या सादा टेक्स्ट.
  • UpdateDraftActionResponse—इसका प्रतिनिधित्व करता है किसी कार्रवाई का जवाब, जो मौजूदा ड्राफ़्ट ईमेल को अपडेट करता है.
  • UpdateDraftActionResponseBuilder—A इसके लिए बिल्डर UpdateDraftActionResponse ऑब्जेक्ट हैं.
  • UpdateDraftBodyAction—इसका प्रतिनिधित्व करता है ऐसी कार्रवाई जिससे मौजूदा ड्राफ़्ट ईमेल के मुख्य हिस्से को अपडेट किया जाएगा.
  • UpdateDraftBodyType—एक ईनम जो यह बताता है कि शरीर में कैसे बदलाव होता है.
  • UpdateDraftSubjectAction—इसका प्रतिनिधित्व करता है ऐसी कार्रवाई जिससे मौजूदा ड्राफ़्ट ईमेल के विषय फ़ील्ड को अपडेट कर दिया जाता है.
  • UpdateDraftToRecipientsAction—इसका प्रतिनिधित्व करता है ऐसी कार्रवाई जिससे मौजूदा ड्राफ़्ट ईमेल पाने वाले 'पाने वाला' फ़ील्ड में बदलाव हो जाएगा.
  • UpdateDraftCcRecipientsAction—इसका प्रतिनिधित्व करता है ऐसी कार्रवाई जिससे मौजूदा ड्राफ़्ट ईमेल की कॉपी पाने वाले लोगों की जानकारी अपडेट हो जाएगी.
  • UpdateDraftBccRecipientsAction—इसका प्रतिनिधित्व करता है ऐसी कार्रवाई जिससे मौजूदा ड्राफ़्ट ईमेल के गुप्त कॉपी पाने वालों की जानकारी अपडेट हो जाएगी.

आम तौर पर, ऐड-ऑन लिखने के यूज़र इंटरफ़ेस (यूआई) में 'सेव करें' शामिल होता है या 'शामिल करें' विजेट को किसी उपयोगकर्ता ने क्लिक करके यह बता सकते हैं कि उन्होंने यूज़र इंटरफ़ेस (यूआई) में विकल्प चुन लिए हैं और वे को उनके द्वारा लिखे जा रहे ईमेल में जोड़े जाने वाले विकल्प चुनने होंगे. इसे जोड़ने के लिए इंटरऐक्टिविटी, विजेट में कोई असोसिएट किया गया Action ऑब्जेक्ट जो यह विजेट, ऐड-ऑन को खास कॉलबैक फ़ंक्शन चलाने का निर्देश देता है. हालांकि, इसके लिए ज़रूरी है कि विजेट क्लिक किया गया. आपको इन कॉलबैक फ़ंक्शन को लागू करना होगा. हर कॉलबैक फ़ंक्शन को CANNOT TRANSLATE UpdateDraftActionResponse ऑब्जेक्ट देता है, जो मौजूदा ड्राफ़्ट ईमेल में किए जाने वाले बदलावों की जानकारी देता है.

उदाहरण 1

नीचे दिया गया कोड स्निपेट, लिखने के लिए ऐसा यूज़र इंटरफ़ेस (यूआई) बनाने का तरीका दिखाता है जो विषय को अपडेट करता है. साथ ही, मौजूदा ईमेल ड्राफ़्ट के पाने वाले, कॉपी, गुप्त कॉपी पाने वाले लोगों की जानकारी को भी अपडेट करने का तरीका बताता है.

    /**
     * Compose trigger function that fires when the compose UI is
     * requested. Builds and returns a compose UI for inserting images.
     *
     * @param {event} e The compose trigger event object. Not used in
     *         this example.
     * @return {Card[]}
     */
    function getComposeUI(e) {
      return [buildComposeCard()];
    }

    /**
     * Build a card to display interactive buttons to allow the user to
     * update the subject, and To, Cc, Bcc recipients.
     *
     * @return {Card}
     */
    function buildComposeCard() {

      var card = CardService.newCardBuilder();
      var cardSection = CardService.newCardSection().setHeader('Update email');
      cardSection.addWidget(
          CardService.newTextButton()
              .setText('Update subject')
              .setOnClickAction(CardService.newAction()
                  .setFunctionName('applyUpdateSubjectAction')));
      cardSection.addWidget(
          CardService.newTextButton()
              .setText('Update To recipients')
              .setOnClickAction(CardService.newAction()
                  .setFunctionName('updateToRecipients')));
      cardSection.addWidget(
          CardService.newTextButton()
              .setText('Update Cc recipients')
              .setOnClickAction(CardService.newAction()
                  .setFunctionName('updateCcRecipients')));
      cardSection.addWidget(
          CardService.newTextButton()
              .setText('Update Bcc recipients')
              .setOnClickAction(CardService.newAction()
                  .setFunctionName('updateBccRecipients')));
      return card.addSection(cardSection).build();
    }

    /**
     * Updates the subject field of the current email when the user clicks
     * on "Update subject" in the compose UI.
     *
     * Note: This is not the compose action that builds a compose UI, but
     * rather an action taken when the user interacts with the compose UI.
     *
     * @return {UpdateDraftActionResponse}
     */
    function applyUpdateSubjectAction() {
      // Get the new subject field of the email.
      // This function is not shown in this example.
      var subject = getSubject();
      var response = CardService.newUpdateDraftActionResponseBuilder()
          .setUpdateDraftSubjectAction(CardService.newUpdateDraftSubjectAction()
              .addUpdateSubject(subject))
          .build();
      return response;
    }

    /**
     * Updates the To recipients of the current email when the user clicks
     * on "Update To recipients" in the compose UI.
     *
     * Note: This is not the compose action that builds a compose UI, but
     * rather an action taken when the user interacts with the compose UI.
     *
     * @return {UpdateDraftActionResponse}
     */
    function applyUpdateToRecipientsAction() {
      // Get the new To recipients of the email.
      // This function is not shown in this example.
      var toRecipients = getToRecipients();
      var response = CardService.newUpdateDraftActionResponseBuilder()
          .setUpdateDraftToRecipientsAction(CardService.newUpdateDraftToRecipientsAction()
              .addUpdateToRecipients(toRecipients))
          .build();
      return response;
    }

    /**
     * Updates the Cc recipients  of the current email when the user clicks
     * on "Update Cc recipients" in the compose UI.
     *
     * Note: This is not the compose action that builds a compose UI, but
     * rather an action taken when the user interacts with the compose UI.
     *
     * @return {UpdateDraftActionResponse}
     */
    function applyUpdateCcRecipientsAction() {
      // Get the new Cc recipients of the email.
      // This function is not shown in this example.
      var ccRecipients = getCcRecipients();
      var response = CardService.newUpdateDraftActionResponseBuilder()
          .setUpdateDraftCcRecipientsAction(CardService.newUpdateDraftCcRecipientsAction()
              .addUpdateToRecipients(ccRecipients))
          .build();
      return response;
    }

    /**
     * Updates the Bcc recipients  of the current email when the user clicks
     * on "Update Bcc recipients" in the compose UI.
     *
     * Note: This is not the compose action that builds a compose UI, but
     * rather an action taken when the user interacts with the compose UI.
     *
     * @return {UpdateDraftActionResponse}
     */
    function applyUpdateBccRecipientsAction() {
      // Get the new Bcc recipients of the email.
      // This function is not shown in this example.
      var bccRecipients = getBccRecipients();
      var response = CardService.newUpdateDraftActionResponseBuilder()
          .setUpdateDraftBccRecipientsAction(CardService.newUpdateDraftBccRecipientsAction()
              .addUpdateToRecipients(bccRecipients))
          .build();
      return response;
    }

उदाहरण 2

नीचे दिया गया कोड स्निपेट, लिखने के लिए ऐसा यूज़र इंटरफ़ेस (यूआई) बनाने का तरीका दिखाता है जिसमें इमेज डाली जा सकती हैं को मौजूदा ड्राफ़्ट ईमेल में भेज दिया जाएगा.

    /**
     * Compose trigger function that fires when the compose UI is
     * requested. Builds and returns a compose UI for inserting images.
     *
     * @param {event} e The compose trigger event object. Not used in
     *         this example.
     * @return {Card[]}
     */
    function getInsertImageComposeUI(e) {
      return [buildImageComposeCard()];
    }

    /**
     * Build a card to display images from a third-party source.
     *
     * @return {Card}
     */
    function buildImageComposeCard() {
      // Get a short list of image URLs to display in the UI.
      // This function is not shown in this example.
      var imageUrls = getImageUrls();

      var card = CardService.newCardBuilder();
      var cardSection = CardService.newCardSection().setHeader('My Images');
      for (var i = 0; i < imageUrls.length; i++) {
        var imageUrl = imageUrls[i];
        cardSection.addWidget(
            CardService.newImage()
                .setImageUrl(imageUrl)
                .setOnClickAction(CardService.newAction()
                      .setFunctionName('applyInsertImageAction')
                      .setParameters({'url' : imageUrl})));
      }
      return card.addSection(cardSection).build();
    }

    /**
     * Adds an image to the current draft email when the image is clicked
     * in the compose UI. The image is inserted at the current cursor
     * location. If any content of the email draft is currently selected,
     * it is deleted and replaced with the image.
     *
     * Note: This is not the compose action that builds a compose UI, but
     * rather an action taken when the user interacts with the compose UI.
     *
     * @param {event} e The incoming event object.
     * @return {UpdateDraftActionResponse}
     */
    function applyInsertImageAction(e) {
      var imageUrl = e.parameters.url;
      var imageHtmlContent = '<img style=\"display: block\" src=\"'
           + imageUrl + '\"/>';
      var response = CardService.newUpdateDraftActionResponseBuilder()
          .setUpdateDraftBodyAction(CardService.newUpdateDraftBodyAction()
              .addUpdateContent(
                  imageHtmlContent,
                  CardService.ContentType.MUTABLE_HTML)
              .setUpdateType(
                  CardService.UpdateDraftBodyType.IN_PLACE_INSERT))
          .build();
      return response;
    }