Edits history of script submission #22810 for ' Send Text Message (whatsapp_business)'

  • bunnative
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    
    /**
     * Send Text Message
     * Send a text message to a WhatsApp user. Free-form messages can only be sent within 24 hours of the user's last message; otherwise use a template.
     */
    export async function main(
      auth: RT.WhatsappBusiness,
      to: string,
      text: string,
      preview_url: boolean | undefined
    ) {
      const apiVersion = auth.api_version || "v25.0"
      const url = new URL(
        `https://graph.facebook.com/${apiVersion}/${auth.phone_number_id}/messages`
      )
    
      const response = await fetch(url, {
        method: "POST",
        headers: {
          Authorization: `Bearer ${auth.token}`,
          "Content-Type": "application/json",
          Accept: "application/json",
        },
        body: JSON.stringify({
          messaging_product: "whatsapp",
          recipient_type: "individual",
          to,
          type: "text",
          text: {
            body: text,
            preview_url: preview_url ?? false,
          },
        }),
      })
    
      if (!response.ok) {
        throw new Error(`${response.status} ${await response.text()}`)
      }
    
      return await response.json()
    }
    

    Submitted by hugo989 5 hours ago