-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Attendance Chat App codelab sources into solutions (#478)
Co-authored-by: pierrick <[email protected]>
- Loading branch information
1 parent
4714a6a
commit 2e6ee8b
Showing
11 changed files
with
543 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# Attendance Chat App | ||
|
||
This code sample shows how to build a Google Chat app using Google | ||
Apps Script. The Chat app responds to messages in a space or direct message (DM) and | ||
allows the user to set a vacation responder in Gmail or add an all-day event to | ||
their Calendar from Google Chat. | ||
|
||
## Usage | ||
|
||
You can follow [this codelab](https://developers.google.com/codelabs/chat-apps-script) | ||
to build and test this Chat app. | ||
|
||
To use this Chat app, you must enable the Hangouts Chat API in the | ||
[Google API Console](https://console.developers.google.com/). After enabling | ||
the API, configuring the Chat app, and publishing it, you must add the Chat app to a space | ||
or DM to begin a conversation. | ||
|
||
For more information about how to publish a Chat app, see | ||
[Publishing Google Chat apps](https://developers.google.com/workspace/chat/apps-publish). | ||
|
||
## Disclaimer | ||
|
||
This is not an official product. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,160 @@ | ||
/** | ||
* Responds to an ADDED_TO_SPACE event in Google Chat. | ||
* @param {object} event the event object from Google Chat | ||
* @return {object} JSON-formatted response | ||
* @see https://developers.google.com/workspace/chat/receive-respond-interactions | ||
*/ | ||
function onAddToSpace(event) { | ||
console.info(event); | ||
var message = 'Thank you for adding me to '; | ||
if (event.space.type === 'DM') { | ||
message += 'a DM, ' + event.user.displayName + '!'; | ||
} else { | ||
message += event.space.displayName; | ||
} | ||
return { text: message }; | ||
} | ||
|
||
/** | ||
* Responds to a REMOVED_FROM_SPACE event in Google Chat. | ||
* @param {object} event the event object from Google Chat | ||
* @see https://developers.google.com/workspace/chat/receive-respond-interactions | ||
*/ | ||
function onRemoveFromSpace(event) { | ||
console.info(event); | ||
console.log('Chat app removed from ', event.space.name); | ||
} | ||
|
||
var DEFAULT_IMAGE_URL = 'https://goo.gl/bMqzYS'; | ||
var HEADER = { | ||
header: { | ||
title : 'Attendance Chat app', | ||
subtitle : 'Log your vacation time', | ||
imageUrl : DEFAULT_IMAGE_URL | ||
} | ||
}; | ||
|
||
/** | ||
* Creates a card-formatted response. | ||
* @param {object} widgets the UI components to send | ||
* @return {object} JSON-formatted response | ||
*/ | ||
function createCardResponse(widgets) { | ||
return { | ||
cards: [HEADER, { | ||
sections: [{ | ||
widgets: widgets | ||
}] | ||
}] | ||
}; | ||
} | ||
|
||
var REASON = { | ||
SICK: 'Out sick', | ||
OTHER: 'Out of office' | ||
}; | ||
/** | ||
* Responds to a MESSAGE event triggered in Google Chat. | ||
* @param {object} event the event object from Google Chat | ||
* @return {object} JSON-formatted response | ||
*/ | ||
function onMessage(event) { | ||
console.info(event); | ||
var reason = REASON.OTHER; | ||
var name = event.user.displayName; | ||
var userMessage = event.message.text; | ||
|
||
// If the user said that they were 'sick', adjust the image in the | ||
// header sent in response. | ||
if (userMessage.indexOf('sick') > -1) { | ||
// Hospital material icon | ||
HEADER.header.imageUrl = 'https://goo.gl/mnZ37b'; | ||
reason = REASON.SICK; | ||
} else if (userMessage.indexOf('vacation') > -1) { | ||
// Spa material icon | ||
HEADER.header.imageUrl = 'https://goo.gl/EbgHuc'; | ||
} | ||
|
||
var widgets = [{ | ||
textParagraph: { | ||
text: 'Hello, ' + name + '.<br/>Are you taking time off today?' | ||
} | ||
}, { | ||
buttons: [{ | ||
textButton: { | ||
text: 'Set vacation in Gmail', | ||
onClick: { | ||
action: { | ||
actionMethodName: 'turnOnAutoResponder', | ||
parameters: [{ | ||
key: 'reason', | ||
value: reason | ||
}] | ||
} | ||
} | ||
} | ||
}, { | ||
textButton: { | ||
text: 'Block out day in Calendar', | ||
onClick: { | ||
action: { | ||
actionMethodName: 'blockOutCalendar', | ||
parameters: [{ | ||
key: 'reason', | ||
value: reason | ||
}] | ||
} | ||
} | ||
} | ||
}] | ||
}]; | ||
return createCardResponse(widgets); | ||
} | ||
|
||
/** | ||
* Responds to a CARD_CLICKED event triggered in Google Chat. | ||
* @param {object} event the event object from Google Chat | ||
* @return {object} JSON-formatted response | ||
* @see https://developers.google.com/workspace/chat/receive-respond-interactions | ||
*/ | ||
function onCardClick(event) { | ||
console.info(event); | ||
var message = ''; | ||
var reason = event.action.parameters[0].value; | ||
if (event.action.actionMethodName == 'turnOnAutoResponder') { | ||
turnOnAutoResponder(reason); | ||
message = 'Turned on vacation settings.'; | ||
} else if (event.action.actionMethodName == 'blockOutCalendar') { | ||
blockOutCalendar(reason); | ||
message = 'Blocked out your calendar for the day.'; | ||
} else { | ||
message = "I'm sorry; I'm not sure which button you clicked."; | ||
} | ||
return { text: message }; | ||
} | ||
|
||
var ONE_DAY_MILLIS = 24 * 60 * 60 * 1000; | ||
/** | ||
* Turns on the user's vacation response for today in Gmail. | ||
* @param {string} reason the reason for vacation, either REASON.SICK or REASON.OTHER | ||
*/ | ||
function turnOnAutoResponder(reason) { | ||
var currentTime = (new Date()).getTime(); | ||
Gmail.Users.Settings.updateVacation({ | ||
enableAutoReply: true, | ||
responseSubject: reason, | ||
responseBodyHtml: "I'm out of the office today; will be back on the next business day.<br><br><i>Created by Attendance Chat app!</i>", | ||
restrictToContacts: true, | ||
restrictToDomain: true, | ||
startTime: currentTime, | ||
endTime: currentTime + ONE_DAY_MILLIS | ||
}, 'me'); | ||
} | ||
|
||
/** | ||
* Places an all-day meeting on the user's Calendar. | ||
* @param {string} reason the reason for vacation, either REASON.SICK or REASON.OTHER | ||
*/ | ||
function blockOutCalendar(reason) { | ||
CalendarApp.createAllDayEvent(reason, new Date(), new Date(Date.now() + ONE_DAY_MILLIS)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
{ | ||
"timeZone": "America/Los_Angeles", | ||
"dependencies": { | ||
"enabledAdvancedServices": [{ | ||
"userSymbol": "Gmail", | ||
"serviceId": "gmail", | ||
"version": "v1" | ||
}] | ||
}, | ||
"chat": { | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/** | ||
* Responds to an ADDED_TO_SPACE event | ||
* in Google Chat. | ||
* | ||
* @param event the event object from Google Chat | ||
* @return JSON-formatted response | ||
*/ | ||
function onAddToSpace(event) { | ||
console.info(event); | ||
|
||
var message = ""; | ||
|
||
if (event.space.type === "DM") { | ||
message = "Thank you for adding me to a DM, " + | ||
event.user.displayName + "!"; | ||
} else { | ||
message = "Thank you for adding me to " + | ||
event.space.displayName; | ||
} | ||
|
||
return { "text": message }; | ||
} | ||
|
||
/** | ||
* Responds to a REMOVED_FROM_SPACE event | ||
* in Google Chat. | ||
* | ||
* @param event the event object from Google Chat | ||
*/ | ||
function onRemoveFromSpace(event) { | ||
console.info(event); | ||
console.info("Chat app removed from ", event.space.name); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"timeZone": "America/Los_Angeles", | ||
"dependencies": { | ||
}, | ||
"chat": {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/** | ||
* Responds to an ADDED_TO_SPACE event | ||
* in Google Chat. | ||
* | ||
* @param event the event object from Google Chat | ||
* @return JSON-formatted response | ||
*/ | ||
function onAddToSpace(event) { | ||
console.info(event); | ||
|
||
var message = ""; | ||
|
||
if (event.space.type === "DM") { | ||
message = "Thank you for adding me to a DM, " + | ||
event.user.displayName + "!"; | ||
} else { | ||
message = "Thank you for adding me to " + | ||
event.space.displayName; | ||
} | ||
|
||
return { "text": message }; | ||
} | ||
|
||
/** | ||
* Responds to a REMOVED_FROM_SPACE event | ||
* in Google Chat. | ||
* | ||
* @param event the event object from Google Chat | ||
*/ | ||
function onRemoveFromSpace(event) { | ||
console.info(event); | ||
console.info("Chat app removed from ", event.space.name); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"timeZone": "America/Los_Angeles", | ||
"dependencies": { | ||
}, | ||
"chat": {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
/** | ||
* Responds to an ADDED_TO_SPACE event | ||
* in Google Chat. | ||
* | ||
* @param event the event object from Google Chat | ||
* @return JSON-formatted response | ||
*/ | ||
function onAddToSpace(event) { | ||
console.info(event); | ||
|
||
var message = ""; | ||
|
||
if (event.space.type === "DM") { | ||
message = "Thank you for adding me to a DM, " + | ||
event.user.displayName + "!"; | ||
} else { | ||
message = "Thank you for adding me to " + | ||
event.space.displayName; | ||
} | ||
|
||
return { "text": message }; | ||
} | ||
|
||
/** | ||
* Responds to a REMOVED_FROM_SPACE event | ||
* in Google Chat. | ||
* | ||
* @param event the event object from Google Chat | ||
*/ | ||
function onRemoveFromSpace(event) { | ||
console.info(event); | ||
console.info("Chat app removed from ", event.space.name); | ||
} | ||
|
||
/** | ||
* Creates a card-formatted response. | ||
* | ||
* @param widgets the UI components to send | ||
* @return JSON-formatted response | ||
*/ | ||
function createCardResponse(widgets) { | ||
return { | ||
"cards": [ | ||
header, | ||
{ | ||
"sections": [{ | ||
"widgets": widgets | ||
}] | ||
}] | ||
}; | ||
} | ||
|
||
/** | ||
* Responds to a MESSAGE event triggered in Google Chat. | ||
* | ||
* @param event the event object from Google Chat | ||
* @return JSON-formatted response | ||
*/ | ||
function onMessage(event) { | ||
var userMessage = event.message.text; | ||
|
||
var widgets = [{ | ||
"textParagraph": { | ||
"text": "You said: " + userMessage | ||
} | ||
}]; | ||
|
||
return createCardResponse(widgets); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"timeZone": "America/Los_Angeles", | ||
"dependencies": { | ||
}, | ||
"chat": {} | ||
} |
Oops, something went wrong.