-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
PublicCommands.ts
347 lines (335 loc) · 15 KB
/
PublicCommands.ts
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
const PublicCommands = () => {
function printTeam(teams?) {
teams = teams || Store.get(Key.TEAMS, {});
return teamName => {
const members = teams[teamName].map(member =>
member === teams[teamName][0] ? member + ' [Team-lead]' : member).join(' \n');
return `**${teamName}** \n${members}`;
};
}
return [
{
name: 'Restart',
adminsOnly: true,
match: commandText => commandText.match(/^\w+[,]? \/restart/i),
evaluate(commandMessage, webexClient) {
const match = this.match(commandMessage.text);
if (match) {
BotUtils.sendDefaultCommandHandledReply(this.name, commandMessage, webexClient);
Bot_Restart();
BotUtils.sendDefaultCommandDoneReply(commandMessage, webexClient);
}
},
},
{
name: 'Help',
hidden: true,
description: `@${MainConfig.botName} **help**`,
match: commandText => commandText.match(/^\w+[,]? help/i),
evaluate(commandMessage, webexClient) {
const match = this.match(commandMessage.text);
if (match) {
const user = commandMessage.personEmail;
const commands = PublicCommands()
.filter(command => !command.hidden &&
(command.adminsOnly ? BotUtils.isMessageFromAdmin(user, webexClient) : true))
.map(command => command.description).join(' \n');
webexClient.sendMessageToRoom(commandMessage.roomId, mention(user) + ', here’s what you can use: \n' + commands);
webexClient.sendMessageToRoom(commandMessage.roomId,
fmt('If you need any additional information, please visit %s.\n\n' +
'The help content is in development, so any contribution will be very appreciated. \n' +
'You can leave your comments there.', getHelpPage()));
}
},
},
{
name: 'Show config',
adminsOnly: true,
description: `@${MainConfig.botName} **show config**`,
match: commandText => commandText.match(/^\w+[,]? show config/i),
evaluate(commandMessage, webexClient) {
const match = this.match(commandMessage.text);
if (match) {
webexClient.sendMessageToRoom(commandMessage.roomId,
mention(commandMessage.personEmail) + ', here: \n' + printObject(MainConfig.get()));
}
},
},
{
name: 'Init Debug Room',
adminsOnly: true,
description: `@${MainConfig.botName} **init debug room**`,
match: commandText => commandText.match(/^\w+[,]? init debug room/i),
evaluate(commandMessage, webexClient) {
const config = MainConfig.get();
config.debugRoomId = commandMessage.roomId
if (!config.dailyStatusRoomId || config.dailyStatusRoomId == NotSpecified) {
config.dailyStatusRoomId = commandMessage.roomId
}
if (!config.operationsRoomId || config.operationsRoomId == NotSpecified) {
config.operationsRoomId = commandMessage.roomId
}
Store.set(Key.CONFIG, config)
webexClient.sendMessageToRoom(commandMessage.roomId, mention(commandMessage.personEmail) + ', done. Use `show config` to check.');
},
},
{
name: 'Init Daily Status Room',
adminsOnly: true,
description: `@${MainConfig.botName} **init status room**`,
match: commandText => commandText.match(/^\w+[,]? init status room/i),
evaluate(commandMessage, webexClient) {
const config = MainConfig.get();
config.dailyStatusRoomId = commandMessage.roomId
Store.set(Key.CONFIG, config)
webexClient.sendMessageToRoom(commandMessage.roomId, mention(commandMessage.personEmail) + ', done. Use `show config` to check.');
},
},
{
name: 'Init Operations Room',
adminsOnly: true,
description: `@${MainConfig.botName} **init operations room**`,
match: commandText => commandText.match(/^\w+[,]? init operations room/i),
evaluate(commandMessage, webexClient) {
const config = MainConfig.get();
config.operationsRoomId = commandMessage.roomId
Store.set(Key.CONFIG, config)
webexClient.sendMessageToRoom(commandMessage.roomId, mention(commandMessage.personEmail) + ', done. Use `show config` to check.');
},
},
{
name: 'Its Our Chat',
hidden: true,
adminsOnly: true,
description: `@${MainConfig.botName} **its our chat**`,
match: commandText => commandText.match(/^\w+[,]? its our chat/i),
evaluate(commandMessage, webexClient) {
const user = commandMessage.personEmail;
const teamName = BotUtils.getTeamName(user);
if (teamName) {
const team = Store.get(Key.TEAMS + '/' + teamName, []);
const commandFromTeamLead = team[0] === user;
if (commandFromTeamLead) {
Store.set(Key.TEAM_CHAT + '/' + teamName, commandMessage.roomId);
webexClient.sendMessageToRoom(commandMessage.roomId,
fmt('%s, ok. I’ll post notifications related to the **%s** team here.', mention(user), teamName));
} else {
webexClient.sendMessageToRoom(commandMessage.roomId,
mention(user) + ', only a team lead can set a team chat.');
}
} else {
webexClient.sendMessageToRoom(commandMessage.roomId,
mention(user) + ', I cannot find a team to which you belong.');
}
},
},
{
name: 'Set Team',
adminsOnly: true,
description: `@${MainConfig.botName} **set team DreamTeam1 [email protected] [email protected]...** - creates/updates a team.`,
match: commandText => commandText.match(/^\w+[,]? set team ([\w-]+)\s+((?:.|\s)+)*/i),
parseListedUsers(input) {
const separator = '&';
return input.trim().replace(/\s+/g, separator).split(separator);
},
evaluate(commandMessage, webexClient) {
const match = this.match(commandMessage.text);
if (match) {
if (match[2]) {
BotUtils.sendFastCommandReply(commandMessage, webexClient);
const listedUsers = this.parseListedUsers(match[2]);
const groupedUsers = BotUtils.groupDailyStatusUsersByListedUsers(listedUsers, webexClient);
const unknownUsers = groupedUsers.unknownUsers;
const knownUsers = groupedUsers.knownUsers;
if (unknownUsers.length) {
let failedToAdd = [];
unknownUsers.forEach(user => {
try {
webexClient.addPersonToRoom(user, MainConfig.dailyStatusRoomId, false);
debugMessage(`Added ${user} to Daily Status room`);
knownUsers.push(user);
} catch (e) {
debugMessage(`Failed to add ${user} to Daily Status room`);
failedToAdd.push(user);
}
});
if (failedToAdd.length) {
webexClient.sendMessageToRoom(commandMessage.roomId,
fmt(`%s, these users are not in the Daily Status room: %s. \nPlease, invite them and try again.`,
mention(commandMessage.personEmail), failedToAdd.join(', ')));
}
}
if (knownUsers.length) {
const teamName = match[1];
Store.set(Key.TEAMS + '/' + teamName, knownUsers);
const teamNames = Store.get(Key.TeamNames, []);
if (!teamNames.includes(teamName)) {
Store.set(Key.TeamNames, teamNames.concat([teamName]));
}
BotUtils.sendDefaultCommandDoneReply(commandMessage, webexClient);
}
} else {
webexClient.sendMessageToRoom(commandMessage.roomId,
mention(commandMessage.personEmail) + ', I didn\'t get it. You have to provide user emails after the team name.');
}
}
},
},
{
name: 'Show Teams',
description: `@${MainConfig.botName} **show teams** - shows all teams with members.`,
match: commandText => commandText.match(/^\w+[,]? show teams/i),
evaluate(commandMessage, webexClient) {
const match = this.match(commandMessage.text);
if (match) {
const teams = Store.get(Key.TEAMS, {});
const teamNames = Store.get(Key.TeamNames, []);
if (teamNames.length) {
const reply = fmt('%s, here:\n\n%s',
mention(commandMessage.personEmail),
teamNames.map(printTeam(teams)).join('\n\n'),
);
webexClient.sendMessageToRoom(commandMessage.roomId, reply);
} else {
webexClient.sendMessageToRoom(commandMessage.roomId,
mention(commandMessage.personEmail) + ', there are no teams added yet. Try to add using `set team` command.');
}
}
},
},
{
name: 'Remove Team',
adminsOnly: true,
description: `@${MainConfig.botName} **remove team DreamTeam1** - removes the team.`,
match: commandText => commandText.match(/^\w+[,]? remove team (\w+)/i),
evaluate(commandMessage, webexClient) {
const match = this.match(commandMessage.text);
if (match) {
const teamName = match[1];
const teamNames = Store.get(Key.TeamNames, []);
if (teamNames.includes(teamName)) {
Store.set(Key.TeamNames, without(teamNames, [teamName]));
Store.remove(`${Key.TEAMS}/${teamName}`);
BotUtils.sendDefaultCommandDoneReply(commandMessage, webexClient);
} else {
webexClient.sendMessageToRoom(commandMessage.roomId, fmt('%s, there is no such team as **%s**.',
mention(commandMessage.personEmail), teamName));
}
}
},
},
{
name: 'Check Statuses',
adminsOnly: true,
description: `@${MainConfig.botName} **check statuses** - collects statuses, sends the summary to the Daily Status room and via email.`,
match: commandText => commandText.match(/^\w+[,]? check statuses/i),
evaluate(commandMessage, webexClient) {
const match = this.match(commandMessage.text);
if (match) {
if (BotCache.get(BotCache.Key.STATUSES_COLLECTED)) {
webexClient.sendMessageToRoom(commandMessage.roomId,
mention(commandMessage.personEmail) + ', all statuses already collected. \n' +
'Please, use **send report** command to silently re-collect again for a final email.');
return;
}
BotUtils.sendDefaultCommandHandledReply(this.name, commandMessage, webexClient);
Bot_CheckStatuses({manualCall: true});
BotUtils.sendDefaultCommandDoneReply(commandMessage, webexClient);
}
},
},
{
name: 'Send Report Mail',
adminsOnly: true,
description: `@${MainConfig.botName} **send email** - compiles daily statuses into an email and sends it to leads/managers.`,
match: commandText => commandText.match(/^\w+[,]? send (report|email)/i),
evaluate(commandMessage, webexClient) {
const match = this.match(commandMessage.text);
if (match) {
BotUtils.sendDefaultCommandHandledReply(this.name, commandMessage, webexClient);
const usersInfo = Bot_CollectUsersInfo();
const dailyStatusesData = BotUtils.collectDailyStatusesData(usersInfo.workingToday, webexClient);
const statuses = dailyStatusesData.statuses;
const usersWithoutStatus = dailyStatusesData.userStatusIssues.noStatus;
if (usersWithoutStatus.length) {
const reply2 = fmt('%s, I’ll send a report, but there are still users without a status: %s',
mention(commandMessage.personEmail), usersWithoutStatus.join(', '));
webexClient.sendMessageToRoom(commandMessage.roomId, reply2);
Reporter.sendReportAsEmail(statuses, webexClient);
} else {
Reporter.sendReportAsEmail(statuses, webexClient);
}
BotUtils.sendDefaultCommandDoneReply(commandMessage, webexClient);
}
},
},
{
name: 'Show Holidays',
description: `@${MainConfig.botName} **show holidays** - shows configured holidays.`,
match: commandText => commandText.match(/^\w+[,]? show holidays/i),
evaluate(commandMessage, webexClient) {
const match = this.match(commandMessage.text);
if (match) {
const holidays = Store.get(Key.HOLIDAYS, []).join(' \n');
webexClient.sendMessageToRoom(commandMessage.roomId,
mention(commandMessage.personEmail) + ', here: \n' + holidays);
}
},
},
{
name: 'User of the day',
description: `@${MainConfig.botName} **who's ...** - randomly peeks someone.`,
match: commandText => commandText.match(/^\w+[,]? who['’]s (.+[^?])/i),
evaluate(commandMessage, webexClient) {
const match = this.match(commandMessage.text);
if (match) {
const users = BotUtils.listUsers(commandMessage.roomId, webexClient);
users.push(MainConfig.botEmail);
const user = getRandomFromList(users);
const message = '🎲 ' + (commandMessage.text.includes('of the day') ?
`${mention(user)} is **“${match[1]}”** of the day. Congrats!` :
`${mention(user)} is ${match[1]}. Congrats!`);
webexClient.sendMessageToRoom(commandMessage.roomId, message);
}
},
},
{
name: 'Shuffle team-members',
workDayOnly: true,
description: `@${MainConfig.botName} **shuffle** - returns team-members in random order.`,
match: commandText => commandText.match(/^\w+[,]? shuffle/i),
evaluate(commandMessage, webexClient) {
const match = this.match(commandMessage.text);
if (match) {
const teamName = BotUtils.getTeamNameByRoom(commandMessage.roomId);
let members;
if (teamName) {
members = Store.get(Key.TEAMS + '/' + teamName, {});
} else {
members = BotUtils.listUsers(commandMessage.roomId, webexClient);
}
const usersInfo = BotCache.get(Key.USERS_INFO) || Bot_CollectUsersInfo();
const excluded = usersInfo.notWorkingToday.map(u => u.user);
const shuffledMembers = shuffle(without(members, excluded));
const message = '👑 ' + (shuffledMembers.map(mention).join(' \n🎲 '));
const notAvailable = usersInfo.notWorkingToday.reduce((s, el) => members.find(m => m === el.user) ?
s + ` \n${BotUtils.getUserFullName(el.user, webexClient)} (${el.reason})` : s, '');
webexClient.sendMessageToRoom(commandMessage.roomId, notAvailable ? `${message}\n\n[N/A]${notAvailable}` : message);
}
},
},
{
name: 'Quick reply',
hidden: true,
description: `@${MainConfig.botName} ...`,
match: commandText => commandText.match(/^\w+[,]?/i),
evaluate(commandMessage, webexClient) {
const match = this.match(commandMessage.text);
if (match) {
const answer = mention(commandMessage.personEmail) + getRandomFromList(Constants.quickReplies);
webexClient.sendMessageToRoom(commandMessage.roomId, answer);
}
},
},
];
};