26 lines
816 B
JavaScript
26 lines
816 B
JavaScript
const express = require('express');
|
|
const axios = require('axios');
|
|
const app = express();
|
|
|
|
const port = process.env.PORT || 3001;
|
|
const agentUrl = process.env.LLM_AGENT_URL;
|
|
const openWaUrl = process.env.OPEN_WA_URL;
|
|
|
|
app.use(express.json());
|
|
|
|
app.post('/webhook', async (req, res) => {
|
|
const { message, text, from } = req.body;
|
|
const incoming = message || text;
|
|
try {
|
|
if (!incoming) return res.sendStatus(200);
|
|
const agentRes = await axios.post(agentUrl, { message: incoming });
|
|
const reply = agentRes.data.reply || agentRes.data;
|
|
await axios.post(`${openWaUrl}/send-text`, { to: from, message: reply });
|
|
} catch (err) {
|
|
console.error('Error processing message', err.message);
|
|
}
|
|
res.sendStatus(200);
|
|
});
|
|
|
|
app.listen(port, () => console.log(`WhatsApp router listening on ${port}`));
|