0

Send Interactive Message

by
Published today

Send an interactive message (reply buttons, list, call-to-action URL or flow) to a WhatsApp user. The interactive object follows the Cloud API interactive message format.

Script whatsapp_business Verified

The script

Submitted by hugo989 Typescript (fetch-only)
Verified 4 hours ago
1
//native
2

3
/**
4
 * Send Interactive Message
5
 * Send an interactive message (reply buttons, list, call-to-action URL or flow) to a WhatsApp user. The interactive object follows the Cloud API interactive message format.
6
 */
7
export async function main(
8
  auth: RT.WhatsappBusiness,
9
  to: string,
10
  interactive: { [key: string]: any }
11
) {
12
  const apiVersion = auth.api_version || "v25.0"
13
  const url = new URL(
14
    `https://graph.facebook.com/${apiVersion}/${auth.phone_number_id}/messages`
15
  )
16

17
  const response = await fetch(url, {
18
    method: "POST",
19
    headers: {
20
      Authorization: `Bearer ${auth.token}`,
21
      "Content-Type": "application/json",
22
      Accept: "application/json",
23
    },
24
    body: JSON.stringify({
25
      messaging_product: "whatsapp",
26
      recipient_type: "individual",
27
      to,
28
      type: "interactive",
29
      interactive,
30
    }),
31
  })
32

33
  if (!response.ok) {
34
    throw new Error(`${response.status} ${await response.text()}`)
35
  }
36

37
  return await response.json()
38
}
39