-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathKokoApp.ts
345 lines (315 loc) · 12.4 KB
/
KokoApp.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
import {
IAppAccessors,
IConfigurationExtend,
IConfigurationModify,
IEnvironmentRead,
IHttp,
ILogger,
IModify,
IPersistence,
IRead,
} from '@rocket.chat/apps-engine/definition/accessors';
import { ApiSecurity, ApiVisibility } from '@rocket.chat/apps-engine/definition/api';
import { App } from '@rocket.chat/apps-engine/definition/App';
import { IAppInfo } from '@rocket.chat/apps-engine/definition/metadata';
import { IRoom } from '@rocket.chat/apps-engine/definition/rooms';
import { StartupType } from '@rocket.chat/apps-engine/definition/scheduler';
import { ISetting } from '@rocket.chat/apps-engine/definition/settings';
import {
IUIKitInteractionHandler,
UIKitBlockInteractionContext,
UIKitViewSubmitInteractionContext,
} from '@rocket.chat/apps-engine/definition/uikit';
import { IUser } from '@rocket.chat/apps-engine/definition/users';
import { KokoOneOnOne } from './actions/KokoOneOnOne';
import { KokoPraise } from './actions/KokoPraise';
import { KokoQuestion } from './actions/KokoQuestion';
import { KokoValues } from './actions/KokoValues';
import { KokoWellness } from './actions/KokoWellness';
import { KokoCommand } from './commands/KokoCommand';
import { OneOnOneEndpoint } from './endpoints/OneOnOneEndpoint';
import { PraiseEndpoint } from './endpoints/PraiseEndpoint';
import { QuestionEndpoint } from './endpoints/QuestionEndpoint';
import { ValuesEndpoint } from './endpoints/ValuesEndpoint';
import { WellnessEndpoint } from './endpoints/WellnessEndpoint';
import { MembersCache } from './MembersCache';
import { learnMoreModal } from './modals/LearnMoreModal';
import { praiseModal } from './modals/PraiseModal';
import { questionModal } from './modals/QuestionModal';
import { valuesModal } from './modals/ValuesModal';
import { settings } from './settings';
export class KokoApp extends App implements IUIKitInteractionHandler {
/**
* The bot username alias
*/
public kokoName: string = 'Koko';
/**
* The bot avatar
*/
public kokoEmojiAvatar: string = ':gorilla:';
/**
* The room name where to get members from
*/
public kokoMembersRoomName: string;
/**
* The actual room object where to get members from
*/
public kokoMembersRoom: IRoom;
/**
* The room name where to post thanks messages to
*/
public kokoPostPraiseRoomName: string;
/**
* The actual room object where to post thanks messages to
*/
public kokoPostPraiseRoom: IRoom;
/**
* The room name where to post answers to
*/
public kokoPostAnswersRoomName: string;
/**
* The actual room object where to post answers to
*/
public kokoPostAnswersRoom: IRoom;
/**
* The bot username who sends the messages
*/
public botUsername: string;
/**
* The bot user sending messages
*/
public botUser: IUser;
/**
* The praise mechanism
*/
public kokoPraise: KokoPraise;
/**
* The question mechanism
*/
public kokoQuestion: KokoQuestion;
/**
* The values mechanism
*/
public kokoValues: KokoValues;
/**
* The random one on one mechanism
*/
public kokoOneOnOne: KokoOneOnOne;
/**
* The wellness mechanism
*/
public kokoWellness: KokoWellness;
/**
* Members cache
*/
// tslint:disable-next-line:variable-name
private _membersCache: MembersCache;
constructor(info: IAppInfo, logger: ILogger, accessors: IAppAccessors) {
super(info, logger, accessors);
}
/**
* Sends a praise or answers a question
*/
public async executeViewSubmitHandler(context: UIKitViewSubmitInteractionContext, read: IRead, http: IHttp, persistence: IPersistence, modify: IModify) {
const data = context.getInteractionData();
switch (data.view.id) {
case 'praise':
return this.kokoPraise.submit({ context, modify, read, persistence, http });
case 'question':
return this.kokoQuestion.submit({ context, modify, read, persistence });
case 'values':
return this.kokoValues.submit({ context, modify, read, persistence, http });
}
return {
success: true,
};
}
/**
* Implements the click of a button
*/
public async executeBlockActionHandler(context: UIKitBlockInteractionContext, read: IRead, http: IHttp, persistence: IPersistence, modify: IModify) {
const data = context.getInteractionData();
switch (data.actionId) {
case 'praise': {
const modal = await praiseModal({ app: this, data, read, modify });
return context.getInteractionResponder().openModalViewResponse(modal);
}
case 'question': {
const modal = await questionModal({ read, modify, data });
return context.getInteractionResponder().openModalViewResponse(modal);
}
case 'values': {
const modal = await valuesModal({ app: this, read, modify });
return context.getInteractionResponder().openModalViewResponse(modal);
}
case 'learnMore': {
const modal = await learnMoreModal({ app: this, read, modify, data });
return context.getInteractionResponder().openModalViewResponse(modal);
}
}
return {
success: true,
};
}
public async initialize(configurationExtend: IConfigurationExtend, environmentRead: IEnvironmentRead): Promise<void> {
this.kokoPraise = new KokoPraise(this);
this.kokoQuestion = new KokoQuestion(this);
this.kokoOneOnOne = new KokoOneOnOne(this);
this.kokoWellness = new KokoWellness(this);
this.kokoValues = new KokoValues(this);
await this.extendConfiguration(configurationExtend);
}
/**
* Loads the room where to get members from
* Loads the room where to post messages to
* Loads the user who'll be posting messages as the botUser
*
* @param environmentRead
* @param configModify
*/
public async onEnable(environmentRead: IEnvironmentRead, configModify: IConfigurationModify): Promise<boolean> {
this.kokoMembersRoomName = await environmentRead.getSettings().getValueById('Members_Room_Name');
if (this.kokoMembersRoomName) {
this.kokoMembersRoom = await this.getAccessors().reader.getRoomReader().getByName(this.kokoMembersRoomName) as IRoom;
}
this.kokoPostPraiseRoomName = await environmentRead.getSettings().getValueById('Post_Praise_Room_Name');
if (this.kokoPostPraiseRoomName) {
this.kokoPostPraiseRoom = await this.getAccessors().reader.getRoomReader().getByName(this.kokoPostPraiseRoomName) as IRoom;
}
this.kokoPostAnswersRoomName = await environmentRead.getSettings().getValueById('Post_Answers_Room_Name');
if (this.kokoPostAnswersRoomName) {
this.kokoPostAnswersRoom = await this.getAccessors().reader.getRoomReader().getByName(this.kokoPostAnswersRoomName) as IRoom;
}
this.botUsername = await environmentRead.getSettings().getValueById('Bot_Username');
if (this.botUsername) {
this.botUser = await this.getAccessors().reader.getUserReader().getByUsername(this.botUsername) as IUser;
}
return true;
}
/**
* Updates room ids for members and messages when settings are updated
*
* @param setting
* @param configModify
* @param read
* @param http
*/
public async onSettingUpdated(setting: ISetting, configModify: IConfigurationModify, read: IRead, http: IHttp): Promise<void> {
switch (setting.id) {
case 'Members_Room_Name':
this.kokoMembersRoomName = setting.value;
if (this.kokoMembersRoomName) {
this.kokoMembersRoom = await read.getRoomReader().getByName(this.kokoMembersRoomName) as IRoom;
}
break;
case 'Post_Praise_Room_Name':
this.kokoPostPraiseRoomName = setting.value;
if (this.kokoPostPraiseRoomName) {
this.kokoPostPraiseRoom = await read.getRoomReader().getByName(this.kokoPostPraiseRoomName) as IRoom;
}
break;
case 'Post_Answers_Room_Name':
this.kokoPostAnswersRoomName = setting.value;
if (this.kokoPostAnswersRoomName) {
this.kokoPostAnswersRoom = await read.getRoomReader().getByName(this.kokoPostAnswersRoomName) as IRoom;
}
break;
case 'Bot_User':
this.botUsername = setting.value;
if (this.botUsername) {
this.botUser = await read.getUserReader().getByUsername(this.botUsername) as IUser;
}
break;
}
}
/**
* Provides a setting for room id where to get members from
* Provides a setting for room id where to post messages to
* Provides an API for activating the praise mechanism
*
* @param configuration
*/
protected async extendConfiguration(configuration: IConfigurationExtend): Promise<void> {
// Settings
await Promise.all(settings.map((setting) => configuration.settings.provideSetting(setting)));
// API endpoints
await configuration.api.provideApi({
visibility: ApiVisibility.PRIVATE,
security: ApiSecurity.UNSECURE,
endpoints: [
new PraiseEndpoint(this),
new QuestionEndpoint(this),
new OneOnOneEndpoint(this),
new WellnessEndpoint(this),
new ValuesEndpoint(this),
],
});
// Slash Commands
await configuration.slashCommands.provideSlashCommand(new KokoCommand(this));
// Scheduler
configuration.scheduler.registerProcessors([
{
id: 'praise',
startupSetting: {
type: StartupType.RECURRING,
interval: '15 14 * * 1',
data: { appId: this.getID() },
},
processor: async (jobContext, read, modify, http, persistence) => {
await this.kokoPraise.run(read, modify, persistence, undefined, undefined, this.kokoPraise.sendScore ? 'praisers' : undefined);
this.kokoPraise.sendScore = !this.kokoPraise.sendScore;
},
},
{
id: 'question',
startupSetting: {
type: StartupType.RECURRING,
interval: '0 15 * * 1,4',
data: { appId: this.getID() },
},
processor: async (jobContext, read, modify, http, persistence) => {
await this.kokoQuestion.run(read, modify, persistence);
},
},
{
id: 'one-on-one',
startupSetting: {
type: StartupType.RECURRING,
interval: '0 17 * * 4',
data: { appId: this.getID() },
},
processor: async (jobContext, read, modify, http, persistence) => {
await this.kokoOneOnOne.run(read, modify, persistence);
},
},
{
id: 'welness',
startupSetting: {
type: StartupType.RECURRING,
interval: '0 13 * * 1,3,5',
data: { appId: this.getID() },
},
processor: async (jobContext, read, modify, http, persistence) => {
await this.kokoWellness.run(read, modify, persistence);
},
},
{
id: 'values',
startupSetting: {
type: StartupType.RECURRING,
interval: '0 16 * * 5',
data: { appId: this.getID() },
},
processor: async (jobContext, read, modify, http, persistence) => {
await this.kokoValues.run(read, modify, persistence);
},
},
]);
}
get membersCache(): MembersCache {
return this._membersCache;
}
set membersCache(memberCache: MembersCache) {
this._membersCache = memberCache;
}
}