import {z} from 'zod' export type CreateApiV1InboxConversationsConversationIdMessagesArgs = {conversationId: string, 'Idempotency-Key'?: string | undefined, body: {accountId: string, message?: string | undefined, attachmentUrl?: string | undefined, category?: 'utility' | undefined, linkPreview?: boolean | undefined, attachmentType?: ('image' | 'video' | 'audio' | 'file') | undefined, attachmentName?: string | undefined, voiceNote?: boolean | undefined, subject?: string | undefined, cc?: Array | undefined, effect?: ('slam' | 'loud' | 'gentle' | 'invisibleInk' | 'echo' | 'spotlight' | 'balloons' | 'confetti' | 'love' | 'lasers' | 'fireworks' | 'shootingStar' | 'celebration') | undefined, contactCard?: boolean | undefined, channel?: ('imessage' | 'sms' | 'rcs' | 'whatsapp') | undefined, sharedMediaId?: string | undefined, sticker?: 'like_heart' | undefined, quickReplies?: (Array<{title: string, payload: string, imageUrl?: string | undefined}>) | undefined, buttons?: (Array<{type: 'url' | 'postback' | 'phone', title: string, url?: string | undefined, payload?: string | undefined, phone?: string | undefined}>) | undefined, template?: ({type?: 'generic' | undefined, imageAspectRatio?: ('horizontal' | 'square') | undefined, elements?: (Array<{title?: string | undefined, subtitle?: string | undefined, imageUrl?: string | undefined, buttons?: (Array<{type?: ('url' | 'postback') | undefined, title?: string | undefined, url?: string | undefined, payload?: string | undefined}>) | undefined, name?: string | undefined, language?: string | undefined, components?: Array<{[key: string]: unknown}> | undefined}>) | undefined}) | undefined, interactive?: ({type: 'list' | 'cta_url' | 'flow' | 'location_request_message' | 'request_contact_info' | 'voice_call' | 'product' | 'product_list' | 'catalog_message' | 'carousel' | 'address_message', header?: ({type?: ('text' | 'image' | 'video' | 'document') | undefined, text?: string | undefined, image?: ({link?: string | undefined}) | undefined, video?: ({link?: string | undefined}) | undefined, document?: ({link?: string | undefined}) | undefined}) | undefined, body?: {text: string} | undefined, footer?: ({text?: string | undefined}) | undefined, action?: ({button: string, sections: Array<{title?: string | undefined, rows: Array<{id: string, title: string, description?: string | undefined}>}>} | {name: 'cta_url', parameters: {display_text: string, url: string}} | {name: 'voice_call', parameters?: ({display_text?: string | undefined, ttl_minutes?: number | undefined, payload?: string | undefined}) | undefined} | {name: 'flow', parameters: {flow_message_version?: '3' | undefined, flow_token: string, flow_id: string, flow_cta: string, flow_action: 'navigate' | 'data_exchange', flow_action_payload?: ({screen?: string | undefined, data?: {[key: string]: unknown} | undefined}) | undefined, mode?: 'draft' | undefined}} | {name: 'send_location'} | {name: 'request_contact_info'} | {catalog_id: string, product_retailer_id: string} | {catalog_id: string, sections: Array<{title?: string | undefined, product_items: Array<{product_retailer_id: string}>}>} | {name: 'catalog_message', parameters?: ({thumbnail_product_retailer_id?: string | undefined}) | undefined} | {cards: Array<{card_index?: number | undefined, type?: string | undefined, header?: Record | undefined, body?: Record | undefined, action?: Record | undefined} | {[key: string]: unknown}>} | {name: 'address_message', parameters: {country: string, values?: {[key: string]: unknown} | undefined, saved_addresses?: Array<{[key: string]: unknown}> | undefined, validation_errors?: {[key: string]: string} | undefined}}) | undefined}) | undefined, replyMarkup?: ({type?: ('inline_keyboard' | 'reply_keyboard') | undefined, keyboard?: (Array>) | undefined, oneTime?: boolean | undefined}) | undefined, messagingType?: ('RESPONSE' | 'UPDATE' | 'MESSAGE_TAG') | undefined, messageTag?: ('CONFIRMED_EVENT_UPDATE' | 'POST_PURCHASE_UPDATE' | 'ACCOUNT_UPDATE' | 'HUMAN_AGENT') | undefined, replyTo?: string | undefined, location?: ({latitude: number, longitude: number, name?: string | undefined, address?: string | undefined}) | undefined, contacts?: (Array<{name: {formatted_name: string, first_name?: string | undefined, last_name?: string | undefined}, phones?: (Array<{phone?: string | undefined, type?: string | undefined}>) | undefined, emails?: (Array<{email?: string | undefined, type?: string | undefined}>) | undefined}>) | undefined}}; export type CreateApiV1InboxConversationsConversationIdMessagesOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiV1InboxConversationsConversationIdMessagesResponse = z.object({success: z.boolean().optional(), warnings: z.array(z.object({code: z.literal("ignored_field"), param: z.literal("replyTo"), message: z.string()})).optional(), data: z.object({messageId: z.string().optional(), conversationId: z.string().optional(), attachments: z.array(z.object({type: z.string().optional(), url: z.string().optional()})).optional(), messageIds: z.array(z.string()).optional(), partialFailure: z.object({part: z.literal("text").optional(), error: z.string().optional(), platformError: z.object({code: z.number().int().optional(), subcode: z.number().int().optional(), fbtraceId: z.string().optional(), type: z.string().optional()}).optional()}).optional()}).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 }; /** * Send message * * POST /v1/inbox/conversations/{conversationId}/messages */ export const createApiV1InboxConversationsConversationIdMessages = async (args: CreateApiV1InboxConversationsConversationIdMessagesArgs, options?: CreateApiV1InboxConversationsConversationIdMessagesOptions) => { const {baseUrl = "https://zernio.com/api", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args['Idempotency-Key'] != null) headers.set("Idempotency-Key", String(args['Idempotency-Key'])) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/inbox/conversations/${encodeURIComponent(String(args.conversationId))}/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(createApiV1InboxConversationsConversationIdMessagesResponse, await res.json()) };