-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
45 lines (36 loc) · 1.21 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
const express = require('express');
const bodyParser = require('body-parser');
const messageService = require('./lib/messaging');
const { handleUserRequest } = require('./helpers/request');
require('dotenv').config();
const app = express();
const port = process.env.PORT || 3000;
// Middlewares
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
// Webhook route for incoming WhatsApp messages
app.post('/itinerary', async (req, res) => {
const incomingMsg = req.body.Body;
const from = req.body.From;
try {
// Send immediate acknowledgment
await messageService.sendWhatsAppMessage(
from,
"We're generating your itinerary. This may take a few moments."
);
// Generate the itinerary
const itinerary = await handleUserRequest(incomingMsg);
// Send the itinerary via WhatsApp
await messageService.sendWhatsAppMessage(from, itinerary);
res
.status(200)
.json({ message: 'Itinerary generated and sent successfully', itinerary });
} catch (error) {
console.error('Error processing request:', error);
res.status(500).json({ error: 'Internal Server Error' });
}
});
// Start the server
app.listen(port, () => {
console.log(`Server is running on port ${port}`);
});