-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.ts
60 lines (51 loc) · 1.2 KB
/
index.ts
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
const VERIFICATION_TOKEN = "";
const serverless = require("serverless-http");
const express = require("express");
const bodyParser = require("body-parser");
const app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: true
}));
app.set("port", 8080);
app.listen(app.get("port"));
app.get("/", (req, res) => {
res.send("Hello World!");
});
function verify(data, callback) {
if (data.token === VERIFICATION_TOKEN) {
callback(data.challenge);
} else {
callback("verification failed");
}
}
function check_event(body, callback) {
// Catch events here
if (body.event.type === "member_joined_channel") {
console.log("member_joined_channel");
// DO actions when user joined.
}
callback(null);
}
app.post("/", (req, res) => {
switch (req.body.type) {
case "url_verification":
verify(req.body, (challange) => {
if (challange) {
res.json({
"challange": `${challange}`
});
res.send();
}
});
break;
case "event_callback":
check_event(req.body, () => {
res.send();
});
break;
default:
res.send();
}
});
module.exports.handler = serverless(app);