import {z} from 'zod' export type SendTemplateMessageContentTemplateDataHeaderDtoV2 = {type?: ('TEXT' | 'IMAGE' | 'DOCUMENT' | 'VIDEO') | undefined, mediaUrl?: string | undefined}; export type SendTemplateMessageContentTemplateDataBodyDtoV2 = {placeholders: Array<{[key: string]: string}>}; export type SendTemplateMessageContentTemplateDataButtonDto = {type: 'URL', parameter?: string | undefined}; export type SendTemplateMessageContentTemplateCarouselCardsDtoV2 = {cardIndex?: number | undefined, components?: ({header?: ({type?: ('IMAGE' | 'VIDEO') | undefined, mediaUrl?: string | undefined}) | undefined, body?: SendTemplateMessageContentTemplateDataBodyDtoV2 | undefined, buttons?: Array | undefined}) | undefined}; export type SendTemplateMessageContentTemplateDataDtoV2 = {header?: SendTemplateMessageContentTemplateDataHeaderDtoV2 | undefined, body?: SendTemplateMessageContentTemplateDataBodyDtoV2 | undefined, buttons?: Array | undefined, cards?: Array | undefined}; export type SendTemplateMessageContentDtoV2 = {templateName: string, language: 'en' | 'en_US' | 'en_GB' | 'pt_BR' | 'es' | 'id', templateData?: SendTemplateMessageContentTemplateDataDtoV2 | undefined}; export type SendWhatsappTemplateMessageDtoV2 = {from?: string | undefined, to?: string | undefined, groupId?: string | undefined, content: SendTemplateMessageContentDtoV2, customData?: {[key: string]: string} | undefined}; export type SendWhatsappTemplateMessagesDtoV2 = {messages: Array, byPassMediaUrlValidation?: boolean | undefined}; export type CreateApiV2WhatsappMessageTemplateArgs = {body: SendWhatsappTemplateMessagesDtoV2}; export type CreateApiV2WhatsappMessageTemplateOptions = {baseUrl?: string | undefined, basic?: string | undefined, fetch?: typeof fetch | undefined}; export const sendWhatsappTemplateMessageResponseV2 = z.object({messages: z.array(z.object({status: z.enum(["SENT", "FAILED", "ENQUEUED"]).optional(), recipient: z.string().optional(), messageId: z.string().optional(), errorMessage: z.string().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 Whatsapp Template Message (V2) * * POST /v2/whatsapp/message/template */ export const createApiV2WhatsappMessageTemplate = async (args: CreateApiV2WhatsappMessageTemplateArgs, options?: CreateApiV2WhatsappMessageTemplateOptions) => { 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(/\/+$/, '')}/v2/whatsapp/message/template${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(sendWhatsappTemplateMessageResponseV2, await res.json()) };