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);
}
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??
Thanks!