export default { async fetch(request, env) { const url = new URL(request.url); // Meta 验证 Webhook if (request.method === "GET" && url.pathname === "/webhook") { 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 === env.VERIFY_TOKEN && challenge ) { return new Response(challenge, { status: 200, headers: { "Content-Type": "text/plain; charset=UTF-8" } }); } return new Response("forbidden", { status: 403 }); } // 接收 WhatsApp 推送消息 if (request.method === "POST" && url.pathname === "/webhook") { try { const body = await request.json(); console.log("收到 WhatsApp Webhook:"); console.log(JSON.stringify(body)); return new Response("EVENT_RECEIVED", { status: 200 }); } catch (error) { console.error("Webhook JSON 解析失败:", error); return new Response("Bad Request", { status: 400 }); } } return new Response("Not Found", { status: 404 }); } };