const express = require("express");
const app = express();
// Middleware to parse JSON bodies
app.use(express.json());
app.post("/api/narada-webhook", async (req, res) => {
const { requestId, status, response } = req.body;
console.log(`Task ${requestId} completed with status: ${status}`); // Task req_abc123def456 completed with status: success
try {
if (status === "success") {
// Process successful response
console.log("Response:", response.text); // Response: Successfully extracted customer data from table
await processAutomationResult(requestId, response.text);
} else if (status === "error") {
// Handle error
console.error("Automation failed:", response.text); // Automation failed: Element not found on page
await handleAutomationError(requestId, response.text);
}
// Always respond with 200 to acknowledge receipt
res.status(200).json({ received: true });
} catch (error) {
console.error("Webhook processing error:", error); // Webhook processing error: Database connection failed
res.status(500).json({ error: "Internal server error" });
}
});
async function processAutomationResult(requestId, responseText) {
// Your business logic here
}
async function handleAutomationError(requestId, errorText) {
// Your error handling logic here
}
app.listen(3000, () => {
console.log("Webhook server listening on port 3000"); // Webhook server listening on port 3000
});