-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBotBridgeApp.ts
More file actions
121 lines (112 loc) · 4.69 KB
/
BotBridgeApp.ts
File metadata and controls
121 lines (112 loc) · 4.69 KB
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
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 {IMessage, IPostMessageSent} from '@rocket.chat/apps-engine/definition/messages';
import {IAppInfo} from '@rocket.chat/apps-engine/definition/metadata';
import {ISetting, SettingType} from '@rocket.chat/apps-engine/definition/settings';
import {
IUIKitInteractionHandler,
IUIKitResponse,
UIKitBlockInteractionContext,
} from '@rocket.chat/apps-engine/definition/uikit';
import {BackendRequestEndpoint} from './src/api/BackendRequestEndpoint';
import { handleButtonClick } from './src/handleButtonClick';
import { PostMessageSentHandler } from './src/handler/PostMessageSentHandler';
export class BotBridgeApp extends App implements IPostMessageSent, IUIKitInteractionHandler {
public bots: {} = {};
constructor(info: IAppInfo, logger: ILogger, accessors: IAppAccessors) {
super(info, logger, accessors);
}
public async onEnable(environment: IEnvironmentRead, configurationModify: IConfigurationModify): Promise<boolean> {
const controlledBotsList: string = await environment.getSettings().getValueById('controlled-bots');
await this.updateControlledBots(controlledBotsList);
return true;
}
public async updateControlledBots(botsList: string): Promise<void> {
const selfUser = await this.getAccessors().reader.getUserReader().getAppUser(this.getID());
this.bots = {};
if (selfUser) {
this.bots[selfUser.id] = selfUser;
}
botsList.split(/,\ ?/).map((botUsername) => {
this.getAccessors().reader.getUserReader().getByUsername(botUsername).then((user) => {
this.bots[user.id] = user;
});
});
}
public async onSettingUpdated(setting: ISetting, configurationModify: IConfigurationModify, read: IRead, http: IHttp): Promise<void> {
switch (setting.id) {
case 'controlled-bots': {
const controlledBotsList: string = setting.value;
this.updateControlledBots(controlledBotsList);
}
}
}
public async initialize(configurationExtend: IConfigurationExtend, environmentRead: IEnvironmentRead): Promise<void> {
const selfUser = await this.getAccessors().reader.getUserReader().getAppUser(this.getID());
console.debug('Initialize', selfUser);
if (selfUser) {
this.bots[selfUser.id] = selfUser;
}
await configurationExtend.settings.provideSetting({
id: 'backend-url',
required: true,
type: SettingType.STRING,
public: false,
packageValue: 'http://localhost:3228',
i18nLabel: 'Backend url',
});
await configurationExtend.settings.provideSetting({
id: 'controlled-bots',
required: false,
type: SettingType.STRING,
public: false,
packageValue: undefined,
i18nLabel: 'Controlled bots',
i18nDescription: 'comma-separated list of accounts usernames',
});
await configurationExtend.api.provideApi({
visibility: ApiVisibility.PRIVATE,
security: ApiSecurity.UNSECURE,
endpoints: [
new BackendRequestEndpoint(this),
],
});
}
public async executePostMessageSent(message: IMessage,
read: IRead,
http: IHttp,
persistence: IPersistence,
modify: IModify): Promise<void> {
const handler = new PostMessageSentHandler(this, message, read, http, persistence, modify);
await handler.run();
}
public async executeBlockActionHandler(
context: UIKitBlockInteractionContext,
read: IRead, http: IHttp, persistence: IPersistence, modify: IModify,
): Promise<IUIKitResponse> {
const data = context.getInteractionData();
switch (data.actionId) {
case 'push-bot-button': {
await handleButtonClick(context, read, http, persistence, modify);
const room = context.getInteractionData().room;
if (!room) {
return {success: false};
}
return {
success: true,
};
}
}
return {success: false};
}
}