-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.js
More file actions
37 lines (30 loc) · 977 Bytes
/
Copy pathapp.js
File metadata and controls
37 lines (30 loc) · 977 Bytes
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
// Import Express.js
const express = require('express');
// Create an Express app
const app = express();
// Middleware to parse JSON bodies
app.use(express.json());
// Set port and verify_token
const port = process.env.PORT || 3000;
const verifyToken = process.env.VERIFY_TOKEN;
// Route for GET requests
app.get('/', (req, res) => {
const { 'hub.mode': mode, 'hub.challenge': challenge, 'hub.verify_token': token } = req.query;
if (mode === 'subscribe' && token === verifyToken) {
console.log('WEBHOOK VERIFIED');
res.status(200).send(challenge);
} else {
res.status(403).end();
}
});
// Route for POST requests
app.post('/', (req, res) => {
const timestamp = new Date().toISOString().replace('T', ' ').slice(0, 19);
console.log(`\n\nWebhook received ${timestamp}\n`);
console.log(JSON.stringify(req.body, null, 2));
res.status(200).end();
});
// Start the server
app.listen(port, () => {
console.log(`\nListening on port ${port}\n`);
});