const whatsApp = client.defineHttpEndpoint({
id: "whatsapp",
source: "whatsapp.com",
icon: "whatsapp",
//only needed for strange APIs like WhatsApp which don't setup the webhook until you pass the test
respondWith: {
//we don't want to trigger Runs for the verification `GET` request
skipTriggeringRuns: true,
//we only want to respond to `GET` requests with a specific query parameter
filter: {
method: ["GET"],
query: {
"hub.mode": [{ $startsWith: "sub" }],
},
},
handler: async (request, verify) => {
const searchParams = new URL(request.url).searchParams;
if (searchParams.get("hub.verify_token") !== process.env.WHATSAPP_WEBHOOK_SECRET) {
return new Response("Unauthorized", { status: 401 });
}
return new Response(searchParams.get("hub.challenge") ?? "OK", { status: 200 });
},
},
verify: async (request) => {
//verification of the actual webhooks is easy
return await verifyRequestSignature({
request,
headerName: "x-hub-signature-256",
secret: process.env.WHATSAPP_APP_SECRET!,
algorithm: "sha256",
});
},
});
client.defineJob({
id: "http-whatsapp",
name: "HTTP WhatsApp",
version: "1.1.0",
enabled: true,
//create a Trigger from the HTTP endpoint above. You can provide an optional filter.
trigger: whatsApp.onRequest(),
run: async (request, io, ctx) => {
//note that when using HTTP endpoints, the first parameter is the request
//you need to get the body, usually it will be json so you do:
const body = await request.json();
await io.logger.info(`Body`, body);
},
});