Skip to content

Commit

Permalink
Add Attendance Chat App codelab sources into solutions (#478)
Browse files Browse the repository at this point in the history
Co-authored-by: pierrick <[email protected]>
  • Loading branch information
PierrickVoulet and pierrick authored Aug 11, 2024
1 parent 4714a6a commit 2e6ee8b
Show file tree
Hide file tree
Showing 11 changed files with 543 additions and 0 deletions.
23 changes: 23 additions & 0 deletions solutions/attendance-chat-app/README.md
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.
160 changes: 160 additions & 0 deletions solutions/attendance-chat-app/final/Code.gs
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));
}
12 changes: 12 additions & 0 deletions solutions/attendance-chat-app/final/appsscript.json
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": {
}
}
33 changes: 33 additions & 0 deletions solutions/attendance-chat-app/step-3/Code.gs
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);
}
6 changes: 6 additions & 0 deletions solutions/attendance-chat-app/step-3/appsscript.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"timeZone": "America/Los_Angeles",
"dependencies": {
},
"chat": {}
}
33 changes: 33 additions & 0 deletions solutions/attendance-chat-app/step-4/Code.gs
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);
}
6 changes: 6 additions & 0 deletions solutions/attendance-chat-app/step-4/appsscript.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"timeZone": "America/Los_Angeles",
"dependencies": {
},
"chat": {}
}
69 changes: 69 additions & 0 deletions solutions/attendance-chat-app/step-5/Code.gs
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);
}
6 changes: 6 additions & 0 deletions solutions/attendance-chat-app/step-5/appsscript.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"timeZone": "America/Los_Angeles",
"dependencies": {
},
"chat": {}
}
Loading

0 comments on commit 2e6ee8b

Please sign in to comment.