0

List Ticket Comments

by
Published Nov 7, 2023

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.

Script zendesk Verified

The script

Submitted by hugo989 Typescript (fetch-only)
Verified 2 hours ago
1
//native
2

3
/**
4
 * List Ticket Comments
5
 * 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.
6
 */
7
export async function main(
8
  auth: RT.Zendesk,
9
  ticket_id: number,
10
  sort: "created_at" | "-created_at" | undefined,
11
  page_size: number | undefined,
12
  cursor: string | undefined
13
) {
14
  const url = new URL(
15
    `https://${auth.subdomain}.zendesk.com/api/v2/tickets/${ticket_id}/comments`
16
  )
17
  url.searchParams.append("page[size]", String(page_size || 100))
18
  if (sort) {
19
    url.searchParams.append("sort", sort)
20
  }
21
  if (cursor) {
22
    url.searchParams.append("page[after]", cursor)
23
  }
24

25
  const response = await fetch(url, {
26
    headers: {
27
      Authorization: auth.token
28
        ? `Bearer ${auth.token}`
29
        : `Basic ${btoa(`${auth.username}:${auth.password}`)}`,
30
      Accept: "application/json",
31
    },
32
  })
33

34
  if (!response.ok) {
35
    throw new Error(`${response.status} ${await response.text()}`)
36
  }
37

38
  return await response.json()
39
}
40

Other submissions
  • Submitted by hugo697 Typescript (fetch-only)
    Created 528 days ago
    1
    type Zendesk = {
    2
      username: string;
    3
      password: string;
    4
      subdomain: string;
    5
    };
    6
    /**
    7
     * List Comments
    8
     * Returns the comments added to the ticket.
    9
     */
    10
    export async function main(
    11
      auth: Zendesk,
    12
      ticket_id: string,
    13
      include_inline_images: string | undefined,
    14
      include: string | undefined
    15
    ) {
    16
      const url = new URL(
    17
        `https://${auth.subdomain}.zendesk.com/api/v2/tickets/${ticket_id}/comments`
    18
      );
    19
      for (const [k, v] of [
    20
        ["include_inline_images", include_inline_images],
    21
        ["include", include],
    22
      ]) {
    23
        if (v !== undefined && v !== "") {
    24
          url.searchParams.append(k, v);
    25
        }
    26
      }
    27
      const response = await fetch(url, {
    28
        method: "GET",
    29
        headers: {
    30
          Authorization: "Basic " + btoa(`${auth.username}:${auth.password}`),
    31
        },
    32
        body: undefined,
    33
      });
    34
      if (!response.ok) {
    35
        const text = await response.text();
    36
        throw new Error(`${response.status} ${text}`);
    37
      }
    38
      return await response.json();
    39
    }
    40