Edits history of script submission #22805 for ' Send Interactive Message (whatsapp_business)'

  • bunnative
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    
    /**
     * Send Interactive Message
     * Send an interactive message (reply buttons, list, call-to-action URL or flow) to a WhatsApp user. The interactive object follows the Cloud API interactive message format.
     */
    export async function main(
      auth: RT.WhatsappBusiness,
      to: string,
      interactive: { [key: string]: any }
    ) {
      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: "interactive",
          interactive,
        }),
      })
    
      if (!response.ok) {
        throw new Error(`${response.status} ${await response.text()}`)
      }
    
      return await response.json()
    }
    

    Submitted by hugo989 5 hours ago