import {z} from 'zod' export type TextMessage = {type: 'text', userId: string, botId?: string | undefined, text: string}; export type ButtonText = {type: 'text', text: string}; export type ButtonOption = {title: string, description?: string | undefined, payload?: string | undefined}; export type Buttons = {title?: string | undefined, buttonText?: ButtonText | undefined, options: Array}; export type TextWithOptionsMessage = {type: 'text', userId: string, botId?: string | undefined, text: string, buttons: Buttons}; export type QuickReply = {title: string}; export type QuickReplyMessage = {type: 'quick_reply', userId: string, botId?: string | undefined, text: string, title?: string | undefined, quick_replies: Array}; export type ImageMessage = {type: 'image', userId: string, botId?: string | undefined, mediaUrl: string, text?: string | undefined}; export type AudioMessage = {type: 'audio', userId: string, botId?: string | undefined, mediaUrl: string}; export type VideoMessage = {type: 'video', userId: string, botId?: string | undefined, mediaUrl: string, text?: string | undefined}; export type FileMessage = {type: 'file', userId: string, botId?: string | undefined, mediaUrl: string, text?: string | undefined, filename?: string | undefined}; export type StickerMessage = {type: 'sticker', userId: string, botId?: string | undefined, mediaUrl: string}; export type Coordinates = {lat: number, long: number}; export type LocationMessage = {type: 'location', userId: string, botId?: string | undefined, coordinates: Coordinates}; export type ContactName = {formatted_name?: string | undefined, first_name?: string | undefined, last_name?: string | undefined, middle_name?: string | undefined}; export type ContactPhone = {phone?: string | undefined, type?: string | undefined, wa_id?: string | undefined}; export type ContactEmail = {email?: string | undefined, type?: string | undefined}; export type ContactAddress = {street?: string | undefined, city?: string | undefined, state?: string | undefined, zip?: string | undefined, country?: string | undefined, country_code?: string | undefined, type?: string | undefined}; export type Contact = {name?: ContactName | undefined, phones?: Array | undefined, emails?: Array | undefined, addresses?: Array | undefined}; export type ContactsMessage = {type: 'contacts', userId: string, botId?: string | undefined, contacts: Array}; export type LocationRequestMessage = {type: 'location_request', userId: string, botId?: string | undefined, text: string}; export type CTAUrlParameters = {url: string, display_text: string}; export type CTAUrlMessage = {type: 'cta_url', userId: string, botId?: string | undefined, title: string, text: string, caption: string, parameters: CTAUrlParameters}; export type VoiceCallParameters = {display_text: string}; export type VoiceCallMessage = {type: 'voice_call', userId: string, botId?: string | undefined, header?: string | undefined, text: string, parameters: VoiceCallParameters}; export type CreateApiV1BotsBotIdMessagesArgs = {botId: string, body: TextMessage | TextWithOptionsMessage | QuickReplyMessage | ImageMessage | AudioMessage | VideoMessage | FileMessage | StickerMessage | LocationMessage | ContactsMessage | LocationRequestMessage | CTAUrlMessage | VoiceCallMessage}; export type CreateApiV1BotsBotIdMessagesOptions = {baseUrl?: string | undefined, basicAuth?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const messageResponse = z.object({success: z.boolean().optional(), messageId: z.string().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/bots/{botId}/messages */ export const createApiV1BotsBotIdMessages = async (args: CreateApiV1BotsBotIdMessagesArgs, options?: CreateApiV1BotsBotIdMessagesOptions) => { const {baseUrl = "https://api.jelou.ai", 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 (basicAuth !== undefined) headers.set('authorization', `Basic ${btoa(`${basicAuth.username}:${basicAuth.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/bots/${encodeURIComponent(String(args.botId))}/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(messageResponse, await res.json()) };