1 | |
2 | type Brevo = { |
3 | apiKey: string; |
4 | }; |
5 | |
6 | * Get all your WhatsApp activity (unaggregated events) |
7 | * This endpoint will show the unaggregated statistics for WhatsApp activity (30 days by default if `startDate` and `endDate` or `days` is not passed. The date range can not exceed 90 days) |
8 | */ |
9 | export async function main( |
10 | auth: Brevo, |
11 | limit: string | undefined, |
12 | offset: string | undefined, |
13 | startDate: string | undefined, |
14 | endDate: string | undefined, |
15 | days: string | undefined, |
16 | contactNumber: string | undefined, |
17 | event: |
18 | | "sent" |
19 | | "delivered" |
20 | | "read" |
21 | | "error" |
22 | | "unsubscribe" |
23 | | "reply" |
24 | | "soft-bounce" |
25 | | undefined, |
26 | sort: "asc" | "desc" | undefined, |
27 | ) { |
28 | const url = new URL(`https://api.brevo.com/v3/whatsapp/statistics/events`); |
29 | for (const [k, v] of [ |
30 | ["limit", limit], |
31 | ["offset", offset], |
32 | ["startDate", startDate], |
33 | ["endDate", endDate], |
34 | ["days", days], |
35 | ["contactNumber", contactNumber], |
36 | ["event", event], |
37 | ["sort", sort], |
38 | ]) { |
39 | if (v !== undefined && v !== "" && k !== undefined) { |
40 | url.searchParams.append(k, v); |
41 | } |
42 | } |
43 | const response = await fetch(url, { |
44 | method: "GET", |
45 | headers: { |
46 | "api-key": auth.apiKey, |
47 | }, |
48 | body: undefined, |
49 | }); |
50 | if (!response.ok) { |
51 | const text = await response.text(); |
52 | throw new Error(`${response.status} ${text}`); |
53 | } |
54 | return await response.json(); |
55 | } |
56 |
|