This commit is contained in:
bipproduction
2025-10-20 16:42:25 +08:00
parent 091bd2ed8d
commit 786fa55718

View File

@@ -1,14 +1,40 @@
import Elysia, { t } from "elysia"; import Elysia, { t } from "elysia";
const VERIFY_TOKEN = "token_yang_kamu_set_di_dashboard";
const WaHookRoute = new Elysia({ const WaHookRoute = new Elysia({
prefix: "/wa-hook", prefix: "/wa-hook",
tags: ["WhatsApp Hook"], tags: ["WhatsApp Hook"],
}) })
// ✅ Handle verifikasi Webhook (GET)
.get("/hook", ({ query, set }) => {
const mode = query["hub.mode"];
const token = query["hub.verify_token"];
const challenge = query["hub.challenge"];
.post("/hook", async (ctx) => { if (mode === "subscribe" && token === VERIFY_TOKEN) {
const { body } = ctx.body; set.status = 200;
return challenge; // WA butuh raw challenge string
}
set.status = 403;
return "Verification failed";
}, {
query: t.Object({
"hub.mode": t.Optional(t.String()),
"hub.verify_token": t.Optional(t.String()),
"hub.challenge": t.Optional(t.String())
}),
detail: {
summary: "Webhook Verification",
description: "Verifikasi dari WhatsApp API",
}
})
// ✅ Handle incoming message (POST)
.post("/hook", async ({ body }) => {
console.log("Incoming WhatsApp Webhook:", body);
console.log(body);
return { return {
success: true, success: true,
message: "WhatsApp Hook received" message: "WhatsApp Hook received"
@@ -16,9 +42,9 @@ const WaHookRoute = new Elysia({
}, { }, {
body: t.Any(), body: t.Any(),
detail: { detail: {
summary: "WhatsApp Hook", summary: "Receive WhatsApp Messages",
description: "WhatsApp Hook", description: "Menerima pesan dari WhatsApp Webhook"
}, }
}) });
export default WaHookRoute; export default WaHookRoute;