-
Notifications
You must be signed in to change notification settings - Fork 267
/
Copy pathChatApp.js
251 lines (231 loc) · 7.6 KB
/
ChatApp.js
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
/**
* 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.
*/
// This script contains the Google Chat-specific callback and utilities functions.
/**
* Responds to a MESSAGE event in Google Chat.
*
* Handles slash commands.
*
* @param {Object} event the event object from Google Chat
*/
function onMessage(event) {
if (event.message.slashCommand) {
return processSlashCommand(event);
}
return { text: "Available slash commands to manage issues are '/create' and '/close'." };
}
/**
* Responds to a CARD_CLICKED event in Google Chat.
*
* Handles action funtions.
*
* @param {Object} event the event object from Google Chat
*/
function onCardClick(event) {
// Issue creation dialog form submission
if (event.action.actionMethodName === "createIssue") {
return createIssue(event);
}
// Disable inclusivity help accessory widget
if (event.action.actionMethodName === "disableInclusivityHelp") {
disableInclusivityHelp(event.common.parameters.spaceId);
}
}
/**
* Responds to a MESSAGE event with a slash command in Google Chat.
*
* @param {Object} event the event object from Google Chat
*/
function processSlashCommand(event) {
if (event.message.slashCommand.commandId == CREATE_COMMAND_ID) {
// Opens the issue creation dialog.
return { actionResponse: {
type: "DIALOG",
dialogAction: { dialog: { body: { sections: [{
header: "Create",
widgets: [{ textInput: {
label: "Title",
name: "title"
}}, { textInput: {
label: "Description",
type: "MULTIPLE_LINE",
name: "description"
}}, { buttonList: { buttons: [{
text: "Create",
onClick: { action: {
function: "createIssue"
}}
}]}
}]
}]}}}
}};
}
if (event.message.slashCommand.commandId == CLOSE_COMMAND_ID && event.message.space.type !== "DM") {
// Closes the issue associated to the space.
const spaceId = event.message.space.name;
const resolution = event.message.argumentText;
const issue = JSON.parse(appProperties.getProperty(spaceId));
const history = exportSpaceHistory(spaceId);
const summary = summarizeSpace(history);
const docUrl = createReport(issue.title, issue.description, resolution, history, summary);
saveClosedIssue(spaceId, resolution, docUrl);
deleteSubscription(issue.subscriptionId);
return {
actionResponse: { type: "NEW_MESSAGE" },
text: `The issue is closed and its report generated:\n${docUrl}`
};
}
return { text: "The command isn't supported." };
}
/**
* Fetches and concatenates the 100 first space messages by using the Google Chat API.
*
* Messages with slash commands are filtered (app command invocations).
*
* @return {string} concatenate space messages in the format "Sender's name: Message"
*/
function exportSpaceHistory(spaceName) {
const messages = Chat.Spaces.Messages.list(spaceName, { 'pageSize': 100 }).messages;
// Returns results after fetching message sender display names.
let users = new Map();
return messages
.filter(message => message.slashCommand === undefined)
.map(message => `${getUserDisplayName(users, message.sender.name)}: ${message.text}`)
.join('\n');
}
/**
* Fetches a user's display name by using the Admin Directory API.
*
* A cache is used to only call the API once per user.
*
* @param {Map} cache the map containing users previously fetched
* @param {string} userId the user ID to fetch
* @return {string} the user's display name
*/
function getUserDisplayName(cache, userId) {
if (cache.has(userId)) {
return cache.get(userId);
}
let displayName = 'Unknown User';
try {
const user = AdminDirectory.Users.get(
userId.replace("users/", ""),
{ projection: 'BASIC', viewType: 'domain_public' });
displayName = user.name.displayName ? user.name.displayName : user.name.fullName;
} catch (e) {
// Ignores errors, uses 'Unknown User' by default.
}
cache.set(userId, displayName);
return displayName;
}
/**
* Handles create issue dialog form submissions in Google Chat.
*
* @param {Object} event the event object from Google Chat
*/
function createIssue(event) {
// Retrieves the form inputs.
const title = event.common.formInputs.title[""].stringInputs.value[0];
const description = event.common.formInputs.description[""].stringInputs.value[0];
const spaceUrl = createIssueSpace(title, description);
const subscriptionId = createSpaceSubscription(spaceUrl);
const createdIssue = saveCreatedIssue(title, description, spaceUrl, subscriptionId);
return {
actionResponse: { type: "NEW_MESSAGE" },
text: `The issue and its dedicated space were created:\n`
+ `https://mail.google.com/mail/u/0/#chat/space/${createdIssue.spaceId}`
};
}
/**
* Initializes a Google Chat space dedicated to a new issue.
*
* The app adds itself and sends a first message to the newly created space.
*
* @param {string} title the title of the issue
* @param {string} description the description of the isue
* @return {string} the ID of the new space
*/
function createIssueSpace(title, description) {
// Creates the space.
const spaceId = Chat.Spaces.setup({
space: {
displayName: title,
spaceType: "SPACE"
}
}).name;
// Adds itself to the space.
Chat.Spaces.Members.create({
member: {
name: "users/app",
type: "BOT"
}
}, spaceId);
// Sends a first message to the space
createAppMessageUsingChatService({ text: description}, spaceId);
return spaceId;
}
/**
* Handles app home requests in Google Chat.
*
* Displays the latest status of all issues.
*/
function onAppHome() {
// Generates one card section per issue.
var sections = [];
for (var issueKey in appProperties.getProperties()) {
const issue = JSON.parse(appProperties.getProperty(issueKey));
if (issue.spaceId) {
sections.push({
header: `${issue.status} - ${issue.title}`,
widgets: [{ textParagraph: {
text: `Description: ${issue.description}`
}}, { textParagraph: {
text: `Resolution: ${issue.resolution}`
}}, { buttonList: { buttons: [{
text: "Open space",
onClick: { openLink: {
url: `https://mail.google.com/mail/u/0/#chat/space/${issue.spaceId}`
}}
}, {
text: "Open report",
onClick: { openLink: {
url: issue.reportUrl !== "" ? issue.reportUrl : "docs.new"
}},
disabled: issue.reportUrl === ""
}]}
}]
});
}
}
return { action: { navigations: [{ push_card: {
sections: sections
}}]}};
}
/**
* Responds to a REMOVED_FROM_SPACE event in Google Chat.
*
* @param {Object} event the event object from Google Chat
*/
function onRemoveFromSpace(event) {
var issue = JSON.parse(appProperties.getProperty(event.space.name));
deleteSubscription(issue.subscriptionId);
}
/**
* Responds to a ADDED_TO_SPACE event in Google Chat.
*
* @param {Object} event the event object from Google Chat
*/
function onAddToSpace(event) {}