My gmail addon consists on several cards. For card navigation I widely use setOnClickAction, e.g.
CardService.newAction().setFunctionName('openUserCard').setParameters({userJSON: JSON.stringify(user)})
Gmail addon reference says both keys and values of setParameters
method must be strings. That's why it's impossible to send any complex object from one card to another.
Global variables are not supported as well. One can use PropertiesService
for storing some data, but that also restricted to strings.
I have initial and export cards. On initial card there's current email data importer that looks like that:
function buildAddon(e) {
var accessToken = e.messageMetadata.accessToken;
GmailApp.setCurrentMessageAccessToken(accessToken);
var message = GmailApp.getMessageById(e.messageMetadata.messageId);
var attachments = message.getAttachments();
... we can do anything with attachments here...
The problem is that I have to use attachments not on the initial, but on the other, export card, to POST them to some external api. But I can not send attachments
array directly using setOnClickAction
, because it consists on complex objects with methods.
That's why I send the initial e.messageMetadata
object to the export card, and there repeat all the operations above: setCurrentMessageAccessToken
, getMessageById
, getAttachments
, and then for each attachment get it's content by attachment.getBytes()
and send to external api.
If a customer goes to export card immediately, this all works. But if he browses some other cards for several minutes, and then goes to export, the call to GmailApp.getMessageById(messageMetadata.messageId)
returns an error Access Denied:: Expired access token
.
How to avoid this?
Each action receives just one parameter, the argument 'e' event.
Then if we inspect that 'e', we will find a JSON object with the property parameters, which is the our parameters sent into the action function via the setParameters() method of the Action.
Inside this variable 'e' there is also a property called messageMetadata with all the proper values.
var myAction = CardService.newAction().setFunctionName("xpto").setParameters({ name: "banana"} );
function xpto(e) {
var name = e.parameters.name;
}
A sample 'e' event has the following JSON inside:
{
formInput = {},
clientPlatform = web,
messageMetadata = {
messageId= ... ,
accessToken= ...
},
formInputs = {},
parameters = { name=Banana }
}
Hope this is still useful.