import {z} from 'zod' export type SendTemplateMessageContentTemplateDataHeaderDto = {type?: ('TEXT' | 'IMAGE' | 'DOCUMENT' | 'VIDEO' | 'LOCATION') | undefined, placeholder?: string | undefined, mediaUrl?: string | undefined, filename?: string | undefined, latitude?: number | undefined, longitude?: number | undefined}; export type SendTemplateMessageContentTemplateDataBodyDto = {placeholders: Array}; export type SendTemplateMessageContentTemplateDataButtonDto = {type: 'URL', parameter?: string | undefined}; export type SendTemplateMessageContentTemplateCarouselCardsDto = {cardIndex?: number | undefined, components?: ({header?: ({type?: ('IMAGE' | 'VIDEO') | undefined, mediaUrl?: string | undefined}) | undefined, body?: SendTemplateMessageContentTemplateDataBodyDto | undefined, buttons?: Array | undefined}) | undefined}; export type SendTemplateMessageContentTemplateDataDto = {header?: SendTemplateMessageContentTemplateDataHeaderDto | undefined, body?: SendTemplateMessageContentTemplateDataBodyDto | undefined, buttons?: Array | undefined, cards?: Array | undefined}; export type SendTemplateMessageContentDto = {templateName: string, language: 'en' | 'en_US' | 'en_GB' | 'pt_BR' | 'es' | 'id', templateData?: SendTemplateMessageContentTemplateDataDto | undefined}; export type SendWhatsAppBroadcastRequestDto = {groupName: string, from?: unknown | undefined, content?: SendTemplateMessageContentDto | undefined}; export type CreateApiWhatsappMessageBroadcastArgs = {body: SendWhatsAppBroadcastRequestDto}; export type CreateApiWhatsappMessageBroadcastOptions = {baseUrl?: string | undefined, basic?: string | undefined, fetch?: typeof fetch | undefined}; export const sendWhatsAppBroadcastResponseDto = z.object({status: z.enum(["SENT", "FAILED"]), messageId: z.string(), errorMessage: 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 Template Whatsapp Message to Broadcast Group * * POST /whatsapp/message/broadcast */ export const createApiWhatsappMessageBroadcast = async (args: CreateApiWhatsappMessageBroadcastArgs, options?: CreateApiWhatsappMessageBroadcastOptions) => { const {baseUrl = "https://public.doubletick.io", fetch: fetchFn = fetch, basic} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (basic !== undefined) headers.set("Authorization", basic) const url = `${baseUrl.replace(/\/+$/, '')}/whatsapp/message/broadcast${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(sendWhatsAppBroadcastResponseDto, await res.json()) };