-
Notifications
You must be signed in to change notification settings - Fork 267
/
Copy pathChatSpaceCreator.gs
90 lines (85 loc) · 2.54 KB
/
ChatSpaceCreator.gs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/**
* Copyright 2023 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
*
* http://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.
*/
// [START chat_incident_response_space_creator]
/**
* Creates a space in Google Chat with the provided title and members, and posts an
* initial message to it.
*
* @param {Object} formData the data submitted by the user. It should contain the fields
* title, description, and users.
* @return {string} the resource name of the new space.
*/
function createChatSpace(formData) {
const users = formData.users.trim().length > 0 ? formData.users.split(',') : [];
const spaceName = setUpSpace_(formData.title, users);
addAppToSpace_(spaceName);
createMessage_(spaceName, formData.description);
return spaceName;
}
/**
* Creates a space in Google Chat with the provided display name and members.
*
* @return {string} the resource name of the new space.
*/
function setUpSpace_(displayName, users) {
const memberships = users.map(email => ({
member: {
name: `users/${email}`,
type: "HUMAN"
}
}));
const request = {
space: {
displayName: displayName,
spaceType: "SPACE",
externalUserAllowed: true
},
memberships: memberships
};
// Call Chat API method spaces.setup
const space = Chat.Spaces.setup(request);
return space.name;
}
/**
* Adds this Chat app to the space.
*
* @return {string} the resource name of the new membership.
*/
function addAppToSpace_(spaceName) {
const request = {
member: {
name: "users/app",
type: "BOT"
}
};
// Call Chat API method spaces.members.create
const membership = Chat.Spaces.Members.create(request, spaceName);
return membership.name;
}
/**
* Posts a text message to the space on behalf of the user.
*
* @return {string} the resource name of the new message.
*/
function createMessage_(spaceName, text) {
const request = {
text: text
};
// Call Chat API method spaces.messages.create
const message = Chat.Spaces.Messages.create(request, spaceName);
return message.name;
}
// [END chat_incident_response_space_creator]