0

Delete Message Template

by
Published today

Delete a message template by name (all languages), or a single language version by also passing its template ID.

Script whatsapp_business Verified

The script

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

3
export type DynSelect_name = string
4

5
// Resolver: lists the WABA's message templates for the dropdown.
6
export async function name(auth: RT.WhatsappBusiness) {
7
  const apiVersion = auth.api_version || "v25.0"
8
  const r = await fetch(
9
    `https://graph.facebook.com/${apiVersion}/${auth.business_account_id}/message_templates?fields=name,language,status&limit=100`,
10
    {
11
      headers: {
12
        Authorization: `Bearer ${auth.token}`,
13
        Accept: "application/json",
14
      },
15
    }
16
  )
17
  const { data } = (await r.json()) as {
18
    data: { name: string; language: string; status: string }[]
19
  }
20
  return (data ?? []).map((t) => ({
21
    value: t.name,
22
    label: `${t.name} (${t.language}, ${t.status})`,
23
  }))
24
}
25

26
/**
27
 * Delete Message Template
28
 * Delete a message template by name (all languages), or a single language version by also passing its template ID.
29
 */
30
export async function main(
31
  auth: RT.WhatsappBusiness,
32
  name: DynSelect_name,
33
  template_id: string | undefined
34
) {
35
  const apiVersion = auth.api_version || "v25.0"
36
  const url = new URL(
37
    `https://graph.facebook.com/${apiVersion}/${auth.business_account_id}/message_templates`
38
  )
39
  url.searchParams.append("name", name)
40
  if (template_id !== undefined && template_id !== "") {
41
    url.searchParams.append("hsm_id", template_id)
42
  }
43

44
  const response = await fetch(url, {
45
    method: "DELETE",
46
    headers: {
47
      Authorization: `Bearer ${auth.token}`,
48
      Accept: "application/json",
49
    },
50
  })
51

52
  if (!response.ok) {
53
    throw new Error(`${response.status} ${await response.text()}`)
54
  }
55

56
  return await response.json()
57
}
58