Skip to content

Commit 7693c9d

Browse files
committed
Address latest comments
1 parent fe0b2c5 commit 7693c9d

File tree

5 files changed

+24
-84
lines changed

5 files changed

+24
-84
lines changed

apps-script/incident-response/ChatApi.gs

-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
* @return {string} The resource name of the new space.
2626
*/
2727
function handleIncident(formData) {
28-
console.log(formData)
2928
const isAppCredentials = formData.isAppCredentials; // Get the isAppCredentials element
3029
if(isAppCredentials){
3130
return handleIncidentWithAppCredentials(formData)

apps-script/incident-response/ChatApiAppCredentials.gs

+12-18
Original file line numberDiff line numberDiff line change
@@ -27,30 +27,29 @@
2727
*/
2828
function handleIncidentWithAppCredentials(formData) {
2929
const users = formData.users.trim().length > 0 ? formData.users.split(',') : [];
30-
const service = _getService();
30+
const service = getService_();
3131
if (!service.hasAccess()) {
3232
console.error(service.getLastError());
3333
return;
3434
}
35-
const spaceName = _createChatSpaceWithAppCredentials(formData.title, service);
36-
_createHumanMembershipWithAppCredentials(spaceName, getUserEmail(),service);
35+
const spaceName = createChatSpaceWithAppCredentials_(formData.title, service);
36+
createHumanMembershipWithAppCredentials_(spaceName, getUserEmail(),service);
3737
for (const user of users ){
38-
_createHumanMembershipWithAppCredentials(spaceName, user, service);
38+
createHumanMembershipWithAppCredentials_(spaceName, user, service);
3939
}
40-
_createMessageWithAppCredentials(spaceName, formData.description, service);
40+
createMessageWithAppCredentials_(spaceName, formData.description, service);
4141
return spaceName;
4242
}
43-
4443
/**
4544
* Creates a chat space with application credentials.
4645
*
4746
* @param {string} displayName - The name of the chat space.
4847
* @param {object} service - The credentials of the service account.
4948
* @returns {string} The resource name of the new space.
50-
*/
51-
function _createChatSpaceWithAppCredentials(displayName, service) {
49+
*/
50+
function createChatSpaceWithAppCredentials_(displayName, service) {
5251
try {
53-
// for private apps, the alias can be used
52+
// For private apps, the alias can be used
5453
const my_customer_alias = "customers/my_customer"
5554
// Specify the space to create.
5655
const space = {
@@ -64,23 +63,21 @@ function _createChatSpaceWithAppCredentials(displayName, service) {
6463
{},
6564
// Authenticate with the service account token.
6665
{'Authorization': 'Bearer ' + service.getAccessToken()});
67-
// Log details about the created message.
68-
console.log(createdSpace);
6966
return createdSpace.name;
7067
} catch (err) {
7168
// TODO (developer) - Handle exception.
7269
console.log('Failed to create space with error %s', err.message);
7370
}
7471
}
75-
/*
72+
/*
7673
* Creates a chat message with application credentials.
7774
*
7875
* @param {string} spaceName - The resource name of the space.
7976
* @param {string} message - The text to be posted.
8077
* @param {object} service - The credentials of the service account.
8178
* @return {string} the resource name of the new space.
8279
*/
83-
function _createMessageWithAppCredentials(spaceName, message, service) {
80+
function createMessageWithAppCredentials_(spaceName, message, service) {
8481
try {
8582
const service = getService_();
8683
if (!service.hasAccess()) {
@@ -96,8 +93,6 @@ function _createMessageWithAppCredentials(spaceName, message, service) {
9693
// Authenticate with the service account token.
9794
{'Authorization': 'Bearer ' + service.getAccessToken()});
9895

99-
// Log details about the created message.
100-
console.log(result);
10196
} catch (err) {
10297
// TODO (developer) - Handle exception.
10398
console.log('Failed to create message with error %s', err.message);
@@ -110,7 +105,7 @@ function _createMessageWithAppCredentials(spaceName, message, service) {
110105
* @param {string} email - The email of the user to be added.
111106
* @param {object} service - The credentials of the service account.
112107
*/
113-
function _createHumanMembershipWithAppCredentials(spaceName, email, service){
108+
function createHumanMembershipWithAppCredentials_(spaceName, email, service){
114109
try{
115110
const service = getService_();
116111
if (!service.hasAccess()) {
@@ -130,7 +125,6 @@ function _createHumanMembershipWithAppCredentials(spaceName, email, service){
130125
{},
131126
{'Authorization': 'Bearer ' + service.getAccessToken()}
132127
);
133-
console.log(result)
134128
} catch (err){
135129
console.log('Failed to create membership with error %s', err.message)
136130
}
@@ -141,7 +135,7 @@ function _createHumanMembershipWithAppCredentials(spaceName, email, service){
141135
* Creates a service for the service account.
142136
* @return {object} - The credentials of the service account.
143137
*/
144-
function _getService() {
138+
function getService_() {
145139
return OAuth2.createService(APP_CREDENTIALS.client_email)
146140
.setTokenUrl('https://oauth2.googleapis.com/token')
147141
.setPrivateKey(APP_CREDENTIALS.private_key)

apps-script/incident-response/ChatApiHumanCredentials.gs

+6-6
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@
2727
*/
2828
function handleIncidentWithHumanCredentials(formData) {
2929
const users = formData.users.trim().length > 0 ? formData.users.split(',') : [];
30-
const spaceName = _setUpSpaceWithHumanCredentials(formData.title, users);
31-
_addAppToSpaceWithHumanCredentials(spaceName);
32-
_createMessageWithHumanCredentials(spaceName, formData.description);
30+
const spaceName = setUpSpaceWithHumanCredentials_(formData.title, users);
31+
addAppToSpaceWithHumanCredentials_(spaceName);
32+
createMessageWithHumanCredentials_(spaceName, formData.description);
3333
return spaceName;
3434
}
3535

@@ -38,7 +38,7 @@ function handleIncidentWithHumanCredentials(formData) {
3838
*
3939
* @return {string} the resource name of the new space.
4040
*/
41-
function _setUpSpaceWithHumanCredentials(displayName, users) {
41+
function setUpSpaceWithHumanCredentials_(displayName, users) {
4242
const memberships = users.map(email => ({
4343
member: {
4444
name: `users/${email}`,
@@ -63,7 +63,7 @@ function _setUpSpaceWithHumanCredentials(displayName, users) {
6363
*
6464
* @return {string} the resource name of the new membership.
6565
*/
66-
function _addAppToSpaceWithHumanCredentials(spaceName) {
66+
function addAppToSpaceWithHumanCredentials_(spaceName) {
6767
const request = {
6868
member: {
6969
name: "users/app",
@@ -80,7 +80,7 @@ function _addAppToSpaceWithHumanCredentials(spaceName) {
8080
*
8181
* @return {string} the resource name of the new message.
8282
*/
83-
function _createMessageWithHumanCredentials(spaceName, text) {
83+
function createMessageWithHumanCredentials_(spaceName, text) {
8484
const request = {
8585
text: text
8686
};

apps-script/incident-response/GeminiApi.gs

+3-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,9 @@ function summarizeChatHistory_(chatHistory) {
4949

5050
if (responseCode === 200) {
5151
var json = JSON.parse(responseBody);
52-
if (json.candidates && json.candidates.length > 0 && json.candidates[0].content && json.candidates[0].content.parts && json.candidates[0].content.parts.length > 0) {
52+
if (json.candidates && json.candidates.length > 0 &&
53+
json.candidates[0].content && json.candidates[0].content.parts &&
54+
json.candidates[0].content.parts.length > 0) {
5355
return json.candidates[0].content.parts[0].text;
5456
} else {
5557
return "Gemini API: Unexpected response structure.";

apps-script/incident-response/README.md

+3-58
Original file line numberDiff line numberDiff line change
@@ -138,64 +138,9 @@ Chat apps and Google Chat API requests](https://developers.devsite.corp.google.c
138138

139139
#### 4.2 Set up authentication as an app
140140

141-
This is available in [Developer Preview](/workspace/preview) only.
142-
143-
##### 4.2.1 Setup service account and key
144-
145-
1. In the Google Cloud console, go to **Menu* > **IAM & Admin** > **Service Accounts**
146-
1. Click **Create service account**.
147-
1. Fill in the service account details, then click **Create and continue**.
148-
1. Click **Continue**
149-
1. Click **Done**. Make a note of the email address for the service account.
150-
1. Create private key:
151-
1. In the Google Cloud console, go to **Menu** > **IAM & Admin** > **Service Accounts**.
152-
[Go to Service Accounts]({{console_url}}iam-admin/serviceaccounts){:
153-
class="button button-blue"
154-
target="console"}
155-
1. Select your service account.
156-
1. Click **Keys** > **Add key** > **Create new key**.
157-
1. Select **JSON**, then click **Create**.
158-
1. Click **Close**.
159-
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-
164-
##### 4.2.2 Receive administrator approval
165-
166-
To use an authorization scope that begins with
167-
`https://www.googleapis.com/auth/chat.app.*`, which are available as part of
168-
a Developer Preview, your Chat app must get a one-time
169-
[administrator approval](https://support.google.com/a?p=chat-app-auth).
170-
171-
To use the `https://www.googleapis.com/auth/chat.bot` authorization scope,
172-
no administrator approval is required.
173-
174-
To receive administrator approval, you must prepare your chat app's service account with the following
175-
information:
176-
177-
* A Google Workspace Marketplace-compatible OAuth client
178-
* App configuration in the Google Workspace Marketplace SDK.
179-
180-
1. In the Google Cloud console, go to **Menu** > **IAM & Admin** > **Service Accounts**.
181-
1. Click the service account you created for your Chat app.
182-
1. Click **Advanced Setting**.
183-
1. Click **Create Google Workspace Marketplace-compatible OAuth client**.
184-
1. Click **Continue**.
185-
186-
A confirmation message appears that says a Google Workspace Marketplace-compatible OAuth client has been created.
187-
After that, the Chap app should be configured in **Goolge Workspace Marketplace SDK**.
188-
189-
1. In the Google Cloud console, enable the Google Workspace Marketplace SDK.
190-
1. In the Google Cloud console, go to go to **Menu** > **APIs & Services** > **Enabled APIs & services** > **Google Workspace Marketplace SDK** > **App Configuration**.
191-
1. Complete the App Configuration page. How you configure your Chat app depends on who your intended audience is and other factors. For help completing the app configuration page, see Configure your app in the Google Workspace Marketplace SDK. For the purposes of this guide, enter the following information:
192-
1. Under **App visibility**, select **Private**
193-
1. Under **Installation settings**, select **Individual + admin install**.
194-
1. Under **App integrations**, select **Chat app**.
195-
1. Under **OAuth scopes**, enter all the scopes with ``https://www.googleapis.com/auth/chat.app.*``
196-
1. Under **Developer information**, enter your **Developer name**, **Developer website URL**, and **Developer email**.
197-
1. Click **Save draft**
198-
1. [Set up authorization Chat app](https://support.google.com/a?p=chat-app-auth).
141+
This is available in [Developer Preview](/workspace/preview) only.
142+
[The Google Chat app authentication]((https://developers.google.com/workspace/chat/authenticate-authorize-chat-app)) documentation provides details on how to set up authentication as an app.
143+
199144
### 5. Create an Apps Script project and connect it to the Google Cloud project
200145

201146
Before creating the Apps Script project, copy your Google Cloud project number.

0 commit comments

Comments
 (0)