tambahan
This commit is contained in:
@@ -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;
|
||||||
|
|||||||
Reference in New Issue
Block a user