47 lines
1.8 KiB
TypeScript
47 lines
1.8 KiB
TypeScript
|
|
import { serve } from "https://deno.land/std@0.168.0/http/server.ts";
|
||
|
|
|
||
|
|
const META_VERIFY_TOKEN = Deno.env.get("META_VERIFY_TOKEN") || "foodsnap_meta_webhook_2026";
|
||
|
|
const META_ACCESS_TOKEN = Deno.env.get("META_ACCESS_TOKEN") || "";
|
||
|
|
const META_PHONE_NUMBER_ID = Deno.env.get("META_PHONE_NUMBER_ID") || "";
|
||
|
|
|
||
|
|
serve(async (req) => {
|
||
|
|
// ── 1. Rota Obrigatória: Verificação do Webhook (GET) ───────────
|
||
|
|
if (req.method === "GET") {
|
||
|
|
const url = new URL(req.url);
|
||
|
|
const mode = url.searchParams.get("hub.mode");
|
||
|
|
const token = url.searchParams.get("hub.verify_token");
|
||
|
|
const challenge = url.searchParams.get("hub.challenge");
|
||
|
|
|
||
|
|
if (mode === "subscribe" && token === META_VERIFY_TOKEN) {
|
||
|
|
console.log("[META-WH] Webhook verificado com sucesso!");
|
||
|
|
return new Response(challenge, { status: 200 });
|
||
|
|
} else {
|
||
|
|
console.error("[META-WH] Falha na verificação. Token inválido.");
|
||
|
|
return new Response("Forbidden", { status: 403 });
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
// ── 2. Rota Principal de Mensagem (POST) ────────────────────────
|
||
|
|
if (req.method === "POST") {
|
||
|
|
try {
|
||
|
|
const payload = await req.json();
|
||
|
|
|
||
|
|
// Validação básica do Payload da Graph API
|
||
|
|
if (payload.object !== "whatsapp_business_account") {
|
||
|
|
return new Response("Ignored", { status: 200 });
|
||
|
|
}
|
||
|
|
|
||
|
|
console.log("[META-WH] Payload Recebido:", JSON.stringify(payload, null, 2));
|
||
|
|
|
||
|
|
// Aqui implantaremos a leitura das mensagens, imagens e disparo do bot em breve.
|
||
|
|
|
||
|
|
return new Response("OK", { status: 200 });
|
||
|
|
} catch (err) {
|
||
|
|
console.error("[META-WH] Erro interno:", err);
|
||
|
|
return new Response("Internal Server Error", { status: 500 });
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
return new Response("Method Not Allowed", { status: 405 });
|
||
|
|
});
|