Reputation: 21
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!
Upvotes: 2
Views: 1399
Reputation: 81
I believe the proper way to pass the currentMessage content to the callAPI function would be to use the setParameters as described in this documentation
Your code would look like this:
var submitForm = CardService.newAction()
.setFunctionName('callAPI')
.setParameters({message: currentMessage});
In the callback, your would fetch the message by using something like:
function callAPI(event) {
var payload = event.parameters.message;
...
As a reminder, both the key (message) as the value (currentMessage) must be Strings.
Upvotes: 8
Reputation: 19309
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
.
createCard
after declaring currentMessage
:var userProperties = PropertiesService.getUserProperties();
userProperties.setProperty("currentMessage", currentMessage);
callAPI
:var userProperties = PropertiesService.getUserProperties();
var msg = userProperties.getProperty("currentMessage");
In order to avoid the limit of characters for a single property, you could also split your message into as many properties as were necessary.
createCard
, after declaring currentMessage
:var messageArray = [];
var i = 0;
var k = 5000; // Character limit for one property (change accordingly)
while (i < currentMessage.length) {
var part = currentMessage.substring(i, i += k);
messageArray.push(part); // Splitting string into an array of strings
}
var userProperties = PropertiesService.getUserProperties();
userProperties.deleteAllProperties(); // Delete old properties
for (var j = 0; j < messageArray.length; j++) { // Set a different property for each element in the array
userProperties.setProperty('messagePart' + j, messageArray[j]);
}
callAPI
:var userProperties = PropertiesService.getUserProperties();
var keys = userProperties.getKeys(); // Get all property keys
var j = 0;
var currentMessage = "";
do {
var part = userProperties.getProperty('messagePart' + j);
currentMessage = currentMessage.concat(part); // Concat each property value to a single string.
j++;
} while (keys.indexOf('messagePart' + j) !== -1); // Check if property key exists
I hope this is of any help.
Upvotes: -1