const express = require("express");
const crypto = require("crypto");
const GOWA = "http://localhost:3000";
const AUTH = "Basic " + Buffer.from("admin:changeme").toString("base64");
const SECRET = "change-me";
const app = express();
app.post("/wa-hook", express.raw({ type: "application/json" }), async (req, res) => {
const sig = crypto.createHmac("sha256", SECRET).update(req.body).digest("hex");
if (req.get("X-Hub-Signature-256") !== `sha256=${sig}`) return res.sendStatus(401);
res.sendStatus(200);
const evt = JSON.parse(req.body);
if (evt.event !== "message" || evt.payload.is_from_me) return;
if (!/^hi\b/i.test(evt.payload.body || "")) return;
await fetch(`${GOWA}/send/message`, {
method: "POST",
headers: {
Authorization: AUTH,
"Content-Type": "application/json",
"X-Device-Id": evt.session_id, // reply from the slot that received it
},
body: JSON.stringify({
phone: evt.payload.chat_id,
message: "Hello! How can we help?",
reply_message_id: evt.payload.id,
}),
});
});
app.listen(8080);