-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathbot.js
More file actions
62 lines (57 loc) · 1.68 KB
/
bot.js
File metadata and controls
62 lines (57 loc) · 1.68 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
const _ = require('lodash');
const AWS = require('aws-sdk');
const botBuilder = require('claudia-bot-builder');
const slackTemplate = botBuilder.slackTemplate;
const lambda = new AWS.Lambda();
const slackDelayedReply = botBuilder.slackDelayedReply;
const command = require('./command');
const parser = require('yargs-parser');
const api = botBuilder(
(message, apiRequest) => {
const dave = _.get(message, 'originalRequest.user_name', 'dave');
const text = _.get(message, 'originalRequest.text', _.get(message, 'text'));
const baseCommand =
parser(text)['_'][0] && parser(text)['_'][0].toLowerCase();
return lambda
.invoke({
FunctionName: apiRequest.lambdaContext.functionName,
InvocationType: 'Event',
Payload: JSON.stringify({ slackEvent: message }),
Qualifier: apiRequest.lambdaContext.functionVersion
})
.promise()
.then(data => {
return {
response_type: 'in_channel'
};
})
.catch(err => {
console.error('initial response error', err);
return {
response_type: 'in_channel',
text: err.toString()
};
});
},
{ platforms: ['slackSlashCommand'] }
);
api.intercept(event => {
if (!event.slackEvent) {
return event;
}
const text = _.get(
event.slackEvent,
'slackEventoriginalRequest.text',
_.get(event.slackEvent, 'text')
);
return command(text, event)
.then(response => {
return slackDelayedReply(
event.slackEvent,
response || '[RESPONSE_ERROR]: Missing Response'
);
})
.catch(error => console.error('[SLACK_ERROR]', error))
.then(() => false);
});
module.exports = api;