-
Notifications
You must be signed in to change notification settings - Fork 0
/
gmail.gs
92 lines (85 loc) · 3.23 KB
/
gmail.gs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/**
* Callback for rendering the card for a specific Gmail message.
* @param {Object} e The event object.
* @return {CardService.Card} The card to show to the user.
*/
function onGmailMessage(e) {
console.log(e);
// Get the ID of the message the user has open.
var messageId = e.gmail.messageId;
// Get an access token scoped to the current message and use it for GmailApp
// calls.
var accessToken = e.gmail.accessToken;
GmailApp.setCurrentMessageAccessToken(accessToken);
// Get the subject of the email.
var message = GmailApp.getMessageById(messageId);
var subject = message.getThread().getFirstMessageSubject();
// Remove labels and prefixes.
subject = subject
.replace(/^([rR][eE]|[fF][wW][dD])\:\s*/, '')
.replace(/^\[.*?\]\s*/, '');
// If neccessary, truncate the subject to fit in the image.
subject = truncate(subject);
return createCatCard(subject);
}
/**
* Callback for rendering the card for the compose action dialog.
* @param {Object} e The event object.
* @return {CardService.Card} The card to show to the user.
*/
function onGmailCompose(e) {
console.log(e);
var header = CardService.newCardHeader()
.setTitle('Insert cat')
.setSubtitle('Add a custom cat image to your email message.');
// Create text input for entering the cat's message.
var input = CardService.newTextInput()
.setFieldName('text')
.setTitle('Caption')
.setHint('What do you want the cat to say?');
// Create a button that inserts the cat image when pressed.
var action = CardService.newAction()
.setFunctionName('onGmailInsertCat');
var button = CardService.newTextButton()
.setText('Insert cat')
.setOnClickAction(action)
.setTextButtonStyle(CardService.TextButtonStyle.FILLED);
var buttonSet = CardService.newButtonSet()
.addButton(button);
// Assemble the widgets and return the card.
var section = CardService.newCardSection()
.addWidget(input)
.addWidget(buttonSet);
var card = CardService.newCardBuilder()
.setHeader(header)
.addSection(section);
return card.build();
}
/**
* Callback for inserting a cat into the Gmail draft.
* @param {Object} e The event object.
* @return {CardService.UpdateDraftActionResponse} The draft update response.
*/
function onGmailInsertCat(e) {
console.log(e);
// Get the text that was entered by the user.
var text = e.formInput.text;
// Use the "Cat as a service" API to get the cat image. Add a "time" URL
// parameter to act as a cache buster.
var now = new Date();
var imageUrl = 'https://cataas.com/cat';
if (text) {
// Replace formward slashes in the text, as they break the CataaS API.
var caption = text.replace(/\//g, ' ');
imageUrl += Utilities.formatString('/says/%s?time=%s',
encodeURIComponent(caption), now.getTime());
}
var imageHtmlContent = '<img style="display: block; max-height: 300px;" src="'
+ imageUrl + '"/>';
var response = CardService.newUpdateDraftActionResponseBuilder()
.setUpdateDraftBodyAction(CardService.newUpdateDraftBodyAction()
.addUpdateContent(imageHtmlContent,CardService.ContentType.MUTABLE_HTML)
.setUpdateType(CardService.UpdateDraftBodyType.IN_PLACE_INSERT))
.build();
return response;
}