-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Main.ts
180 lines (156 loc) · 6.82 KB
/
Main.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
function Bot_Init() {
WebexClient().deleteAllWebHooks();
WebexClient().createWebHook();
}
function Bot_Restart() {
WebexClient().deleteAllWebHooks();
WebexClient().createWebHook();
checkTriggers();
debugMessage('Bot restarted.');
}
function Bot_Stop() {
WebexClient().deleteAllWebHooks();
debugMessage('Bot stopped.');
}
function Bot_AskStatuses(options) {
const manualCall = !options || options && options.manualCall;
const statusesAskedToday = BotCache.get(BotCache.Key.STATUSES_ASKED_TODAY);
if (manualCall || !statusesAskedToday) {
const usersInfo = Bot_CollectUsersInfo();
const webexClient = WebexClient();
const dailyStatusesData = BotUtils.collectDailyStatusesData(usersInfo.workingToday, webexClient);
const statuses = dailyStatusesData.statuses;
const usersWithoutStatus = dailyStatusesData.userStatusIssues.noStatus;
let errorOccurred = false;
if (Object.keys(statuses).length) {
webexClient.sendMessageToRoom(MainConfig.operationsRoomId, 'Some developers already have a status');
}
Object.keys(statuses).forEach(user => {
const dailyStatus = statuses[user];
webexClient.sendMessageToRoom(MainConfig.operationsRoomId, dailyStatus);
const firstName = BotUtils.getUserFirstName(user, webexClient);
const gotStatusReply = fmt(
'Hey, %s. You can send me a final version of your daily status before the moment they are collected. \n' +
'Use `show status` at any time to check it. \n' +
'Read the rules at %s.', firstName, getHelpPage());
try {
webexClient.sendMessageToPerson(user, gotStatusReply);
} catch (e) {
errorOccurred = true;
console.error('Failed to send "your status" message to ' + user);
}
});
usersWithoutStatus.forEach(user => {
const askStatusReply = fmt(
'Please, send your daily status before the moment they are collected. \n' +
'You can use `show status` at any time to check it. \n' +
'Read the rules at %s.', getHelpPage());
try {
webexClient.sendMessageToPerson(user, askStatusReply);
} catch (e) {
errorOccurred = true;
console.error('Failed to send "send me status" message to ' + user);
}
});
BotCache.put(BotCache.Key.STATUSES_ASKED_TODAY, true);
if (errorOccurred) {
debugMessage('Ask Statuses finished with errors.');
}
}
}
function Bot_CheckStatuses(options) {
const statusesCollected = BotCache.get(BotCache.Key.STATUSES_COLLECTED);
const manualCall = !options || options && options.manualCall;
const webexClient = WebexClient();
if (!statusesCollected && options && !options.manualCall) {
webexClient.sendMessageToRoom(MainConfig.operationsRoomId, 'Collecting daily statuses...');
}
if (manualCall || !statusesCollected) {
const usersInfo = Bot_CollectUsersInfo();
const usersWorkingToday = usersInfo.workingToday;
const dailyStatusesData = BotUtils.collectDailyStatusesData(usersWorkingToday, webexClient);
const statuses = dailyStatusesData.statuses;
const usersWithoutStatus = dailyStatusesData.userStatusIssues.noStatus;
Reporter.notifyHowManyMissed(usersWithoutStatus, usersWorkingToday.length, webexClient);
Reporter.sendReportAsChatMessage(dailyStatusesData, usersInfo, webexClient);
if (!usersWithoutStatus.length) {
Reporter.sendReportAsEmail(statuses, webexClient);
BotCache.put(BotCache.Key.STATUSES_COLLECTED, true);
} else {
usersWithoutStatus.forEach(user => {
const name = BotUtils.getUserFirstName(user, webexClient);
webexClient.sendMessageToPerson(user,
`${warning} ${name}, I cannot send the final report without your daily status. \n` +
'Please, send it as soon as possible!');
});
BotCache.put(BotCache.Key.USERS_WITHOUT_STATUS, usersWithoutStatus);
}
}
}
function Bot_CollectUsersInfo(optParams?) {
const webexClient = WebexClient();
const usersInfo = BotUtils.getTeamMembersInfo(webexClient);
BotCache.put(Key.USERS_INFO, usersInfo, 3600); // one hour
// console.log('Collected users info and put to Cache', usersInfo);
if (optParams && optParams.replyToRoom) {
Reporter.sendUsersInfoToRoom(usersInfo, optParams.replyToRoom, webexClient);
}
return usersInfo;
}
function Bot_SendLastCallToUpdateDailyStatus() {
const webexClient = WebexClient();
const usersInfo = Bot_CollectUsersInfo();
const dailyStatusesData = BotUtils.collectDailyStatusesData(usersInfo.workingToday, webexClient);
Object.keys(dailyStatusesData.statuses).forEach(function (user) {
try {
if (dailyStatusesData.userStatusIssues.lackOfDetails.includes(user)) {
webexClient.sendMessageToPerson(user,
`${warning} Your current daily status lacks details. **You must make it more descriptive.**`);
}
const dailyStatus = dailyStatusesData.statuses[user];
const haveStatusMessage = fmt(
'I’m gonna use this as your today\'s status:\n\n%s\n\n' +
'Hurry up if you want to update it.', dailyStatus);
webexClient.sendMessageToPerson(user, haveStatusMessage);
} catch (e) {
console.error('Failed to send "last call" message to ' + user);
}
});
dailyStatusesData.userStatusIssues.noStatus.forEach(user => {
try {
const firstName = BotUtils.getUserFirstName(user, webexClient);
const teamChat = BotUtils.getTeamChat(user);
const fullName = BotUtils.getUserFullName(user, webexClient);
let gotTeamChatError;
if (teamChat) {
try {
const troubleGif = GiphyClient.getRandomGifByTag(getRandomFromList(['alert', 'warning']));
const msg = `${warning} **${fullName}** has no daily status!\n\n<@all>`;
if (troubleGif) {
webexClient.sendAttachment({roomId: teamChat}, troubleGif, msg);
} else {
webexClient.sendMessageToRoom(teamChat, msg);
}
debugMessage('sent last call warning for ' + user + ' to team chat');
} catch (e) {
debugMessage('failed to send warning to team chat');
gotTeamChatError = true;
}
}
if (!teamChat || gotTeamChatError) {
const teamLead = BotUtils.getTeamLeadFor(user);
if (teamLead) {
webexClient.sendMessageToPerson(teamLead,
`${warning} **${fullName}** has no daily status!\n\n` +
'I can send such notifications into your team chat. Please, add me and there send the command `its our chat`.');
debugMessage('sent last call warning for ' + user + ' to team lead');
}
}
const noStatusMessage =
`${warning} ${firstName}, you haven’t left your daily status. \nThis is the last chance to do it. Hurry up!`;
webexClient.sendMessageToPerson(user, noStatusMessage);
} catch (e) {
debugMessage('Failed to send "hurry up" message to ' + user);
}
});
}