我正在尝试创建一个适用于 Gmail 的 Google Workspace 插件,用于读取电子邮件并撰写草稿。
我正在关注文档https://developers.google.com/workspace/add-ons/gmail/compose,我只是复制/粘贴示例中的代码,这里是我的代码:
appsscript.json
{
"timeZone": "Etc/GMT",
"oauthScopes": [
"https://www.googleapis.com/auth/gmail.addons.execute",
"https://www.googleapis.com/auth/gmail.addons.current.action.compose",
"https://www.googleapis.com/auth/gmail.addons.current.message.metadata",
"https://www.googleapis.com/auth/gmail.metadata",
"https://www.googleapis.com/auth/gmail.modify"
],
"gmail": {
"name": "Test!",
"logoUrl": "https://www.gstatic.com/images/icons/material/system/1x/receipt_black_24dp.png",
"contextualTriggers": [
{
"unconditional": {},
"onTriggerFunction": "getContextualAddOn"
}
],
"composeTrigger": {
"selectActions": [
{
"text": "Generate answer",
"runFunction": "createReplyDraft"
}
],
"draftAccess": "METADATA"
},
"primaryColor": "#41f470",
"secondaryColor": "#94f441"
},
"dependencies": {
"enabledAdvancedServices": [
{
"userSymbol": "Gmail",
"version": "v1",
"serviceId": "gmail"
}
]
}
}
代码:
/**
* 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('ciao');
// 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();
}
这发生在这一行:
var message = GmailApp.getMessageById(messageId);
我感觉messageId为null,但不知道如何调试。无论如何,由于我只是从文档中复制了示例,所以我不知道如何解决这个问题,你知道我做错了什么吗?
要了解调试 Google Apps 脚本代码,请阅读 https://developers.google.com/apps-script/guides/support/troubleshooting。在那里,您将学习调试、日志记录、执行日志和调试器的基础知识。
有问题的代码不仅仅是代码的“副本”https://developers.google.com/workspace/add-ons/gmail/compose,因为它遗漏了一些部分并更改了其他部分。更具体地说,它与书面说明不一致。更具体地说,清单包含用于调用
createReplyDraft
的 composeTrigger,但此函数应由 Google Apps 脚本操作调用。当编辑现有草稿以按预期创建新草稿时,可能会使用它。参考文章中的代码包含以下内容,但不包含在问题正文中:
var composeAction = CardService.newAction()
.setFunctionName('createReplyDraft');
var composeButton = CardService.newTextButton()
.setText('Compose Reply')
.setComposeAction(
composeAction,
CardService.ComposedEmailType.REPLY_AS_DRAFT);