0

List Active Macros

by
Published Nov 7, 2023

List the active macros the agent can apply. Offset-paged (`page`, up to 100 per page).

Script zendesk Verified

The script

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

3
/**
4
 * List Active Macros
5
 * List the active macros the agent can apply. Offset-paged (`page`, up to 100 per page).
6
 */
7
export async function main(
8
  auth: RT.Zendesk,
9
  access: "personal" | "agents" | "shared" | "account" | undefined,
10
  category: number | undefined,
11
  group_id: number | undefined,
12
  sort_by:
13
    | "alphabetical"
14
    | "created_at"
15
    | "updated_at"
16
    | "usage_1h"
17
    | "usage_24h"
18
    | "usage_7d"
19
    | "usage_30d"
20
    | undefined,
21
  sort_order: "asc" | "desc" | undefined,
22
  page: number | undefined,
23
  per_page: number | undefined
24
) {
25
  const url = new URL(
26
    `https://${auth.subdomain}.zendesk.com/api/v2/macros/active`
27
  )
28
  const params = {
29
    access,
30
    category,
31
    group_id,
32
    sort_by,
33
    sort_order,
34
    page: page || 1,
35
    per_page: per_page || 100,
36
  }
37
  for (const [key, value] of Object.entries(params)) {
38
    if (value != null && value !== "") {
39
      url.searchParams.append(key, String(value))
40
    }
41
  }
42

43
  const response = await fetch(url, {
44
    headers: {
45
      Authorization: auth.token
46
        ? `Bearer ${auth.token}`
47
        : `Basic ${btoa(`${auth.username}:${auth.password}`)}`,
48
      Accept: "application/json",
49
    },
50
  })
51

52
  if (!response.ok) {
53
    throw new Error(`${response.status} ${await response.text()}`)
54
  }
55

56
  return await response.json()
57
}
58

Other submissions
  • Submitted by hugo697 Typescript (fetch-only)
    Created 540 days ago
    1
    type Zendesk = {
    2
      username: string;
    3
      password: string;
    4
      subdomain: string;
    5
    };
    6
    /**
    7
     * List Active Macros
    8
     * Lists all active shared and personal macros available to the current user.
    9
    
    
    10
    #### Allowed For
    11
    * Agents
    12
    
    
    13
     */
    14
    export async function main(
    15
      auth: Zendesk,
    16
      include: string | undefined,
    17
      access: string | undefined,
    18
      category: string | undefined,
    19
      group_id: string | undefined,
    20
      sort_by: string | undefined,
    21
      sort_order: string | undefined
    22
    ) {
    23
      const url = new URL(
    24
        `https://${auth.subdomain}.zendesk.com/api/v2/macros/active`
    25
      );
    26
      for (const [k, v] of [
    27
        ["include", include],
    28
        ["access", access],
    29
        ["category", category],
    30
        ["group_id", group_id],
    31
        ["sort_by", sort_by],
    32
        ["sort_order", sort_order],
    33
      ]) {
    34
        if (v !== undefined && v !== "") {
    35
          url.searchParams.append(k, v);
    36
        }
    37
      }
    38
      const response = await fetch(url, {
    39
        method: "GET",
    40
        headers: {
    41
          Authorization: "Basic " + btoa(`${auth.username}:${auth.password}`),
    42
        },
    43
        body: undefined,
    44
      });
    45
      if (!response.ok) {
    46
        const text = await response.text();
    47
        throw new Error(`${response.status} ${text}`);
    48
      }
    49
      return await response.json();
    50
    }
    51