-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
275 lines (262 loc) · 9.15 KB
/
app.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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
const SlackApp = require('@slack/bolt').App;
const MarketplaceService = require('./lib/MarketplaceService.js');
const SlackCommand = require('./lib/SlackCommand.js');
const SlackModal = require('./lib/SlackModal.js');
const logger = require('./lib/logger.js');
//initialize SlackApp using environment variables
const slackApp = new SlackApp({
token: process.env.SLACK_BOT_TOKEN,
signingSecret: process.env.SLACK_SIGNING_SECRET
});
//initialize MarketplaceService using environment variables
const mktSvc = new MarketplaceService(
process.env.MARKETPLACE_SERVICE_PROTOCOL,
process.env.MARKETPLACE_SERVICE_HOST
);
//process askami command
slackApp.command('/askami', async ({ command, ack, respond, body, client, say }) => {
const context = '[slackApp.command:askami]';
logger.debug(`${context}`);
//acknowledge request
await ack();
logger.debug(`${context} ${JSON.stringify(command,null,2)}`);
//capture usage for development insights
logger.info(`${context} @${command.user_name} (${command.user_id}) - "${command.text}"`);
try {
const result = await client.chat.postMessage({
channel: process.env.OBSERVABILITY_CHANNEL,
text: `@${command.user_name} (${command.user_id}) - "${command.text}"`
});
}
catch (error) {
logger.error(`${context} ${error}`);
}
try {
//parse and validate command
const cmd = new SlackCommand(command.text);
//branch on command type
switch (cmd.getCommandType()) {
case 'list':
//branch on command option
switch (cmd.getListOption()) {
case '-applications':
let applicationList = await mktSvc.getApplications(0);
await client.views.open({
trigger_id: body.trigger_id,
view: SlackModal.formatApplicationList(applicationList,0)
});
break;
case '-products':
let productList = await mktSvc.getProducts(0);
await client.views.open({
trigger_id: body.trigger_id,
view: SlackModal.formatProductList(productList,0)
});
break;
default:
throw new Error('parsing error');
}
break;
case 'search':
//branch on command option
switch (cmd.getListOption()) {
case '-addons':
let addonList = await mktSvc.getAddons(cmd.getCommandTerms(),0);
await client.views.open({
trigger_id: body.trigger_id,
view: SlackModal.formatAddonList(addonList,cmd.getCommandTerms(),0)
});
break;
case '-vendors':
let vendorList = await mktSvc.getVendors(cmd.getCommandTerms(),0);
await client.views.open({
trigger_id: body.trigger_id,
view: SlackModal.formatVendorList(vendorList,cmd.getCommandTerms(),0)
});
break;
default:
throw new Error('parsing error');
}
break;
case 'feedback':
await respond(`gotcha @${command.user_name}! i'll let my people know what you think...`);
break;
case 'help':
await respond(SlackCommand.help());
break;
default:
throw new Error('parsing error');
}
}
catch (error) {
logger.error(`${context} ${error}`);
await respond(SlackCommand.help(error));
}
});
//process addon detail request
slackApp.action('addonDetail', async ({ action, ack, body, client }) => {
const context = '[slackApp.action:addonDetail]';
logger.debug(`${context}`);
//acknowledge request
await ack();
//parse metadata
const key = action.value;
let i = await mktSvc.getAddon(key);
try {
await client.views.push({
trigger_id: body.trigger_id,
view: SlackModal.formatAddonDetail(i)
});
}
catch (error) {
logger.error(`${context} ${error}`);
}
});
//process application detail request
slackApp.action('applicationDetail', async ({ action, ack, body, client }) => {
const context = '[slackApp.action:applicationDetail]';
logger.debug(`${context}`);
//acknowledge request
await ack();
//parse metadata
const key = action.value;
let i = await mktSvc.getApplication(key);
try {
await client.views.push({
trigger_id: body.trigger_id,
view: SlackModal.formatApplicationDetail(i)
});
}
catch (error) {
logger.error(`${context} ${error}`);
}
});
//process product detail request
slackApp.action('productDetail', async ({ action, ack, body, client }) => {
const context = '[slackApp.action:productDetail]';
logger.debug(`${context}`);
//acknowledge request
await ack();
//parse metadata
const key = action.value;
let i = await mktSvc.getProduct(key);
try {
await client.views.push({
trigger_id: body.trigger_id,
view: SlackModal.formatProductDetail(i)
});
}
catch (error) {
logger.error(`${context} ${error}`);
}
});
//process vendor detail request
slackApp.action('vendorDetail', async ({ action, ack, body, client }) => {
const context = '[slackApp.action:vendorDetail]';
logger.debug(`${context}`);
//acknowledge request
await ack();
//parse metadata
const key = action.value;
let i = await mktSvc.getVendor(key);
try {
await client.views.push({
trigger_id: body.trigger_id,
view: SlackModal.formatVendorDetail(i)
});
}
catch (error) {
logger.error(`${context} ${error}`);
}
});
//process addon list paging request
slackApp.action({block_id: 'AddonListPagination'}, async ({ action, ack, body, client }) => {
const context = '[slackApp.action:AddonListPagination]';
logger.debug(`${context}`);
//acknowledge request
await ack();
//parse metadata
const metadata = JSON.parse(body.view.private_metadata);
const index = action.value;
let addonList = await mktSvc.getAddons(metadata.terms,index);
try {
await client.views.update({
view_id: body.view.id,
view: SlackModal.formatAddonList(addonList,metadata.terms,index)
});
}
catch (error) {
logger.error(`${context} ${error}`);
}
});
//process application list paging request
slackApp.action({block_id: 'ApplicationListPagination'}, async ({ action, ack, body, client }) => {
const context = '[slackApp.action:ApplicationListPagination]';
logger.debug(`${context}`);
//acknowledge request
await ack();
//parse metadata
const metadata = JSON.parse(body.view.private_metadata);
const index = action.value;
let applicationList = await mktSvc.getApplications(index);
try {
await client.views.update({
view_id: body.view.id,
view: SlackModal.formatApplicationList(applicationList,index)
});
}
catch (error) {
logger.error(`${context} ${error}`);
}
});
//process product list paging request
slackApp.action({block_id: 'ProductListPagination'}, async ({ action, ack, body, client }) => {
const context = '[slackApp.action:ProductListPagination]';
logger.debug(`${context}`);
//acknowledge request
await ack();
//parse metadata
const metadata = JSON.parse(body.view.private_metadata);
const index = action.value;
let productList = await mktSvc.getProducts(index);
try {
await client.views.update({
view_id: body.view.id,
view: SlackModal.formatProductList(productList,index)
});
}
catch (error) {
logger.error(`${context} ${error}`);
}
});
//process vendor list paging request
slackApp.action({block_id: 'VendorListPagination'}, async ({ action, ack, body, client }) => {
const context = '[slackApp.action:VendorListPagination]';
logger.debug(`${context}`);
//acknowledge request
await ack();
//parse metadata
const metadata = JSON.parse(body.view.private_metadata);
const index = action.value;
let vendorList = await mktSvc.getVendors(metadata.terms,index);
try {
await client.views.update({
view_id: body.view.id,
view: SlackModal.formatVendorList(vendorList,metadata.terms,index)
});
}
catch (error) {
logger.error(`${context} ${error}`);
}
});
//global error handler
slackApp.error((error) => {
const context = '[slackApp.error]';
logger.error(`${context} ${error}`);
});
// initialize and start
(async () => {
const context = '[slackApp]';
await slackApp.start(process.env.PORT || 3000);
logger.info(`${context} askami is alive!`);
})();