Skip to content

Commit 0359241

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

10 files changed

+101
-66
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/README.md

+12-13
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ the following APIs:
9191
### 4. Create API key
9292

9393
To use Gemini API, you need to have an API key created:
94-
1. In the Google Cloud ocnsole, click **Menu** > **APIs & Services** > **Credentials**
94+
1. In the Google Cloud console, click **Menu** > **APIs & Services** > **Credentials**
9595
1. Click **CREATE CRENDETIALAS**, then select **API key**.
9696
1. Create the key
9797

@@ -110,7 +110,7 @@ replace placeholder information with real information.
110110
To learn more about authentication in Google Chat, see [Authenticate and authorize
111111
Chat apps and Google Chat API requests](https://developers.devsite.corp.google.com/chat/api/guides/auth).
112112

113-
#### 4.1 Authenticate as a user configuration
113+
#### 4.1 Set up authentication as a user
114114

115115
1. In the Google Cloud console, go to **Menu** > **APIs & Services** >
116116
[**OAuth consent screen**](https://console.cloud.google.com/apis/credentials/consent).
@@ -136,11 +136,11 @@ Chat apps and Google Chat API requests](https://developers.devsite.corp.google.c
136136
1. Click **Save and Continue**.
137137
1. Review the app registration summary, then click **Back to Dashboard**.
138138

139-
#### 4.2 Authenticate as a app configuration
139+
#### 4.2 Set up authentication as an app
140140

141141
This is available in [Developer Preview](/workspace/preview) only.
142142

143-
##### 4.2.1 setup service account and key
143+
##### 4.2.1 Setup service account and key
144144

145145
1. In the Google Cloud console, go to **Menu* > **IAM & Admin** > **Service Accounts**
146146
1. Click **Create service account**.
@@ -157,6 +157,10 @@ This is available in [Developer Preview](/workspace/preview) only.
157157
1. Select **JSON**, then click **Create**.
158158
1. Click **Close**.
159159

160+
**Warning**: This example uses an exported service account key for simplicity's sake. Exporting a private key is not recommended
161+
in production because it shouldn't be stored in an insecure location, such as source control. To learn more about secure service account
162+
implementations and best practices, see Choose when to use service accounts.
163+
160164
##### 4.2.2 Receive administrator approval
161165

162166
To use an authorization scope that begins with
@@ -192,12 +196,6 @@ After that, the Chap app should be configured in **Goolge Workspace Marketplace
192196
1. Under **Developer information**, enter your **Developer name**, **Developer website URL**, and **Developer email**.
193197
1. Click **Save draft**
194198
1. [Set up authorization Chat app](https://support.google.com/a?p=chat-app-auth).
195-
196-
**Warning**: This example uses an exported service account key for simplicity's sake. Exporting a private key is not recommended
197-
in production because it shouldn't be stored in an insecure location, such as source control. To learn more about secure service account
198-
implementations and best practices, see Choose when to use service accounts.
199-
200-
201199
### 5. Create an Apps Script project and connect it to the Google Cloud project
202200

203201
Before creating the Apps Script project, copy your Google Cloud project number.
@@ -293,6 +291,7 @@ The Chat app is ready to respond to messages.
293291

294292
Navigate to the **Web app URL** from the Apps Script deployment to test your app.
295293

296-
#### 8.1 app auth mode
297-
If the checkbox is selected, the Chat app will use app authentication to create the space, add members and post the message to the space.
298-
If the checkbox is not selected, the Chat app will use human credentials instead and all the actions will be done on behalf of the user.
294+
#### 8.1. Select the authentication mode
295+
296+
If the checkbox 'Use app credentials' is selected, the Chat app will use app authentication to create the space, add members and post the message to the space.
297+
If the checkbox is not selected, the Chat app will use user credentials instead and all the actions will be done on behalf of the user.

apps-script/incident-response/appsscript.json

+5
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@
88
"userSymbol": "Chat",
99
"serviceId": "chat",
1010
"version": "v1"
11+
},
12+
{
13+
"userSymbol": "AdminDirectory",
14+
"version": "directory_v1",
15+
"serviceId": "admin"
1116
}
1217
],
1318
"libraries": [

0 commit comments

Comments
 (0)