0

List Message Templates

by
Published today

List the message templates of the WhatsApp Business Account, optionally filtered by name or status.

Script whatsapp_business Verified

The script

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

3
/**
4
 * List Message Templates
5
 * List the message templates of the WhatsApp Business Account, optionally filtered by name or status.
6
 */
7
export async function main(
8
  auth: RT.WhatsappBusiness,
9
  name: string | undefined,
10
  status:
11
    | "APPROVED"
12
    | "PENDING"
13
    | "REJECTED"
14
    | "PAUSED"
15
    | "DISABLED"
16
    | undefined,
17
  limit: number | undefined,
18
  after: string | undefined
19
) {
20
  const apiVersion = auth.api_version || "v25.0"
21
  const url = new URL(
22
    `https://graph.facebook.com/${apiVersion}/${auth.business_account_id}/message_templates`
23
  )
24
  url.searchParams.append(
25
    "fields",
26
    "id,name,status,category,language,components,quality_score"
27
  )
28
  if (name !== undefined && name !== "") {
29
    url.searchParams.append("name", name)
30
  }
31
  if (status !== undefined) {
32
    url.searchParams.append("status", status)
33
  }
34
  url.searchParams.append("limit", String(limit ?? 100))
35
  if (after !== undefined && after !== "") {
36
    url.searchParams.append("after", after)
37
  }
38

39
  const response = await fetch(url, {
40
    method: "GET",
41
    headers: {
42
      Authorization: `Bearer ${auth.token}`,
43
      Accept: "application/json",
44
    },
45
  })
46

47
  if (!response.ok) {
48
    throw new Error(`${response.status} ${await response.text()}`)
49
  }
50

51
  return await response.json()
52
}
53