import {z} from 'zod' export type CreateMessageAttachment = {url: string, name: string, size?: number | undefined, content_type: string, public?: boolean | undefined, extra?: (string | number | number | boolean | Record | Array) | undefined}; export type LegacyChannelSlug = 'aircall' | 'api' | 'chat' | 'contact_form' | 'email' | 'facebook' | 'facebook-mention' | 'facebook-messenger' | 'facebook-recommendations' | 'help-center' | 'instagram-ad-comment' | 'instagram-comment' | 'instagram-direct-message' | 'instagram-mention' | 'internal-note' | 'phone' | 'sms' | 'twitter' | 'twitter-direct-message' | 'whatsapp' | 'yotpo-review'; export type CreateMessageLastSendingError = {error: string}; export type CreateMessageMacro = {id: number}; export type CreateMessageMeta = {relevant_content_indexes?: Array | undefined} | {[key: string]: unknown}; export type TicketMessageSourceType = 'aircall' | 'api' | 'app' | 'custom-chat' | 'chat' | 'chat-contact-form' | 'chat-offline-capture' | 'contact-form' | 'email' | 'facebook' | 'facebook-comment' | 'facebook-mention-comment' | 'facebook-mention-post' | 'facebook-message' | 'facebook-messenger' | 'facebook-post' | 'facebook-review' | 'facebook-review-comment' | 'form' | 'google-business-messages' | 'help-center' | 'help-center-contact-form' | 'helpdesk' | 'instagram' | 'instagram-ad-comment' | 'instagram-ad-media' | 'instagram-comment' | 'instagram-direct-message' | 'instagram-media' | 'instagram-mention-comment' | 'instagram-mention-media' | 'internal-note' | 'ottspott-call' | 'phone' | 'rule' | 'self_service' | 'shopify' | 'sms' | 'system-message' | 'tiktok-shop' | 'twilio' | 'twitter-direct-message' | 'twitter-mention-tweet' | 'twitter-quoted-tweet' | 'twitter-tweet' | 'whatsapp-message' | 'yotpo-review' | 'yotpo-review-private-comment' | 'yotpo-review-public-comment'; export type CreateMessageSourcePerson = {name?: string | undefined, address?: string | undefined}; export type CreateMessageSource = {type?: TicketMessageSourceType | undefined, from?: CreateMessageSourcePerson | undefined, to?: Array | undefined, cc?: Array | undefined, bcc?: Array | undefined, extra?: (string | number | number | boolean | Record | Array) | undefined}; export type CreateMessageCustomerChannel = {type: string, address: string}; export type CreateMessageCustomerOrUser = {id?: number | undefined, email?: string | undefined, name?: string | undefined, external_id?: string | undefined, note?: string | undefined, timezone?: string | undefined, language?: string | undefined, meta?: Record | undefined, channels?: Array | undefined}; export type CreateMessage = {actions?: (Array | Array>) | undefined, attachments?: Array | undefined, body_html?: string | undefined, body_text?: string | undefined, channel: LegacyChannelSlug | string, created_datetime?: string | undefined, deleted_datetime?: string | undefined, external_id?: string | undefined, failed_datetime?: string | undefined, from_agent: boolean, headers?: ({[key: string]: string | number | number | boolean | Record | Array}) | undefined, integration_id?: number | undefined, last_sending_error?: CreateMessageLastSendingError | undefined, macros?: Array | undefined, message_id?: string | undefined, meta?: CreateMessageMeta | undefined, opened_datetime?: string | undefined, public?: boolean | undefined, sent_datetime?: string | undefined, source?: CreateMessageSource | undefined, stripped_html?: string | undefined, stripped_signature?: string | undefined, stripped_text?: string | undefined, subject?: string | undefined, via?: ('aircall' | 'api' | 'chat' | 'contact_form' | 'email' | 'facebook' | 'facebook-mention' | 'facebook-messenger' | 'facebook-recommendations' | 'form' | 'helpdesk' | 'help-center' | 'instagram' | 'instagram-ad-comment' | 'instagram-comment' | 'instagram-direct-message' | 'instagram-mention' | 'internal-note' | 'phone' | 'rule' | 'shopify' | 'sms' | 'twilio' | 'twitter' | 'twitter-direct-message' | 'whatsapp' | 'yotpo' | 'yotpo-review' | 'zendesk') | undefined, imported?: boolean | undefined, mention_ids?: Array | undefined, receiver?: CreateMessageCustomerOrUser | undefined, sender?: CreateMessageCustomerOrUser | undefined}; export type CreateApiApiTicketsTicketIdMessagesArgs = {ticket_id: number, action?: ('force' | 'retry' | 'cancel') | undefined, 'If-Unmodified-Since'?: string | undefined, body: CreateMessage}; export type CreateApiApiTicketsTicketIdMessagesOptions = {baseUrl?: string | undefined, basicAuth?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const legacyChannelSlug = z.enum(["aircall", "api", "chat", "contact_form", "email", "facebook", "facebook-mention", "facebook-messenger", "facebook-recommendations", "help-center", "instagram-ad-comment", "instagram-comment", "instagram-direct-message", "instagram-mention", "internal-note", "phone", "sms", "twitter", "twitter-direct-message", "whatsapp", "yotpo-review"]); export const ticketVia = z.enum(["aircall", "api", "chat", "contact_form", "email", "facebook", "facebook-mention", "facebook-messenger", "facebook-recommendations", "form", "gorgias_chat", "help-center", "helpdesk", "instagram", "instagram-ad-comment", "instagram-comment", "instagram-direct-message", "instagram-mention", "internal-note", "offline_capture", "phone", "rule", "self_service", "shopify", "sms", "twilio", "twitter", "twitter-direct-message", "whatsapp", "yotpo", "yotpo-review", "zendesk"]); export const ticketMessageSourceAddress = z.object({name: z.string().optional(), address: z.string().optional()}); export const ticketMessageSource = z.object({type: z.string(), to: z.array(ticketMessageSourceAddress).optional(), cc: z.array(ticketMessageSourceAddress).optional(), bcc: z.array(ticketMessageSourceAddress).optional(), from: ticketMessageSourceAddress.optional(), extra: z.unknown()}); export const ticketMessageUserOrCustomer = z.object({id: z.number().int(), email: z.string(), name: z.string(), firstname: z.string(), lastname: z.string(), meta: z.unknown()}); export const ticketMessageAuthCustomerIdentityService = z.object({id: z.string(), name: z.string()}); export const ticketMessageAuthCustomerIdentity = z.object({service: ticketMessageAuthCustomerIdentityService, identifier: z.string()}); export const ticketMessageIntent = z.object({name: z.string(), rejected: z.boolean(), is_user_feedback: z.boolean()}); export const ticketMessageAttachment = z.object({url: z.string(), name: z.string().optional(), size: z.number().int().optional(), content_type: z.string(), public: z.boolean().optional(), extra: z.unknown()}); export const ticketMessageMacro = z.object({id: z.number().int().gt(0)}); export const ticketMessageSendingError = z.object({error: z.string()}); export const ticketMessageRepliedByOrTo = z.object({integration_id: z.number().int(), message_id: z.string(), created_datetime: z.string(), body_text: z.string(), source: ticketMessageSource, customer: ticketMessageUserOrCustomer, user: ticketMessageUserOrCustomer}); export const ticketMessage = z.object({id: z.number().int(), uri: z.string(), message_id: z.string(), ticket_id: z.number().int(), external_id: z.string(), public: z.boolean().optional(), channel: z.union([legacyChannelSlug, z.string()]), via: ticketVia, source: ticketMessageSource, sender: ticketMessageUserOrCustomer, auth_customer_identity: ticketMessageAuthCustomerIdentity, integration_id: z.number().int(), intents: z.array(ticketMessageIntent), rule_id: z.number().int(), from_agent: z.boolean(), receiver: ticketMessageUserOrCustomer, subject: z.string(), body_text: z.string(), body_html: z.string(), body_url: z.string(), stripped_text: z.string(), stripped_html: z.string(), stripped_signature: z.string(), attachments: z.array(ticketMessageAttachment), macros: z.array(ticketMessageMacro).optional(), actions: z.array(z.object({})), headers: z.object({}).optional(), imported: z.boolean(), meta: z.unknown(), created_datetime: z.string(), processed_datetime: z.string(), deleted_datetime: z.string().optional(), sent_datetime: z.string(), failed_datetime: z.string(), opened_datetime: z.string(), last_sending_error: ticketMessageSendingError, is_retriable: z.boolean(), replied_by: ticketMessageRepliedByOrTo.optional(), replied_to: ticketMessageRepliedByOrTo.optional()}); export const validateResponse = async ( schema: { readonly '~standard': { readonly validate: (value: unknown) => ({ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }) | Promise<{ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }> } }, value: unknown ): Promise => { const result = await schema['~standard'].validate(value) if (result.issues !== undefined) { const detail = result.issues .map(({ message, path }) => path?.length ? `${path.map((segment) => String(typeof segment === 'object' ? segment.key : segment)).join('.')}: ${message}` : message ) .join('; ') throw new Error(`Response does not match the schema: ${detail}`, { cause: { issues: result.issues } }) } return result.value }; /** * Create a message * * POST /api/tickets/{ticket_id}/messages */ export const createApiApiTicketsTicketIdMessages = async (args: CreateApiApiTicketsTicketIdMessagesArgs, options?: CreateApiApiTicketsTicketIdMessagesOptions) => { const {baseUrl = "https://your-domain.gorgias.com", fetch: fetchFn = fetch, basicAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args.action != null) query.append("action", String(args.action)) if (args['If-Unmodified-Since'] != null) headers.set("If-Unmodified-Since", String(args['If-Unmodified-Since'])) if (basicAuth !== undefined) headers.set('authorization', `Basic ${btoa(`${basicAuth.username}:${basicAuth.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/tickets/${encodeURIComponent(String(args.ticket_id))}/messages${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(ticketMessage, await res.json()) };