//native
export type DynSelect_group_id = string
// Resolver: the account's groups (first 100).
export async function group_id(auth: RT.Zendesk) {
const response = await fetch(
`https://${auth.subdomain}.zendesk.com/api/v2/groups?page[size]=100`,
{
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()}`)
}
const { groups } = await response.json()
return groups.map((g: { id: number; name: string }) => ({
value: String(g.id),
label: g.name,
}))
}
/**
* Create Ticket
* 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 }`.
*/
export async function main(
auth: RT.Zendesk,
subject: string,
comment: string,
comment_is_html: boolean,
requester_email: string | undefined,
requester_name: string | undefined,
status: "new" | "open" | "pending" | "hold" | "solved" | "closed" | undefined,
priority: "urgent" | "high" | "normal" | "low" | undefined,
type: "problem" | "incident" | "question" | "task" | undefined,
assignee_id: number | undefined,
group_id: DynSelect_group_id | undefined,
tags: string[] | undefined,
custom_fields: { id: number; value: any }[] | undefined,
fields: { [key: string]: any } | undefined
) {
const ticket: { [key: string]: any } = {
...fields,
subject,
comment: comment_is_html ? { html_body: comment } : { body: comment },
status: status || undefined,
priority: priority || undefined,
type: type || undefined,
assignee_id: assignee_id ?? undefined,
group_id: group_id ? Number(group_id) : undefined,
tags: tags?.length ? tags : undefined,
custom_fields: custom_fields?.length ? custom_fields : undefined,
}
if (requester_email) {
ticket.requester = {
email: requester_email,
name: requester_name || undefined,
}
}
const response = await fetch(
`https://${auth.subdomain}.zendesk.com/api/v2/tickets`,
{
method: "POST",
headers: {
Authorization: auth.token
? `Bearer ${auth.token}`
: `Basic ${btoa(`${auth.username}:${auth.password}`)}`,
"Content-Type": "application/json",
Accept: "application/json",
},
body: JSON.stringify({ ticket }),
}
)
if (!response.ok) {
throw new Error(`${response.status} ${await response.text()}`)
}
return await response.json()
}
Submitted by hugo989 5 days ago