Edits history of script submission #22806 for ' Send Location Message (whatsapp_business)'

  • bunnative
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    
    /**
     * Send Location Message
     * Send a location message with coordinates and an optional name and address to a WhatsApp user.
     */
    export async function main(
      auth: RT.WhatsappBusiness,
      to: string,
      latitude: number,
      longitude: number,
      name: string | undefined,
      address: string | undefined
    ) {
      const apiVersion = auth.api_version || "v25.0"
      const url = new URL(
        `https://graph.facebook.com/${apiVersion}/${auth.phone_number_id}/messages`
      )
    
      const location: { [key: string]: any } = { latitude, longitude }
      if (name !== undefined && name !== "") {
        location.name = name
      }
      if (address !== undefined && address !== "") {
        location.address = address
      }
    
      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: "location",
          location,
        }),
      })
    
      if (!response.ok) {
        throw new Error(`${response.status} ${await response.text()}`)
      }
    
      return await response.json()
    }
    

    Submitted by hugo989 5 hours ago