1 | |
2 |
|
3 | |
4 | * Send Location Message |
5 | * Send a location message with coordinates and an optional name and address to a WhatsApp user. |
6 | */ |
7 | export async function main( |
8 | auth: RT.WhatsappBusiness, |
9 | to: string, |
10 | latitude: number, |
11 | longitude: number, |
12 | name: string | undefined, |
13 | address: string | undefined |
14 | ) { |
15 | const apiVersion = auth.api_version || "v25.0" |
16 | const url = new URL( |
17 | `https://graph.facebook.com/${apiVersion}/${auth.phone_number_id}/messages` |
18 | ) |
19 |
|
20 | const location: { [key: string]: any } = { latitude, longitude } |
21 | if (name !== undefined && name !== "") { |
22 | location.name = name |
23 | } |
24 | if (address !== undefined && address !== "") { |
25 | location.address = address |
26 | } |
27 |
|
28 | const response = await fetch(url, { |
29 | method: "POST", |
30 | headers: { |
31 | Authorization: `Bearer ${auth.token}`, |
32 | "Content-Type": "application/json", |
33 | Accept: "application/json", |
34 | }, |
35 | body: JSON.stringify({ |
36 | messaging_product: "whatsapp", |
37 | recipient_type: "individual", |
38 | to, |
39 | type: "location", |
40 | location, |
41 | }), |
42 | }) |
43 |
|
44 | if (!response.ok) { |
45 | throw new Error(`${response.status} ${await response.text()}`) |
46 | } |
47 |
|
48 | return await response.json() |
49 | } |
50 |
|