-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
51 lines (39 loc) · 1.49 KB
/
app.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
"use strict";
const token = process.env.WHATSAPP_TOKEN;
// Imports dependencies and set up http server
require('dotenv').config()
const express = require("express");
const body_parser = require("body-parser");
const session = require('./session'); // Import session module
const service = require("./service")
const app = express() // creates express http server
app.use(body_parser.json());
app.use(session);
// Sets server port and logs message on success
app.listen(process.env.PORT || 1337, () => console.log("webhook is listening port:",process.env.PORT || 1337));
app.post("/webhook", service.webhook)
app.post("/sendMessage", service.sendMessage);
app.get("/webhook", (req, res) => {
/**
* UPDATE YOUR VERIFY TOKEN
*This will be the Verify Token value when you set up webhook
**/
const verify_token = process.env.VERIFY_TOKEN;
// Parse params from the webhook verification request
let mode = req.query["hub.mode"];
let verifyToken = req.query["hub.verify_token"];
let challenge = req.query["hub.challenge"];
// Check if a token and mode were sent
if (mode && verifyToken) {
// Check the mode and token sent are correct
if (mode === "subscribe" && verifyToken === verify_token) {
// Respond with 200 OK and challenge token from the request
console.log("WEBHOOK_VERIFIED");
res.status(200).send(challenge);
} else {
// Responds with '403 Forbidden' if verify tokens do not match
res.sendStatus(403);
}
}
});
module.exports = { app }