Edits history of script submission #22796 for ' Create Message Template (whatsapp_business)'

  • bunnative
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    
    /**
     * Create Message Template
     * Create a new message template on the WhatsApp Business Account. Templates must be approved by Meta before they can be sent.
     */
    export async function main(
      auth: RT.WhatsappBusiness,
      name: string,
      category: "AUTHENTICATION" | "MARKETING" | "UTILITY",
      language: string,
      components: { [key: string]: any }[],
      allow_category_change: boolean | undefined
    ) {
      const apiVersion = auth.api_version || "v25.0"
      const url = new URL(
        `https://graph.facebook.com/${apiVersion}/${auth.business_account_id}/message_templates`
      )
    
      const payload: { [key: string]: any } = {
        name,
        category,
        language,
        components,
      }
      if (allow_category_change !== undefined) {
        payload.allow_category_change = allow_category_change
      }
    
      const response = await fetch(url, {
        method: "POST",
        headers: {
          Authorization: `Bearer ${auth.token}`,
          "Content-Type": "application/json",
          Accept: "application/json",
        },
        body: JSON.stringify(payload),
      })
    
      if (!response.ok) {
        throw new Error(`${response.status} ${await response.text()}`)
      }
    
      return await response.json()
    }
    

    Submitted by hugo989 5 hours ago