0

Create Message Template

by
Published today

Create a new message template on the WhatsApp Business Account. Templates must be approved by Meta before they can be sent.

Script whatsapp_business Verified

The script

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

3
/**
4
 * Create Message Template
5
 * Create a new message template on the WhatsApp Business Account. Templates must be approved by Meta before they can be sent.
6
 */
7
export async function main(
8
  auth: RT.WhatsappBusiness,
9
  name: string,
10
  category: "AUTHENTICATION" | "MARKETING" | "UTILITY",
11
  language: string,
12
  components: { [key: string]: any }[],
13
  allow_category_change: boolean | undefined
14
) {
15
  const apiVersion = auth.api_version || "v25.0"
16
  const url = new URL(
17
    `https://graph.facebook.com/${apiVersion}/${auth.business_account_id}/message_templates`
18
  )
19

20
  const payload: { [key: string]: any } = {
21
    name,
22
    category,
23
    language,
24
    components,
25
  }
26
  if (allow_category_change !== undefined) {
27
    payload.allow_category_change = allow_category_change
28
  }
29

30
  const response = await fetch(url, {
31
    method: "POST",
32
    headers: {
33
      Authorization: `Bearer ${auth.token}`,
34
      "Content-Type": "application/json",
35
      Accept: "application/json",
36
    },
37
    body: JSON.stringify(payload),
38
  })
39

40
  if (!response.ok) {
41
    throw new Error(`${response.status} ${await response.text()}`)
42
  }
43

44
  return await response.json()
45
}
46