Edits history of script submission #23841 for ' List Ticket Comments (zendesk)'

  • bunnative
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    
    /**
     * List Ticket Comments
     * List a ticket's comments (public replies and internal notes), with their attachments. Cursor-paged: pass `meta.after_cursor` as `cursor` while `meta.has_more` is true.
     */
    export async function main(
      auth: RT.Zendesk,
      ticket_id: number,
      sort: "created_at" | "-created_at" | undefined,
      page_size: number | undefined,
      cursor: string | undefined
    ) {
      const url = new URL(
        `https://${auth.subdomain}.zendesk.com/api/v2/tickets/${ticket_id}/comments`
      )
      url.searchParams.append("page[size]", String(page_size || 100))
      if (sort) {
        url.searchParams.append("sort", sort)
      }
      if (cursor) {
        url.searchParams.append("page[after]", cursor)
      }
    
      const response = await fetch(url, {
        headers: {
          Authorization: auth.token
            ? `Bearer ${auth.token}`
            : `Basic ${btoa(`${auth.username}:${auth.password}`)}`,
          Accept: "application/json",
        },
      })
    
      if (!response.ok) {
        throw new Error(`${response.status} ${await response.text()}`)
      }
    
      return await response.json()
    }
    

    Submitted by hugo989 2 hours ago