How to create a standalone Draft message from my gmail add-on, when user is already in composer?

428 views Asked by At

Use case :

I'm developing a Gmail add-on.

My add-on has a composer trigger button, which will include attachments taken from my website. When any error occurs there, I would like to provide a Report Issue button, which will create a draft mail to my email address, with user activeUserKey value (Session.getTemporaryActiveUserKey()).

Issue :

I can use the below function to create a Draft mail.

GmailApp.createDraft(recipient, subject, body, options)

But, it requires access token. So, I tried using the below function

GmailApp.setCurrentMessageAccessToken(accessToken);

My problem here is, how can I get this accessToken from event object from composer?

event.messageMetadata.accessToken is accessible when any email is opened. But, I have only composer opened while triggering my callback function, so my event object don't have any messageMetadata.

Is there any other alternative option to create Draft mail without using accessToken / any other way to get accessToken from composer window ?

1

There are 1 answers

1
Omar Alvarez On

As the composer window is already open and from where you opened your add-on, the following should work. This should be inside a function that is called by the setOnClickAction, from the button. The only thing I haven't tried is to change/add a recipient. Let me know if it works.

Then your function just need to return an updateDraftBodyAction.

var updateDraftActionResponse = CardService.newUpdateDraftActionResponseBuilder()
  .setUpdateDraftBodyAction(CardService.newUpdateDraftBodyAction()
  .addUpdateContent("<a href=\"https://www.google.com\">Google</a>",
            CardService.ContentType.MUTABLE_HTML)

  .setUpdateType(CardService.UpdateDraftBodyType.IN_PLACE_INSERT))
  .build();

return updateDraftActionResponse;

You can check the google code example here https://developers.google.com/gmail/add-ons/how-tos/extending-compose-ui