Gmail मैसेज पढ़ते समय, उपयोगकर्ता को कार्ड पर आधारित इंटरफ़ेस दिखता है. इसके अलावा, Gmail को बेहतर बनाने वाले Google Workspace ऐड-ऑन, उपयोगकर्ता के लिए एक और इंटरफ़ेस उपलब्ध करा सकते हैं. यह इंटरफ़ेस तब दिखता है, जब उपयोगकर्ता नए मैसेज लिख रहा हो या मौजूदा मैसेज का जवाब दे रहा हो. इससे ऐड-ऑन, उपयोगकर्ता के लिए ईमेल लिखने के काम को ऑटोमेट कर सकते हैं.
Google Workspace ऐड-ऑन के कंपोज़ यूज़र इंटरफ़ेस (यूआई) का ऐक्सेस पाना
ऐड-ऑन के कंपोज़ यूज़र इंटरफ़ेस (यूआई) को देखने के दो तरीके हैं. पहला तरीका यह है कि ऐड-ऑन के खुले होने के दौरान, नया ड्राफ़्ट लिखना शुरू किया जाए या किसी ड्राफ़्ट का जवाब दिया जाए. दूसरा तरीका यह है कि ड्राफ़्ट लिखते समय, ऐड-ऑन को चालू किया जाए.
दोनों ही मामलों में, ऐड-ऑन, कंपोज़ ट्रिगर फ़ंक्शन को लागू करता है. यह फ़ंक्शन, ऐड-ऑन के मेनिफ़ेस्ट में तय किया जाता है. कंपोज़ ट्रिगर फ़ंक्शन, कंपोज़ ऐक्शन के लिए कंपोज़ यूज़र इंटरफ़ेस (यूआई) बनाता है. इसके बाद, Gmail इसे उपयोगकर्ता को दिखाता है.
कंपोज़ ऐड-ऑन बनाना
यहां दिए गए सामान्य चरणों को पूरा करके, किसी ऐड-ऑन में कंपोज़ की सुविधा जोड़ी जा सकती है:
- ऐड-ऑन के स्क्रिप्ट प्रोजेक्ट मेनिफ़ेस्ट में,
gmail.composeTriggerफ़ील्ड जोड़ें. साथ ही, मेनिफ़ेस्ट के स्कोप को अपडेट करके, कंपोज़ ऐक्शन के लिए ज़रूरी स्कोप शामिल करें. - कंपोज़ ट्रिगर फ़ंक्शन लागू करें. यह फ़ंक्शन, ट्रिगर होने पर कंपोज़ यूज़र इंटरफ़ेस (यूआई) बनाता है. कंपोज़ ट्रिगर फ़ंक्शन, कंपोज़ ऐक्शन के लिए कंपोज़ यूज़र इंटरफ़ेस (यूआई) बनाने वाले
Cardऑब्जेक्ट याCardऑब्जेक्ट का अरे दिखाता है. - उपयोगकर्ता के कंपोज़ यूज़र इंटरफ़ेस (यूआई) के साथ इंटरैक्शन पर प्रतिक्रिया देने के लिए, ज़रूरी कॉलबैक फ़ंक्शन लागू करें. ये फ़ंक्शन, कंपोज़ ऐक्शन नहीं होते. कंपोज़ ऐक्शन से सिर्फ़ कंपोज़ यूज़र इंटरफ़ेस (यूआई) दिखता है. इसके बजाय, ये अलग-अलग फ़ंक्शन होते हैं. इनसे यह तय होता है कि कंपोज़ यूज़र इंटरफ़ेस (यूआई) के अलग-अलग एलिमेंट चुने जाने पर क्या होता है. उदाहरण के लिए, बटन वाले यूज़र इंटरफ़ेस (यूआई) कार्ड में आम तौर पर एक कॉलबैक फ़ंक्शन होता है. जब कोई उपयोगकर्ता उस बटन पर क्लिक करता है, तो यह फ़ंक्शन लागू होता है. ऐसे विजेट के लिए कॉलबैक फ़ंक्शन, जो ड्राफ़्ट मैसेज
के कॉन्टेंट को अपडेट करते हैं, उन्हें
UpdateDraftActionResponseऑब्जेक्ट दिखाना चाहिए.
कंपोज़ ट्रिगर फ़ंक्शन
ऐड-ऑन का कंपोज़ यूज़र इंटरफ़ेस (यूआई), ऐड-ऑन के मैसेज यूज़र इंटरफ़ेस (यूआई) की तरह ही बनाया जाता है. इसके लिए, Apps Script की Card सेवा का इस्तेमाल करके कार्ड बनाए जाते हैं और उनमें विजेट जोड़े जाते हैं.
आपको अपने मेनिफ़ेस्ट में तय किया गया
gmail.composeTrigger.selectActions[].runFunction
लागू करना होगा. कंपोज़ ट्रिगर फ़ंक्शन को, उस ऐक्शन के लिए कंपोज़ यूज़र इंटरफ़ेस (यूआई) बनाने वाले
एक Card ऑब्जेक्ट या
Card ऑब्जेक्ट का अरे दिखाना होगा. ये फ़ंक्शन, कॉन्टेक्चुअल ट्रिगर फ़ंक्शन के जैसे ही होते हैं और इन्हें उसी तरह कार्ड बनाने चाहिए.
कंपोज़ ट्रिगर इवेंट ऑब्जेक्ट
जब कोई कंपोज़ ऐक्शन चुना जाता है, तो वह कंपोज़ ट्रिगर फ़ंक्शन को लागू करता है. साथ ही, फ़ंक्शन को पैरामीटर के तौर पर एक इवेंट ऑब्जेक्ट पास करता है. इवेंट ऑब्जेक्ट, ट्रिगर फ़ंक्शन को ऐड-ऑन के कॉन्टेक्स्ट और लिखे जा रहे ड्राफ़्ट के बारे में जानकारी दे सकता है.
इवेंट ऑब्जेक्ट में जानकारी को व्यवस्थित करने के तरीके के बारे में जानने के लिए, इवेंट ऑब्जेक्ट का स्ट्रक्चर
देखें. इवेंट ऑब्जेक्ट में मौजूद जानकारी को,
मेनिफ़ेस्ट फ़ील्ड की वैल्यू से कंट्रोल किया जाता है:gmail.composeTrigger.draftAccess
अगर
gmail.composeTrigger.draftAccessमेनिफ़ेस्ट फ़ील्ड की वैल्यूNONEहै या इसे शामिल नहीं किया गया है, तो इवेंट ऑब्जेक्ट में सिर्फ़ ज़रूरी जानकारी होती है.अगर
gmail.composeTrigger.draftAccessकी वैल्यूMETADATAपर सेट है, तो कंपोज़ ट्रिगर फ़ंक्शन को पास किए गए इवेंट ऑब्जेक्ट में लिखे जा रहे ईमेल के पाने वाले लोगों की सूचियां शामिल होती हैं.METADATAड्राफ़्ट ऐक्सेस का इस्तेमाल करने के लिए, ज़रूरी है कि ऐड-ऑन के मेनिफ़ेस्ट में,https://www.googleapis.com/auth/gmail.addons.current.message.metadataGmail स्कोप शामिल हो.
चालू ड्राफ़्ट में कॉन्टेंट जोड़ना
आम तौर पर, ऐड-ऑन का कंपोज़ यूज़र इंटरफ़ेस (यूआई), उपयोगकर्ता को ऐसे विकल्प और कंट्रोल उपलब्ध कराता है जिनसे मैसेज लिखने में मदद मिलती है. इन इस्तेमाल के मामलों में, जब उपयोगकर्ता यूज़र इंटरफ़ेस (यूआई) में विकल्प चुन लेता है, तो ऐड-ऑन उन विकल्पों को समझता है और मौजूदा ड्राफ़्ट ईमेल को उसके मुताबिक अपडेट करता है.
मौजूदा ड्राफ़्ट ईमेल को अपडेट करना आसान बनाने के लिए, Card सेवा को इन क्लास के साथ बढ़ाया गया है:
ContentType—यह एक एनम है. इससे यह तय होता है कि बदलाव किया जा सकने वाला एचटीएमएल, बदलाव नहीं किया जा सकने वाला एचटीएमएल (जिसे Gmail उपयोगकर्ता में बदलाव नहीं कर सकते) या सादा टेक्स्ट कॉन्टेंट जोड़ा जाए.UpdateDraftActionResponse—यह किसी ऐसे ऐक्शन के जवाब को दिखाता है जिससे मौजूदा ड्राफ़्ट ईमेल अपडेट होता है.UpdateDraftActionResponseBuilder—यहUpdateDraftActionResponseऑब्जेक्ट के लिए एक बिल्डर है.UpdateDraftBodyAction—यह एक ऐसे ऐक्शन को दिखाता है जिससे मौजूदा ड्राफ़्ट ईमेल का मुख्य भाग अपडेट होता है.UpdateDraftBodyType—यह एक एनम है. इससे यह तय होता है कि मुख्य भाग में कैसे बदलाव किया जाए.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;
}