1 | |
2 |
|
3 | export type DynSelect_group_id = string |
4 |
|
5 | |
6 | export async function group_id(auth: RT.Zendesk) { |
7 | const response = await fetch( |
8 | `https://${auth.subdomain}.zendesk.com/api/v2/groups?page[size]=100`, |
9 | { |
10 | headers: { |
11 | Authorization: auth.token |
12 | ? `Bearer ${auth.token}` |
13 | : `Basic ${btoa(`${auth.username}:${auth.password}`)}`, |
14 | Accept: "application/json", |
15 | }, |
16 | } |
17 | ) |
18 | if (!response.ok) { |
19 | throw new Error(`${response.status} ${await response.text()}`) |
20 | } |
21 | const { groups } = await response.json() |
22 | return groups.map((g: { id: number; name: string }) => ({ |
23 | value: String(g.id), |
24 | label: g.name, |
25 | })) |
26 | } |
27 |
|
28 | |
29 | * Create Ticket |
30 | * Create a ticket. The comment becomes the ticket's first (public) message. A requester given by email is created as an end user if they don't exist yet; without one the ticket is requested by the authenticated agent. Set any other ticket field through `fields`, e.g. `{ "collaborator_ids": [123], "due_at": "2026-12-01T00:00:00Z", "brand_id": 456 }`. |
31 | */ |
32 | export async function main( |
33 | auth: RT.Zendesk, |
34 | subject: string, |
35 | comment: string, |
36 | comment_is_html: boolean, |
37 | requester_email: string | undefined, |
38 | requester_name: string | undefined, |
39 | status: "new" | "open" | "pending" | "hold" | "solved" | "closed" | undefined, |
40 | priority: "urgent" | "high" | "normal" | "low" | undefined, |
41 | type: "problem" | "incident" | "question" | "task" | undefined, |
42 | assignee_id: number | undefined, |
43 | group_id: DynSelect_group_id | undefined, |
44 | tags: string[] | undefined, |
45 | custom_fields: { id: number; value: any }[] | undefined, |
46 | fields: { [key: string]: any } | undefined |
47 | ) { |
48 | const ticket: { [key: string]: any } = { |
49 | ...fields, |
50 | subject, |
51 | comment: comment_is_html ? { html_body: comment } : { body: comment }, |
52 | status: status || undefined, |
53 | priority: priority || undefined, |
54 | type: type || undefined, |
55 | assignee_id: assignee_id ?? undefined, |
56 | group_id: group_id ? Number(group_id) : undefined, |
57 | tags: tags?.length ? tags : undefined, |
58 | custom_fields: custom_fields?.length ? custom_fields : undefined, |
59 | } |
60 | if (requester_email) { |
61 | ticket.requester = { |
62 | email: requester_email, |
63 | name: requester_name || undefined, |
64 | } |
65 | } |
66 |
|
67 | const response = await fetch( |
68 | `https://${auth.subdomain}.zendesk.com/api/v2/tickets`, |
69 | { |
70 | method: "POST", |
71 | headers: { |
72 | Authorization: auth.token |
73 | ? `Bearer ${auth.token}` |
74 | : `Basic ${btoa(`${auth.username}:${auth.password}`)}`, |
75 | "Content-Type": "application/json", |
76 | Accept: "application/json", |
77 | }, |
78 | body: JSON.stringify({ ticket }), |
79 | } |
80 | ) |
81 |
|
82 | if (!response.ok) { |
83 | throw new Error(`${response.status} ${await response.text()}`) |
84 | } |
85 |
|
86 | return await response.json() |
87 | } |
88 |
|