-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
66 lines (56 loc) · 1.66 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
const {
handleMessage,
handlePostback,
handleRead,
handleReact,
} = require("./handle_messages");
const { initializeProfile } = require("./init");
const express = require("express");
const bodyParser = require("body-parser");
const app = express();
app.use(bodyParser.json());
const PORT = process.env.PORT || 1337;
const VERIFY_TOKEN = process.env.VERIFY_TOKEN || "TOKEN";
initializeProfile();
app.get("/", function (req, res) {
res.send("👀");
});
app.get("/webhook", (req, res) => {
let mode = req.query["hub.mode"];
let token = req.query["hub.verify_token"];
let challenge = req.query["hub.challenge"];
if (mode && token) {
if (mode === "subscribe" && token === VERIFY_TOKEN) {
console.log("WEBHOOK_VERIFIED");
res.status(200).send(challenge);
} else {
res.sendStatus(403);
}
} else {
res.sendStatus(403);
}
});
app.post("/webhook", (req, res) => {
let body = req.body;
if (body.object === "page") {
body.entry.forEach(function (entry) {
let webhook_event = entry.messaging[0];
let sender_psid = webhook_event.sender.id;
if (webhook_event.message) {
handleMessage(sender_psid, webhook_event.message);
} else if (webhook_event.postback) {
handlePostback(sender_psid, webhook_event.postback);
} else if (webhook_event.read) {
handleRead(sender_psid);
} else if (webhook_event.reaction) {
handleReact(sender_psid, webhook_event.reaction);
} else {
console.dir(webhook_event);
}
});
res.status(200).send("EVENT_RECEIVED");
} else {
res.sendStatus(404);
}
});
app.listen(PORT, () => console.log("webhook is listening"));