-
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.
Initial import of Chat stand-up app from COL300/Next24 (#467)
- Loading branch information
Showing
6 changed files
with
407 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,38 @@ | ||
# Chat API - Stand up with AI | ||
|
||
## Project Description | ||
|
||
Google Chat application that creates AI summaries of a consolidation Chat threads and posts them back within the top-level Chat message. Use case is using AI to streamline Stand up content within Google Chat. | ||
|
||
## Prerequisites | ||
|
||
* Google Cloud Project (aka Standard Cloud Project for Apps Script) with billing enabled | ||
|
||
## Set up your environment | ||
|
||
1. Create a Cloud Project | ||
1. Configure OAuth consent screen | ||
1. Enable the Admin SDK API | ||
1. Enable the Generative Language API | ||
1. Enable and configure the Google Chat API with the following values: | ||
1. App status: Live - available to users | ||
1. App name: “Standup” | ||
1. Avatar URL: “https://www.gstatic.com/images/branding/productlogos/chat_2020q4/v8/web-24dp/logo_chat_2020q4_color_2x_web_24dp.png” | ||
1. Description: “Standup App” | ||
1. Enable Interactive features: Disabled | ||
1. Create a Google Gemini API Key | ||
1. Navigate to https://aistudio.google.com/app/apikey | ||
1. Create API key for existing project from step 1 | ||
1. Copy the generated key | ||
1. Create and open a standalone Apps Script project | ||
1. From Project Settings, change project to GCP project number of Cloud Project from step 1 | ||
1. Add the following script properties: | ||
1. Set `API_KEY` with the API key previously generated as the value. | ||
1. Set `SPREADSHEET_ID` with the file ID of a blank spreadsheet. | ||
1. Set `SPACE_NAME` to the resource name of a Chat space (e.g. `spaces/AAAXYZ`) | ||
1. Enable the Google Chat advanced service | ||
1. Enable the AdminDirectory advanced service | ||
1. Add the project code to Apps Script | ||
1. Enable triggers: | ||
1. Add Time-driven to run function `standup` at the desired interval frequency (e.g. Week timer) | ||
1. Add Time-driven to run function `summarize` at the desired interval frequency (e.g. Hour timer) |
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,37 @@ | ||
{ | ||
"timeZone": "America/Los_Angeles", | ||
"exceptionLogging": "STACKDRIVER", | ||
"runtimeVersion": "V8", | ||
"dependencies": { | ||
"enabledAdvancedServices": [ | ||
{ | ||
"userSymbol": "Chat", | ||
"serviceId": "chat", | ||
"version": "v1" | ||
}, | ||
{ | ||
"userSymbol": "AdminDirectory", | ||
"serviceId": "admin", | ||
"version": "directory_v1" | ||
} | ||
] | ||
}, | ||
"webapp": { | ||
"executeAs": "USER_ACCESSING", | ||
"access": "DOMAIN" | ||
}, | ||
"oauthScopes": [ | ||
"https://www.googleapis.com/auth/chat.messages", | ||
"https://www.googleapis.com/auth/spreadsheets", | ||
"https://www.googleapis.com/auth/admin.directory.user.readonly", | ||
"https://www.googleapis.com/auth/script.external_request", | ||
"https://www.googleapis.com/auth/chat.spaces.create", | ||
"https://www.googleapis.com/auth/chat.spaces", | ||
"https://www.googleapis.com/auth/chat.spaces.readonly", | ||
"https://www.googleapis.com/auth/chat.spaces.create", | ||
"https://www.googleapis.com/auth/chat.delete", | ||
"https://www.googleapis.com/auth/chat.memberships", | ||
"https://www.googleapis.com/auth/chat.memberships.app", | ||
"https://www.googleapis.com/auth/userinfo.email" | ||
] | ||
} |
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,98 @@ | ||
/* | ||
Copyright 2024 Google LLC | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
https://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
/** @typedef {object} Message | ||
* @property {string} name | ||
* @property {string} text | ||
* @property {object} sender | ||
* @property {string} sender.type | ||
* @property {string} sender.name | ||
* @property {object[]} annotations | ||
* @property {number} annotations.startIndex | ||
* @property {string} annotations.type | ||
* @property {object} annotations.userMention | ||
* @property {number} annotations.length | ||
* @property {string} formattedText | ||
* @property {string} createTime | ||
* @property {string} argumentText | ||
* @property {object} thread | ||
* @property {string} thread.name | ||
* @property {object} space | ||
* @property {string} space.name | ||
*/ | ||
|
||
|
||
class DB { | ||
/** | ||
* params {String} spreadsheetId | ||
*/ | ||
constructor(spreadsheetId) { | ||
this.spreadsheetId = spreadsheetId; | ||
this.sheetName = "Messages"; | ||
|
||
} | ||
|
||
/** | ||
* @returns {SpreadsheetApp.Sheet} | ||
*/ | ||
get sheet() { | ||
const spreadsheet = SpreadsheetApp.openById(this.spreadsheetId); | ||
let sheet = spreadsheet.getSheetByName(this.sheetName); | ||
|
||
// create if it does not exist | ||
if (sheet == undefined) { | ||
sheet = spreadsheet.insertSheet(); | ||
sheet.setName(this.sheetName) | ||
} | ||
|
||
return sheet; | ||
} | ||
|
||
/** | ||
* @returns {Message|undefined} | ||
*/ | ||
get last() { | ||
const lastRow = this.sheet.getLastRow() | ||
if (lastRow === 0) return; | ||
return JSON.parse(this.sheet.getSheetValues(lastRow, 1, 1, 2)[0][1]); | ||
} | ||
|
||
|
||
/** | ||
* @params {Chat_v1.Chat.V1.Schema.Message} message | ||
*/ | ||
append(message) { | ||
this.sheet.appendRow([message.name, JSON.stringify(message, null, 2)]); | ||
} | ||
|
||
} | ||
|
||
|
||
/** | ||
* Test function for DB Object | ||
*/ | ||
function testDB() { | ||
const db = new DB(SPREADSHEET_ID); | ||
|
||
let thread = db.last; | ||
if (thread == undefined) return; | ||
console.log(thread) | ||
|
||
db.rowOffset = 1; | ||
thread = db.last; | ||
if (thread == undefined) return; | ||
console.log(thread) | ||
} |
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,41 @@ | ||
/* | ||
Copyright 2024 Google LLC | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
https://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
/** | ||
* Makes a simple content-only call to Gemini AI. | ||
* | ||
* @param {string} text Prompt to pass to Gemini API. | ||
* @param {string} API_KEY Developer API Key enabled to call Gemini. | ||
* | ||
* @return {string} Response from AI call. | ||
*/ | ||
function generateContent(text, API_KEY) { | ||
const url = `https://generativelanguage.googleapis.com/v1beta/models/gemini-pro:generateContent?key=${API_KEY}`; | ||
|
||
return JSON.parse(UrlFetchApp.fetch(url, { | ||
method: "POST", | ||
headers: { | ||
"content-type": "application/json" | ||
}, | ||
payload: JSON.stringify({ | ||
contents: [{ | ||
parts: [ | ||
{text} | ||
] | ||
}] | ||
}), | ||
}).getContentText()) | ||
} |
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,127 @@ | ||
/* | ||
Copyright 2024 Google LLC | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
https://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
/** TODO | ||
* Update global variables for your project settings | ||
* */ | ||
const API_KEY = PropertiesService.getScriptProperties().getProperty("API_KEY"); | ||
const SPREADSHEET_ID = PropertiesService.getScriptProperties().getProperty("SPREADSHEET_ID"); // e.g. "1O0IW7fW1QeFLa7tIrv_h7_PlSUTB6kd0miQO_sXo7p0" | ||
const SPACE_NAME = PropertiesService.getScriptProperties().getProperty("SPACE_NAME"); // e.g. "spaces/AAAABCa12Cc" | ||
|
||
const SUMMARY_HEADER = `\n\n*Gemini Generated Summary*\n\n`; | ||
|
||
|
||
/** | ||
* Sends the message to create new standup instance. | ||
* Called by trigger on interval of standup, e.g. Weekly | ||
* | ||
* @return {string} The thread name of the message sent. | ||
*/ | ||
function standup() { | ||
const db = new DB(SPREADSHEET_ID); | ||
|
||
const last = db.last; | ||
|
||
let text = `<users/all> Please share your weekly update here.\n\n*Source Code*: <https://script.google.com/corp/home/projects/${ScriptApp.getScriptId()}/edit|Apps Script>`; | ||
|
||
if (last) { | ||
text += `\n*Last Week*: <${linkToThread(last)}|View thread>`; | ||
} | ||
|
||
const message = Chat.Spaces.Messages.create({ | ||
text, | ||
}, PropertiesService.getScriptProperties().getProperty("spaceName") // Demo replaces => SPACE_NAME | ||
); | ||
|
||
db.append(message); | ||
|
||
console.log(`Thread Name: ${message.thread.name}`) | ||
return message.thread.name | ||
} | ||
|
||
/** | ||
* Uses AI to create a summary of messages for a stand up period. | ||
* Called by trigger on interval required to summarize, e.g. Hourly | ||
* | ||
* @return n/a | ||
*/ | ||
function summarize() { | ||
const db = new DB(SPREADSHEET_ID); | ||
const last = db.last; | ||
|
||
if (last == undefined) return; | ||
|
||
const filter = `thread.name=${last.thread.name}`; | ||
let { messages } = Chat.Spaces.Messages.list(PropertiesService.getScriptProperties().getProperty("spaceName"), { filter }); // Demo replaces => SPACE_NAME | ||
|
||
messages = (messages ?? []) | ||
.slice(1) | ||
.filter(message => message.slashCommand === undefined) | ||
|
||
if (messages.length === 0) { | ||
return; | ||
} | ||
|
||
const history = messages | ||
.map(({ sender, text }) => `${cachedGetSenderDisplayName(sender)}: ${text}`) | ||
.join('/n'); | ||
|
||
const response = generateContent( | ||
`Summarize the following weekly tasks and discussion per team member in a single concise sentence for each individual with an extra newline between members, but without using markdown or any special character except for newlines: ${history}`, | ||
API_KEY); | ||
const summary = response.candidates[0].content?.parts[0].text; | ||
|
||
if (summary == undefined) { | ||
return; | ||
} | ||
|
||
Chat.Spaces.Messages.update({ | ||
text: last.formattedText + SUMMARY_HEADER + summary.replace("**", "*") | ||
}, | ||
last.name, | ||
{ update_mask: "text" } | ||
); | ||
|
||
} | ||
|
||
/** | ||
* Gets the display name from AdminDirectory Services. | ||
* | ||
* @param {!Object} sender | ||
* @return {string} User name on success | 'Unknown' if not. | ||
*/ | ||
function getSenderDisplayName(sender) { | ||
try { | ||
const user = AdminDirectory.Users.get( | ||
sender.name.replace("users/", ""), | ||
{ projection: 'BASIC', viewType: 'domain_public' }); | ||
return user.name.displayName ?? user.name.fullName; | ||
} catch (e) { | ||
console.error("Unable to get display name"); | ||
return "Unknown" | ||
}; | ||
} | ||
|
||
const cachedGetSenderDisplayName = memoize(getSenderDisplayName); | ||
|
||
/** | ||
* @params {Chat_v1.Chat.V1.Schema.Message|Message} message | ||
* @returns {String} | ||
*/ | ||
function linkToThread(message) { | ||
// https://chat.google.com/room/SPACE/THREAD/ | ||
return `https://chat.google.com/room/${message.space.name.split("/").pop()}/${message.thread.name.split("/").pop()}`; | ||
} |
Oops, something went wrong.