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

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

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

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

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

ईमेल लिखने में मदद करने वाला ऐड-ऑन बनाना

किसी ऐड-ऑन में ईमेल लिखने की सुविधा जोड़ने के लिए, यह सामान्य तरीका अपनाएं:

  1. ऐड-ऑन के स्क्रिप्ट प्रोजेक्ट के gmail.composeTrigger फ़ील्ड में मेनिफ़ेस्ट जोड़ें. साथ ही, मेनिफ़ेस्ट के स्कोप अपडेट करें, ताकि कंपोज़ ऐक्शन के लिए ज़रूरी स्कोप शामिल किए जा सकें.
  2. एक ऐसा कंपोज़ ट्रिगर फ़ंक्शन लागू करें जो ट्रिगर चालू होने पर, कंपोज़ यूज़र इंटरफ़ेस (यूआई) बनाता है. Compose ट्रिगर फ़ंक्शन, एक 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—यह UpdateDraftActionResponse ऑब्जेक्ट बनाने वाला टूल है.
  • UpdateDraftBodyAction—यह ऐसी कार्रवाई को दिखाता है जिससे मौजूदा ड्राफ़्ट ईमेल का मुख्य हिस्सा अपडेट होता है.
  • UpdateDraftBodyType—यह एक enum है. इससे यह तय होता है कि मुख्य हिस्से में किस तरह का बदलाव किया गया है.
  • UpdateDraftSubjectAction—यह एक ऐसी कार्रवाई को दिखाता है जो मौजूदा ड्राफ़्ट ईमेल के विषय फ़ील्ड को अपडेट करती है.
  • UpdateDraftToRecipientsAction—यह एक ऐसी कार्रवाई को दिखाता है जिससे मौजूदा ड्राफ़्ट ईमेल में, 'पाने वाले' फ़ील्ड में मौजूद लोगों की जानकारी अपडेट होती है.
  • UpdateDraftCcRecipientsAction—यह एक ऐसी कार्रवाई है जिससे मौजूदा ड्राफ़्ट ईमेल की कॉपी पाने वाले लोगों की सूची अपडेट होती है.
  • UpdateDraftBccRecipientsAction—इससे ऐसी कार्रवाई का पता चलता है जिससे मौजूदा ड्राफ़्ट ईमेल के गुप्त कॉपी फ़ील्ड में मौजूद लोगों की सूची अपडेट होती है.

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