-
Notifications
You must be signed in to change notification settings - Fork 5
/
gmail.gs
166 lines (136 loc) · 4.2 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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/**
* @fileoverview Code related to Cards and actions specific to the Add-on when
* showing in the context of a user's Gmail.
*/
/**
* Function that's called (per manifest) for Gmail homepage trigger
*
* return {CardService.Card} Card to show for Gmail Homepage
*/
function onGmailHomepage() {
return buildIntegrationCard(CALL_CONTEXT.GMAIL_HOMEPAGE);
}
/**
* Function that's called (per manifest) for Gmail message opened trigger.
*
* @return {CardService.Card} Card to show for opened message.
*/
function onGmailMessageOpened(event) {
let message = getCurrentMessage(event);
// form and pass values from this context to use as merge key/values
let senderName = getMessageSenderName(message);
let senderEmail = getMessageSenderEmail(message);
addMergeKeyValuePair('{{senderName}}', senderName);
addMergeKeyValuePair('{{senderEmail}}', senderEmail);
addMergeKeyValuePair('{{token}}', senderEmail);
return buildIntegrationCard(CALL_CONTEXT.GMAIL_VIEW);
}
/**
* Function that's called (per manifest) for Gmail compose trigger.
*
* @return {CardService.Card} Card to show for opened message.
*/
function onGmailCompose(event) {
let toRecipients = event.draftMetadata.toRecipients;
let toRecipient = '';
if (toRecipients.length > 0) {
toRecipient = toRecipients[0];
}
return buildGmailComposeCard(toRecipient);
}
/**
* Function that returns Gmail homepage Card
*
* return {CardService.Card} Card to show for Gmail Homepage
*/
function buildGmailHomepage() {
let card = CardService.newCardBuilder();
let brandedHeader = buildCustomerBrandedHeader();
card.setHeader(brandedHeader);
let section = CardService.newCardSection();
section.addWidget(CardService.newImage().setImageUrl(OPEN_EMAIL_ICON));
section.addWidget(
CardService.newTextParagraph().setText(
' ' +
' ' +
'Open an email to get started!'
)
);
card.addSection(section);
return card.build();
}
/**
* Function that returns Gmail compose Card
*
* return {CardService.Card} Card to show in Gmail compose message
*/
function buildGmailComposeCard(toRecipient) {
if (!toRecipient) {
let card = CardService.newCardBuilder();
let section = CardService.newCardSection();
section.addWidget(
CardService.newTextParagraph().setText(
'No actions can be taken for an unaddressed email.'
)
);
card.addSection(section);
return card.build();
}
addMergeKeyValuePair('{{senderName}}', toRecipient);
addMergeKeyValuePair('{{senderEmail}}', toRecipient);
addMergeKeyValuePair('{{token}}', toRecipient);
return buildIntegrationCard(CALL_CONTEXT.GMAIL_COMPOSE);
}
/**
* Retrieves the current message given an action event object.
* @param {Event} event Action event object
* @return {Message}
*/
function getCurrentMessage(event) {
let accessToken = event.messageMetadata.accessToken;
let messageId = event.messageMetadata.messageId;
GmailApp.setCurrentMessageAccessToken(accessToken);
return GmailApp.getMessageById(messageId);
}
/**
* Determines date the email was received.
*
* @param {Message} message - The message currently open.
* @returns {String}
*/
function getMessageReceivedDate(message) {
return message.getDate().toLocaleDateString();
}
/**
* Determines the name of whomever sent the message.
*
* @param {Message} message - The message currently open.
* @returns {String}
*/
function getMessageSenderName(message) {
let sender = message.getFrom();
let senderName = sender.split('<')[0].trim();
return senderName;
}
/**
* Determines the email address of whomever sent the message.
*
* @param {Message} message - The message currently open.
* @returns {String}
*/
function getMessageSenderEmail(message) {
let sender = message.getFrom();
let senderEmail = '';
// look for email address in a string like 'Sender Name <[email protected]>'
let re = /[^< ]+(?=>)/g;
let senderParts = sender.match(re);
if (senderParts) {
senderEmail = sender.match(re)[0];
} else {
// can be just straight up email address with no preceeding name or <> symbols
if (sender.includes('@')) {
senderEmail = sender;
}
}
return senderEmail;
}