-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
49 lines (45 loc) · 1.13 KB
/
index.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
require("dotenv").config();
const {
handleUrlVerification,
handleEventCallback
} = require("./controllers/eventHandler");
/**
* @file index.js is the root file for this tech-news slack app
* @author Prakhil TP
* @see github <a href="https://github.com/prakhil-tp">GitHub Profile</a>
*/
/**
* Pure function to verify the slack requests
* @function verify
* @param {object} data
* @param {object} event
* @returns {boolean}
*/
function verify(data, event) {
return (
(!data.headers["X-Slack-Retry-Num"] &&
event.event &&
!event.event["subtype"]) ||
!event.event
);
}
/**
* lambda handler function
* @function techNews
* @param {object} - slack events
* @returns {object} - response depends to the event type
*/
module.exports.techNews = async (data) => {
const event = JSON.parse(data.body);
if (verify(data, event)) {
if (event.type === "url_verification")
return handleUrlVerification(event);
else if (event.type === "event_callback")
return handleEventCallback(event);
return {
statusCode: 400,
body: "Empty Request",
headers: { "X-Slack-No-Retry": 1 }
};
}
};