Edits history of script submission #22804 for ' Mark Message as Read (whatsapp_business)'

  • bunnative
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    
    /**
     * Mark Message as Read
     * Mark a received message as read (blue ticks), optionally showing a typing indicator that lasts up to 25 seconds.
     */
    export async function main(
      auth: RT.WhatsappBusiness,
      message_id: string,
      show_typing_indicator: boolean | undefined
    ) {
      const apiVersion = auth.api_version || "v25.0"
      const url = new URL(
        `https://graph.facebook.com/${apiVersion}/${auth.phone_number_id}/messages`
      )
    
      const payload: { [key: string]: any } = {
        messaging_product: "whatsapp",
        status: "read",
        message_id,
      }
      if (show_typing_indicator) {
        payload.typing_indicator = { type: "text" }
      }
    
      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