Gmail附加传递参数可通过卡片操作触发功能 [英] Gmail Add-on pass parameters to function triggered by card action

查看:44
本文介绍了Gmail附加传递参数可通过卡片操作触发功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个Gmail加载项,该加载项仅用一个表单按钮即可创建一张卡片.单击后,我希望按钮触发一个功能,该功能会将打开的电子邮件的内容发送到外部API.

I'm building a Gmail add-on that creates a card with just one form button. Once clicked, I want the button to trigger a function that will send the open email's content to an external API.

到目前为止,我有这样的事情:

So far, I have something like this:

function createCard(event) {
  var currentMessage = getCurrentMessage(event).getBody();
  var section = CardService.newCardSection();

  var submitForm = CardService.newAction()
    .setFunctionName('callAPI');

  var submitButton = CardService.newTextButton()
    .setText('Submit')
    .setOnClickAction(submitForm);

  section.addWidget(CardService.newButtonSet()
    .addButton(submitButton));

  var card = CardService.newCardBuilder()
    .setHeader(CardService.newCardHeader()
    .setTitle('Click this button'))
    .addSection(section)
    .build();

  return [card];
}

function callAPI(event) {
  var payload = { "msg": msg }; // msg is the parameter I need to get from the function call
  var options = {
        "method"  : "POST",
        "contentType": "application/json",
        "payload" : JSON.stringify(payload),
        "followRedirects" : true,
        "muteHttpExceptions": true
  };

  return UrlFetchApp.fetch('https://www.someAPI.com/api/endpoint', options);
}

如何将 currentMessage 变量传递给 callAPI 函数?根据文档,我们可以从动作函数中获得的唯一参数似乎是 event ,它仅具有表单字段数据.如果没有传递其他参数的方法,该函数是否有办法直接在函数内部获取消息的上下文数据?

How can I pass the currentMessage variable into the callAPI function? According to the documentation, the only parameter that we can get from an action function seems to be event that only has the form fields data. If there isn't a way to pass other parameters, is there a way for that function to get the context data of the message directly inside the function??

谢谢!

推荐答案

您可以使用属性服务,可让您将字符串存储为键值对.想法是将 currentMessage 的值存储在函数 createCard 中的属性中,然后在 callAPI 中使用它.

You can use Properties Service, which allows you to store strings as key-value pairs. The idea would be to store the value of currentMessage in a property in the function createCard, and then use it in callAPI.

  • 首先,要存储变量,可以在声明 currentMessage 后将以下行添加到 createCard 中:
  • First, to store the variable, you can add the following lines to createCard after declaring currentMessage:
var userProperties = PropertiesService.getUserProperties();
userProperties.setProperty("currentMessage", currentMessage);

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆