Skip to content

Commit 75e4808

Browse files
committed
Address comments from PR review.
1 parent 985df46 commit 75e4808

11 files changed

+150
-115
lines changed

apps-script/incident-response/ChatApi.gs

+10-7
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,20 @@
1414
* limitations under the License.
1515
*/
1616
/**
17-
* Creates a space in Google Chat with the provided title and members, and posts an
18-
* initial message to it.
17+
* Handles an incident by creating a chat space, adding members, and posting a message.
18+
* Uses either application credentials or human credentials based on the formData.
1919
*
20-
* @param {Object} formData the data submitted by the user. It should contain the fields
21-
* title, description, and users.
22-
* @return {string} the resource name of the new space.
20+
* @param {Object} formData - The data submitted by the user. It should contain the fields:
21+
* - title: The display name of the chat space.
22+
* - description: The description of the incident.
23+
* - users: A comma-separated string of user emails to be added to the space.
24+
* - isAppCredentials: Boolean indicating whether to use application credentials.
25+
* @return {string} The resource name of the new space.
2326
*/
2427
function handleIncident(formData) {
2528
console.log(formData)
26-
const appCredentialsMode = formData.appCredentials; // Get the appCredentials element
27-
if(appCredentialsMode){
29+
const isAppCredentials = formData.isAppCredentials; // Get the isAppCredentials element
30+
if(isAppCredentials){
2831
return handleIncidentWithAppCredentials(formData)
2932
} else{
3033
return handleIncidentWithHumanCredentials(formData)

apps-script/incident-response/ChatApiAppCredentials.gs

+53-21
Original file line numberDiff line numberDiff line change
@@ -13,32 +13,48 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16+
// [START handle_incident_with_application_credentials]
1617

18+
/**
19+
* Handles an incident by creating a chat space, adding members, and posting a message.
20+
* All the actions are done using application credentials.
21+
*
22+
* @param {Object} formData - The data submitted by the user. It should contain the fields:
23+
* - title: The display name of the chat space.
24+
* - description: The description of the incident.
25+
* - users: A comma-separated string of user emails to be added to the space.
26+
* @return {string} The resource name of the new space.
27+
*/
1728
function handleIncidentWithAppCredentials(formData) {
1829
const users = formData.users.trim().length > 0 ? formData.users.split(',') : [];
19-
const spaceName = createChatSpaceWithAppCredentials(formData.title);
20-
createHumanMembershipWithAppCredentials(spaceName, getUserEmail());
30+
const service = _getService();
31+
if (!service.hasAccess()) {
32+
console.error(service.getLastError());
33+
return;
34+
}
35+
const spaceName = _createChatSpaceWithAppCredentials(formData.title, service);
36+
_createHumanMembershipWithAppCredentials(spaceName, getUserEmail(),service);
2137
for (const user of users ){
22-
createHumanMembershipWithAppCredentials(spaceName, user);
38+
_createHumanMembershipWithAppCredentials(spaceName, user, service);
2339
}
24-
createMessageWithAppCredentials(spaceName, formData.description);
40+
_createMessageWithAppCredentials(spaceName, formData.description, service);
2541
return spaceName;
2642
}
2743

28-
29-
30-
function createChatSpaceWithAppCredentials(spaceName) {
44+
/**
45+
* Creates a chat space with application credentials.
46+
*
47+
* @param {string} displayName - The name of the chat space.
48+
* @param {object} service - The credentials of the service account.
49+
* @returns {string} The resource name of the new space.
50+
*/
51+
function _createChatSpaceWithAppCredentials(displayName, service) {
3152
try {
32-
const service = getService_();
33-
if (!service.hasAccess()) {
34-
console.error(service.getLastError());
35-
return;
36-
}
3753
// for private apps, the alias can be used
3854
const my_customer_alias = "customers/my_customer"
3955
// Specify the space to create.
4056
const space = {
41-
displayName: spaceName,
57+
displayName: displayName,
4258
spaceType: 'SPACE',
4359
customer: my_customer_alias
4460
};
@@ -56,8 +72,15 @@ function createChatSpaceWithAppCredentials(spaceName) {
5672
console.log('Failed to create space with error %s', err.message);
5773
}
5874
}
59-
60-
function createMessageWithAppCredentials(spaceName, message) {
75+
/*
76+
* Creates a chat message with application credentials.
77+
*
78+
* @param {string} spaceName - The resource name of the space.
79+
* @param {string} message - The text to be posted.
80+
* @param {object} service - The credentials of the service account.
81+
* @return {string} the resource name of the new space.
82+
*/
83+
function _createMessageWithAppCredentials(spaceName, message, service) {
6184
try {
6285
const service = getService_();
6386
if (!service.hasAccess()) {
@@ -80,8 +103,14 @@ function createMessageWithAppCredentials(spaceName, message) {
80103
console.log('Failed to create message with error %s', err.message);
81104
}
82105
}
83-
84-
function createHumanMembershipWithAppCredentials(spaceName, email){
106+
/**
107+
* Creates a human membership in a chat space with application credentials.
108+
*
109+
* @param {string} spaceName - The resource name of the space.
110+
* @param {string} email - The email of the user to be added.
111+
* @param {object} service - The credentials of the service account.
112+
*/
113+
function _createHumanMembershipWithAppCredentials(spaceName, email, service){
85114
try{
86115
const service = getService_();
87116
if (!service.hasAccess()) {
@@ -90,7 +119,6 @@ function createHumanMembershipWithAppCredentials(spaceName, email){
90119
}
91120
const membership = {
92121
member: {
93-
// TODO(developer): Replace USER_NAME here
94122
name: 'users/'+email,
95123
// User type for the membership
96124
type: 'HUMAN'
@@ -109,13 +137,17 @@ function createHumanMembershipWithAppCredentials(spaceName, email){
109137

110138
}
111139

112-
113-
function getService_() {
140+
/*
141+
* Creates a service for the service account.
142+
* @return {object} - The credentials of the service account.
143+
*/
144+
function _getService() {
114145
return OAuth2.createService(APP_CREDENTIALS.client_email)
115146
.setTokenUrl('https://oauth2.googleapis.com/token')
116147
.setPrivateKey(APP_CREDENTIALS.private_key)
117148
.setIssuer(APP_CREDENTIALS.client_email)
118149
.setSubject(APP_CREDENTIALS.client_email)
119150
.setScope(APP_CREDENTIALS_SCOPES)
120151
.setPropertyStore(PropertiesService.getScriptProperties());
121-
}
152+
}
153+
// [END handle_incident_with_application_credentials]

apps-script/incident-response/ChatApiHumanCredentials.gs

+15-14
Original file line numberDiff line numberDiff line change
@@ -13,21 +13,23 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
// [START chat_incident_response_space_creator]
16+
// [START handle_incident_with_human_credentials]
1717

1818
/**
19-
* Creates a space in Google Chat with the provided title and members, and posts an
20-
* initial message to it.
19+
* Handles an incident by creating a chat space, adding members, and posting a message.
20+
* All the actions are done using human credentials.
2121
*
22-
* @param {Object} formData the data submitted by the user. It should contain the fields
23-
* title, description, and users.
24-
* @return {string} the resource name of the new space.
22+
* @param {Object} formData - The data submitted by the user. It should contain the fields:
23+
* - title: The display name of the chat space.
24+
* - description: The description of the incident.
25+
* - users: A comma-separated string of user emails to be added to the space.
26+
* @return {string} The resource name of the new space.
2527
*/
2628
function handleIncidentWithHumanCredentials(formData) {
2729
const users = formData.users.trim().length > 0 ? formData.users.split(',') : [];
28-
const spaceName = setUpSpaceWithHumanCredentials(formData.title, users);
29-
addAppToSpaceWithHumanCredentials(spaceName);
30-
createMessageWithHumanCredentials(spaceName, formData.description);
30+
const spaceName = _setUpSpaceWithHumanCredentials(formData.title, users);
31+
_addAppToSpaceWithHumanCredentials(spaceName);
32+
_createMessageWithHumanCredentials(spaceName, formData.description);
3133
return spaceName;
3234
}
3335

@@ -36,7 +38,7 @@ function handleIncidentWithHumanCredentials(formData) {
3638
*
3739
* @return {string} the resource name of the new space.
3840
*/
39-
function setUpSpaceWithHumanCredentials(displayName, users) {
41+
function _setUpSpaceWithHumanCredentials(displayName, users) {
4042
const memberships = users.map(email => ({
4143
member: {
4244
name: `users/${email}`,
@@ -61,7 +63,7 @@ function setUpSpaceWithHumanCredentials(displayName, users) {
6163
*
6264
* @return {string} the resource name of the new membership.
6365
*/
64-
function addAppToSpaceWithHumanCredentials(spaceName) {
66+
function _addAppToSpaceWithHumanCredentials(spaceName) {
6567
const request = {
6668
member: {
6769
name: "users/app",
@@ -78,13 +80,12 @@ function addAppToSpaceWithHumanCredentials(spaceName) {
7880
*
7981
* @return {string} the resource name of the new message.
8082
*/
81-
function createMessageWithHumanCredentials(spaceName, text) {
83+
function _createMessageWithHumanCredentials(spaceName, text) {
8284
const request = {
8385
text: text
8486
};
8587
// Call Chat API method spaces.messages.create
8688
const message = Chat.Spaces.Messages.create(request, spaceName);
8789
return message.name;
8890
}
89-
90-
// [END chat_incident_response_space_creator]
91+
// [END handle_incident_with_human_credentials]

apps-script/incident-response/ChatApp.gs

-1
Original file line numberDiff line numberDiff line change
@@ -185,5 +185,4 @@ function getUserDisplayName_(userMap, userName) {
185185
userMap.set(userName, displayName);
186186
return displayName;
187187
}
188-
189188
// [END chat_incident_response_app]

apps-script/incident-response/Consts.gs

+3-5
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,9 @@
1515
*/
1616
// [START chat_incident_response_consts]
1717

18-
const PROJECT_ID = PROJECT_ID';
18+
const PROJECT_ID = 'replace-with-your-project-id';
1919
const CLOSE_INCIDENT_COMMAND_ID = 3;
20-
const APP_CREDENTIALS = APP_CREDENTIALS;
20+
const APP_CREDENTIALS = 'replace-with-your-app-credentials';
2121
const APP_CREDENTIALS_SCOPES = 'https://www.googleapis.com/auth/chat.bot https://www.googleapis.com/auth/chat.app.memberships https://www.googleapis.com/auth/chat.app.spaces.create';
22-
const GEMINI_API_KEY = GEMINI_API_KEY;
23-
24-
22+
const GEMINI_API_KEY = 'replace-with-your-gemini-api-key';
2523
// [END chat_incident_response_consts]

apps-script/incident-response/DocsApi.gs

+1-2
Original file line numberDiff line numberDiff line change
@@ -36,5 +36,4 @@ function createDoc_(title, resolution, chatHistory, chatSummary) {
3636
body.appendParagraph(chatHistory);
3737
return doc.getUrl();
3838
}
39-
40-
// [END chat_incident_response_docs]
39+
// [END chat_incident_response_docs]

apps-script/incident-response/GeminiApi.gs

+1-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
// [START chat_incident_response_gemini]
1717

1818
/**
19-
* Summarizes a Chat conversation using the Vertex AI text prediction API.
19+
* Summarizes a Chat conversation using the Gemini API text prediction API.
2020
*
2121
* @param {string} chatHistory The Chat history that will be summarized.
2222
* @return {string} The content from the text prediction response.
@@ -62,5 +62,4 @@ function summarizeChatHistory_(chatHistory) {
6262
return 'Gemini API error, please check logs.'
6363
}
6464
}
65-
6665
// [END chat_incident_response_gemini]

apps-script/incident-response/Index.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ <h1>Incident Manager</h1>
4747
</p>
4848
<p>
4949
<label for="appCredentials">Use app credentials</label>
50-
<input type="checkbox" id="appCredentials" name="appCredentials" value="true">
50+
<input type="checkbox" id="isAppCredentials" name="isAppCredentials" value="true">
5151
</p>
5252
<p class="text-center">
5353
<input type="submit" value="CREATE CHAT SPACE" />

apps-script/incident-response/JavaScript.html

+49-49
Original file line numberDiff line numberDiff line change
@@ -15,52 +15,52 @@
1515
-->
1616
<!-- [START chat_incident_response_javascript] -->
1717
<script>
18-
var formDiv = document.getElementById('form');
19-
var outputDiv = document.getElementById('output');
20-
var clearDiv = document.getElementById('clear');
21-
22-
function handleFormSubmit(formObject) {
23-
event.preventDefault();
24-
outputDiv.innerHTML = 'Please wait while we create the space...';
25-
hide(formDiv);
26-
show(outputDiv);
27-
google.script.run
28-
.withSuccessHandler(updateOutput)
29-
.withFailureHandler(onFailure)
30-
.handleIncident(formObject);
31-
}
32-
33-
function updateOutput(response) {
34-
var spaceId = response.replace('spaces/', '');
35-
outputDiv.innerHTML =
36-
'<p>Space created!</p><p><a href="https://mail.google.com/chat/#chat/space/'
37-
+ spaceId
38-
+ '" target="_blank">Open space</a></p>';
39-
show(outputDiv);
40-
show(clearDiv);
41-
}
42-
43-
function onFailure(error) {
44-
outputDiv.innerHTML = 'ERROR: ' + error.message;
45-
outputDiv.classList.add('error');
46-
show(outputDiv);
47-
show(clearDiv);
48-
}
49-
50-
function onReset() {
51-
outputDiv.innerHTML = '';
52-
outputDiv.classList.remove('error');
53-
show(formDiv);
54-
hide(outputDiv);
55-
hide(clearDiv);
56-
}
57-
58-
function hide(element) {
59-
element.classList.add('hidden');
60-
}
61-
62-
function show(element) {
63-
element.classList.remove('hidden');
64-
}
65-
</script>
66-
<!-- [END chat_incident_response_javascript] -->
18+
var formDiv = document.getElementById('form');
19+
var outputDiv = document.getElementById('output');
20+
var clearDiv = document.getElementById('clear');
21+
22+
function handleFormSubmit(formObject) {
23+
event.preventDefault();
24+
outputDiv.innerHTML = 'Please wait while we create the space...';
25+
hide(formDiv);
26+
show(outputDiv);
27+
google.script.run
28+
.withSuccessHandler(updateOutput)
29+
.withFailureHandler(onFailure)
30+
.handleIncident(formObject);
31+
}
32+
33+
function updateOutput(response) {
34+
var spaceId = response.replace('spaces/', '');
35+
outputDiv.innerHTML =
36+
'<p>Space created!</p><p><a href="https://mail.google.com/chat/#chat/space/'
37+
+ spaceId
38+
+ '" target="_blank">Open space</a></p>';
39+
show(outputDiv);
40+
show(clearDiv);
41+
}
42+
43+
function onFailure(error) {
44+
outputDiv.innerHTML = 'ERROR: ' + error.message;
45+
outputDiv.classList.add('error');
46+
show(outputDiv);
47+
show(clearDiv);
48+
}
49+
50+
function onReset() {
51+
outputDiv.innerHTML = '';
52+
outputDiv.classList.remove('error');
53+
show(formDiv);
54+
hide(outputDiv);
55+
hide(clearDiv);
56+
}
57+
58+
function hide(element) {
59+
element.classList.add('hidden');
60+
}
61+
62+
function show(element) {
63+
element.classList.remove('hidden');
64+
}
65+
</script>
66+
<!-- [END chat_incident_response_javascript] -->

0 commit comments

Comments
 (0)