Edits history of script submission #22798 for ' Delete Message Template (whatsapp_business)'

  • bunnative
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    
    export type DynSelect_name = string
    
    // Resolver: lists the WABA's message templates for the dropdown.
    export async function name(auth: RT.WhatsappBusiness) {
      const apiVersion = auth.api_version || "v25.0"
      const r = await fetch(
        `https://graph.facebook.com/${apiVersion}/${auth.business_account_id}/message_templates?fields=name,language,status&limit=100`,
        {
          headers: {
            Authorization: `Bearer ${auth.token}`,
            Accept: "application/json",
          },
        }
      )
      const { data } = (await r.json()) as {
        data: { name: string; language: string; status: string }[]
      }
      return (data ?? []).map((t) => ({
        value: t.name,
        label: `${t.name} (${t.language}, ${t.status})`,
      }))
    }
    
    /**
     * Delete Message Template
     * Delete a message template by name (all languages), or a single language version by also passing its template ID.
     */
    export async function main(
      auth: RT.WhatsappBusiness,
      name: DynSelect_name,
      template_id: string | undefined
    ) {
      const apiVersion = auth.api_version || "v25.0"
      const url = new URL(
        `https://graph.facebook.com/${apiVersion}/${auth.business_account_id}/message_templates`
      )
      url.searchParams.append("name", name)
      if (template_id !== undefined && template_id !== "") {
        url.searchParams.append("hsm_id", template_id)
      }
    
      const response = await fetch(url, {
        method: "DELETE",
        headers: {
          Authorization: `Bearer ${auth.token}`,
          Accept: "application/json",
        },
      })
    
      if (!response.ok) {
        throw new Error(`${response.status} ${await response.text()}`)
      }
    
      return await response.json()
    }
    

    Submitted by hugo989 5 hours ago