-
Notifications
You must be signed in to change notification settings - Fork 17
/
whatsapp-http-endpoint.ts
62 lines (58 loc) · 1.65 KB
/
whatsapp-http-endpoint.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import { TriggerClient, verifyRequestSignature } from "@trigger.dev/sdk";
// hide-code
const client = new TriggerClient({ id: "api-reference" });
// end-hide-code
// WhatsApp
const whatsApp = client.defineHttpEndpoint({
id: "whatsapp",
source: "whatsapp.com",
icon: "whatsapp",
// This is only needed for certain APIs like WhatsApp which don't setup the webhook until you pass the test
respondWith: {
// Don't trigger runs if they match this filter
skipTriggeringRuns: true,
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) => {
return await verifyRequestSignature({
request,
headerName: "x-hub-signature-256",
secret: process.env.WHATSAPP_APP_SECRET!,
algorithm: "sha256",
xp,
});
},
});
client.defineJob({
id: "http-whatsapp",
name: "HTTP WhatsApp",
version: "1.1.0",
enabled: true,
trigger: whatsApp.onRequest(),
run: async (request, io, ctx) => {
const body = await request.json();
await io.logger.info(`Body`, body);
},
});
// hide-code
// These lines can be removed if you don't want to use express
import { createExpressServer } from "@trigger.dev/express";
createExpressServer(client);
// end-hide-code