Skip to content

Commit 1ac6527

Browse files
committed
Adding ooo chat app as a solution.
1 parent 798de96 commit 1ac6527

File tree

3 files changed

+228
-0
lines changed

3 files changed

+228
-0
lines changed

solutions/ooo-chat-app/Code.js

Lines changed: 210 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,210 @@
1+
/**
2+
* Responds to an ADDED_TO_SPACE event in Chat.
3+
* @param {object} event the event object from Chat
4+
* @return {object} JSON-formatted response
5+
* @see https://developers.google.com/hangouts/chat/reference/message-formats/events
6+
*/
7+
function onAddToSpace(event) {
8+
let message = 'Thank you for adding me to ';
9+
if (event.space.type === 'DM') {
10+
message += 'a DM, ' + event.user.displayName + '!';
11+
} else {
12+
message += event.space.displayName;
13+
}
14+
return { text: message };
15+
}
16+
17+
/**
18+
* Responds to a REMOVED_FROM_SPACE event in Chat.
19+
* @param {object} event the event object from Chat
20+
* @param {object} event the event object from Chat
21+
* @see https://developers.google.com/hangouts/chat/reference/message-formats/events
22+
*/
23+
function onRemoveFromSpace(event) {
24+
console.log('App removed from ', event.space.name);
25+
}
26+
27+
28+
/**
29+
* Responds to a MESSAGE event triggered in Chat.
30+
* @param {object} event the event object from Chat
31+
* @return {function} call the respective function
32+
*/
33+
function onMessage(event) {
34+
const message = event.message;
35+
36+
if (message.slashCommand) {
37+
switch (message.slashCommand.commandId) {
38+
case 1: // Help command
39+
return createHelpCard();
40+
case 2: // Block out day command
41+
return blockDayOut();
42+
case 3: // Cancel all meetings command
43+
return cancelAllMeetings();
44+
case 4: // Set auto reply command
45+
return setAutoReply();
46+
}
47+
}
48+
}
49+
50+
function createHelpCard() {
51+
return {
52+
"cardsV2": [
53+
{
54+
"cardId": "2",
55+
"card": {
56+
"sections": [
57+
{
58+
"header": "",
59+
"widgets": [
60+
{
61+
"decoratedText": {
62+
"topLabel": "",
63+
"text": "Hi! 👋 I'm here to help you with your out of office tasks.<br><br>Here's a list of commands I understand.",
64+
"wrapText": true
65+
}
66+
}
67+
]
68+
},
69+
{
70+
"widgets": [
71+
{
72+
"decoratedText": {
73+
"topLabel": "",
74+
"text": "<b>/blockDayOut</b>: I will block out your calendar for you.",
75+
"wrapText": true
76+
}
77+
},
78+
{
79+
"decoratedText": {
80+
"topLabel": "",
81+
"text": "<b>/cancelAllMeetings</b>: I will cancel all your meetings for the day.",
82+
"wrapText": true
83+
}
84+
},
85+
{
86+
"decoratedText": {
87+
"topLabel": "",
88+
"text": "<b>/setAutoReply</b>: Set an out of office auto reply in Gmail.",
89+
"wrapText": true
90+
}
91+
}
92+
]
93+
}
94+
],
95+
"header": {
96+
"title": "OOO app",
97+
"subtitle": "Helping you manage your OOO",
98+
"imageUrl": "https://goo.gle/3SfMkjb",
99+
"imageType": "SQUARE"
100+
}
101+
}
102+
}
103+
]
104+
}
105+
}
106+
107+
/**
108+
* Adds an all day event to the users Google Calendar.
109+
* @return {object} JSON-formatted response
110+
*/
111+
function blockDayOut() {
112+
blockOutCalendar();
113+
return createResponseCard('Your calendar has been blocked out for you.')
114+
}
115+
116+
/**
117+
* Cancels all of the users meeting for the current day.
118+
* @return {object} JSON-formatted response
119+
*/
120+
function cancelAllMeetings() {
121+
cancelMeetings();
122+
return createResponseCard('All your meetings have been canceled.')
123+
}
124+
125+
/**
126+
* Sets an out of office auto reply in the users Gmail account.
127+
* @return {object} JSON-formatted response
128+
*/
129+
function setAutoReply() {
130+
turnOnAutoResponder();
131+
return createResponseCard('The out of office auto reply has been turned on.')
132+
}
133+
134+
135+
const ONE_DAY_MILLIS = 24 * 60 * 60 * 1000;
136+
137+
/**
138+
* Places an all-day meeting on the user's Calendar.
139+
*/
140+
function blockOutCalendar() {
141+
CalendarApp.createAllDayEvent('I am out of office today', new Date(), new Date(Date.now() + ONE_DAY_MILLIS));
142+
}
143+
144+
/**
145+
* Declines all meetings for the day.
146+
*/
147+
148+
function cancelMeetings() {
149+
const events = CalendarApp.getEventsForDay(new Date());
150+
151+
events.forEach(function(event) {
152+
if (event.getGuestList().length > 0) {
153+
event.setMyStatus(CalendarApp.GuestStatus.NO);
154+
}
155+
});
156+
}
157+
158+
/**
159+
* Turns on the user's vacation response for today in Gmail.
160+
*/
161+
function turnOnAutoResponder() {
162+
const currentTime = (new Date()).getTime();
163+
Gmail.Users.Settings.updateVacation({
164+
enableAutoReply: true,
165+
responseSubject: 'I am out of the office today',
166+
responseBodyHtml: 'I am out of the office today; will be back on the next business day.<br><br><i>Created by OOO Chat app!</i>',
167+
restrictToContacts: true,
168+
restrictToDomain: true,
169+
startTime: currentTime,
170+
endTime: currentTime + ONE_DAY_MILLIS
171+
}, 'me');
172+
}
173+
174+
function createResponseCard(responseText) {
175+
return {
176+
"cardsV2": [
177+
{
178+
"cardId": "1",
179+
"card": {
180+
"sections": [
181+
{
182+
"widgets": [
183+
{
184+
"decoratedText": {
185+
"topLabel": "",
186+
"text": responseText,
187+
"startIcon": {
188+
"knownIcon": "NONE",
189+
"altText": "Task done",
190+
"iconUrl": "https://fonts.gstatic.com/s/i/short-term/web/system/1x/task_alt_gm_grey_48dp.png"
191+
},
192+
"wrapText": true
193+
}
194+
}
195+
]
196+
}
197+
],
198+
"header": {
199+
"title": "OOO app",
200+
"subtitle": "Helping you manage your OOO",
201+
"imageUrl": "https://goo.gle/3SfMkjb",
202+
"imageType": "CIRCLE"
203+
}
204+
}
205+
}
206+
]
207+
}
208+
}
209+
210+

solutions/ooo-chat-app/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Sample code for a custom Google Chat app that manages your out of office tasks.
2+
3+
Learn more about Chat apps: https://developers.google.com/chat
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"timeZone": "Europe/Madrid",
3+
"exceptionLogging": "STACKDRIVER",
4+
"runtimeVersion": "V8",
5+
"dependencies": {
6+
"enabledAdvancedServices": [
7+
{
8+
"userSymbol": "Gmail",
9+
"version": "v1",
10+
"serviceId": "gmail"
11+
}
12+
]
13+
},
14+
"chat": {}
15+
}

0 commit comments

Comments
 (0)