您可以在 Google Workspace 外掛程式中建立已連結動作的小工具。您可以使用動作撰寫新的電子郵件草稿,並視需要使用在外掛程式 UI 中輸入的資訊或已開啟的郵件中的資訊填入草稿。舉例來說,您可以在外掛程式的訊息 UI 中使用按鈕,為目前開啟的訊息建立回覆,並預先填入來自外掛程式的資訊。
當建構訊息的動作觸發時,Gmail 會執行回呼函式來建構並傳回草稿。接著,Gmail 會在標準電子郵件撰寫視窗顯示草稿,讓使用者視需要編輯和傳送。
設定動作以建立郵件草稿
如要設定小工具在選取時啟動草稿建構動作,您必須執行下列操作:
請確認您的資訊清單包含
action.compose
範圍:https://www.googleapis.com/auth/gmail.addons.current.action.compose
您可以改用更多寬鬆的範圍,但建議只在絕對必要時採用。
呼叫小工具的
setComposeAction()
小工具處理常式函式,提供Action
物件並指定ComposeEmailType
。實作執行草稿建立動作的回呼函式。這個函式會提供事件物件做為引數。回呼函式必須執行以下作業:
- 建立
GmailDraft
物件。 - 使用
ComposeActionResponseBuilder
類別和GmailDraft
物件建構ComposeActionResponse
物件。 - 傳回已建構的
ComposeActionResponse
。
- 建立
您可以在回呼函式中建立的 GmailDraft
預先填入收件者、主旨、訊息內文和附件。為了填入草稿,資料可以來自任何來源,但通常來自向外掛程式本身提供的資訊、開放式訊息中的資訊,或是從第三方服務收集到的資訊。傳遞至回呼函式的事件物件包含開放訊息 ID,以及可用來預先填入草稿的其他外掛程式資訊。
您可以將草稿建立為新的獨立訊息,或是回覆現有訊息。這由提供給 setComposeAction()
的 ComposeEmailType
列舉控制。您可以建立單一回覆郵件或「回覆所有人」的郵件草稿。
獨立草稿
獨立草稿會啟動新的執行緒,而不是回覆任何現有訊息。您可以使用下列任一 Gmail 服務功能建立獨立草稿:
GmailApp.createDraft(recipient, subject, body)
GmailApp.createDraft(recipient, subject, body, options)
回覆草稿
回覆草稿是現有訊息串的一部分。回覆草稿是只傳送給郵件寄件者的單一回覆,另一種則是傳送給所有收到該訊息的「回覆全部」草稿。您可以使用下列其中一項 Gmail 服務功能建立回覆草稿:
GmailMessage.createDraftReply(body)
GmailMessage.createDraftReply(body, options)
GmailMessage.createDraftReplyAll(body)
GmailMessage.createDraftReplyAll(body, options)
GmailThread.createDraftReply(body)
GmailThread.createDraftReply(body, options)
GmailThread.createDraftReplyAll(body)
GmailThread.createDraftReplyAll(body, options)
範例
下列程式碼片段說明如何將建構回覆草稿的動作指派給按鈕。
var composeAction = CardService.newAction()
.setFunctionName('createReplyDraft');
var composeButton = CardService.newTextButton()
.setText('Compose Reply')
.setComposeAction(
composeAction,
CardService.ComposedEmailType.REPLY_AS_DRAFT);
// ...
/**
* Creates a draft email (with an attachment and inline image)
* as a reply to an existing message.
* @param {Object} e An event object passed by the action.
* @return {ComposeActionResponse}
*/
function createReplyDraft(e) {
// Activate temporary Gmail scopes, in this case to allow
// a reply to be drafted.
var accessToken = e.gmail.accessToken;
GmailApp.setCurrentMessageAccessToken(accessToken);
// Creates a draft reply.
var messageId = e.gmail.messageId;
var message = GmailApp.getMessageById(messageId);
var draft = message.createDraftReply('',
{
htmlBody: "Kitten! <img src='cid:kitten'/>",
attachments: [
UrlFetchApp.fetch('https://example.com/images/myDog.jpg')
.getBlob()
],
inlineImages: {
"kitten": UrlFetchApp.fetch('https://example.com/images/myKitten.jpg')
.getBlob()
}
}
);
// Return a built draft response. This causes Gmail to present a
// compose window to the user, pre-filled with the content specified
// above.
return CardService.newComposeActionResponseBuilder()
.setGmailDraft(draft).build();
}