-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
67 lines (60 loc) · 2.05 KB
/
index.js
File metadata and controls
67 lines (60 loc) · 2.05 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
import axios from 'axios'
import { get } from 'lodash'
import config from './config.json'
import messages from './messages.json'
type SlackRequestObject = {
token: string,
team_id: string,
team_domain: string,
channel_id: string,
channel_name: string,
user_id: string,
user_name: string,
command: string,
text: string,
response_url: string
}
/**
* Entrypoint for AWS Lambda, this Lambda function receive Slack Slash Commands request from AWS SNS, and then send back a result response to Slack client.
* @param event is a AWS SNS Object that transformed from Slack's application/x-www-form-urlencoded request
* @param context has methods to let Lambda know when we're done, similar to express modules `response.send()`
*/
export function handler (event: any, context: any): void {
// Verify request came from SNS
const sns = get(event, ['Records', 0, 'Sns', 'Message'])
if (!sns) {
console.log(messages.SNS_MESSAGE_NOT_FOUND)
return context.fail(messages.SNS_MESSAGE_NOT_FOUND)
}
// Convert SNS message to JSON
const payload: SlackRequestObject = JSON.parse(sns)
console.log(payload)
// Verify request had a right Slack token
if (config.SLASH_COMMANDS_TOKEN_LIST.indexOf(payload.token) === -1) {
console.log(messages.UNAUTHORIZED_TOKEN)
return context.fail(messages.UNAUTHORIZED_TOKEN)
}
// Load module by Slack command
const { command } = require('./lib' + payload.command + '.js')
// Run command module and then send back a response to Slack client
command(payload.text)
.then(response => {
console.log(response)
axios.post(payload.response_url, response)
.then(done => context.succeed(done))
.catch(error => context.fail(error))
})
.catch(error => {
console.log(error)
axios.post(payload.response_url, {
'response_type': 'in_channel',
'attachments': [
{
'text': messages.ERROR_TEXT,
'color': 'danger'
}
]
}).then(done => context.succeed(done))
.catch(error => context.fail(error))
})
}