-
Notifications
You must be signed in to change notification settings - Fork 235
/
Copy pathbot.js
65 lines (55 loc) · 1.95 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
64
65
'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 api = botBuilder((message, apiRequest) => {
const seconds = parseInt(message.text, 10);
if (Number.isInteger(seconds) && seconds > 0 && seconds < 61) {
// Invoke the same Lambda function asynchronously, and do not wait for the response
// this allows the initial request to end within three seconds, as requiured 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(() => {
return { // the initial response
text: `Ok, I'll ping you in ${seconds}s.`,
response_type: 'in_channel'
}
})
.catch(() => {
return `Could not setup timer :(`
});
} else {
return `Number of seconds needs to be between 1 and 60`
}
});
// this will be executed before the normal routing.
// we detect if the event has a flag set by line 21,
// 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;
const message = event.slackEvent;
const seconds = parseInt(message.text, 10);
return promiseDelay(seconds * 1000)
.then(() => {
return slackDelayedReply(message, {
text: `${seconds} seconds passed.`,
response_type: 'in_channel'
})
})
.then(() => false); // prevent normal execution
});
module.exports = api;