/
bot.js
63 lines (55 loc) · 2.08 KB
/
bot.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
'use strict';
const promiseDelay = require('promise-delay');
const aws = require('aws-sdk');
const lambda = new aws.Lambda();
const botBuilder = require('claudia-bot-builder');
const slackDelayedReply = botBuilder.slackDelayedReply;
const ApiBuilder = require('claudia-api-builder');
const rota = require('./rota.js');
const api = botBuilder((message, apiRequest) => {
// Invoke the same Lambda function asynchronously, and do not wait for the response
// this allows the initial request to end within three seconds, as required by Slack
return new Promise((resolve, reject) => {
lambda.invoke({
FunctionName: apiRequest.lambdaContext.functionName,
InvocationType: 'Event',
Payload: JSON.stringify({
slackEvent: message // this will enable us to detect the event later and filter it
}),
Qualifier: apiRequest.lambdaContext.functionVersion
},
(err, done) => {
if (err) return reject(err);
resolve();
});
})
.then(() => { // empty response to acknowledge
console.log('sending empty acknowledgement');
return new ApiBuilder.ApiResponse('', { 'Content-Type': 'text/plain' }, 200);
})
.catch(() => {
return `There was an error running the command`
});
});
// this will be executed before the normal routing.
// we detect if the event has a flag set by line 18,
// and if so, avoid normal procesing, running a delayed response instead
api.intercept((event) => {
if (!event.slackEvent) // if this is a normal web request, let it run
return event;
console.log('in the intercept');
const message = event.slackEvent;
console.log('passing the message to the rota object');
let oRota = new rota(message);
let DDBRequestPromise = oRota.process();
return Promise.all([DDBRequestPromise]).then(function(values) {
console.log('promise.all return values:');
console.log(values);
return slackDelayedReply(message, {
text: `${values[0]}`,
response_type: 'in_channel'
})
})
.then(() => false); // prevent normal execution
});
module.exports = api;