import {z} from 'zod' export type ServiceConversationMessageEnumWebhookEnabledType = 'true' | 'false'; export type CreateApiV1ServicesChatServiceSidConversationsConversationSidMessagesArgs = {ChatServiceSid: string, ConversationSid: string, 'X-Twilio-Webhook-Enabled'?: ServiceConversationMessageEnumWebhookEnabledType | undefined, body: {Author?: string | undefined, Body?: string | undefined, DateCreated?: string | undefined, DateUpdated?: string | undefined, Attributes?: string | undefined, MediaSid?: string | undefined, ContentSid?: string | undefined, ContentVariables?: string | undefined, Subject?: string | undefined}}; export type CreateApiV1ServicesChatServiceSidConversationsConversationSidMessagesOptions = {baseUrl?: string | undefined, accountSidAuthToken?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const conversationsV1ServiceServiceConversationServiceConversationMessage = z.object({account_sid: z.string().min(34).max(34).nullable().optional(), chat_service_sid: z.string().min(34).max(34).nullable().optional(), conversation_sid: z.string().min(34).max(34).nullable().optional(), sid: z.string().min(34).max(34).nullable().optional(), index: z.number().int().optional(), author: z.string().nullable().optional(), body: z.string().nullable().optional(), media: z.array(z.unknown()).nullable().optional(), attributes: z.string().nullable().optional(), participant_sid: z.string().min(34).max(34).nullable().optional(), date_created: z.string().nullable().optional(), date_updated: z.string().nullable().optional(), delivery: z.unknown(), url: z.string().nullable().optional(), links: z.object({}).nullable().optional(), content_sid: z.string().min(34).max(34).nullable().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 }; /** * Add a new message to the conversation in a specific service * * POST /v1/Services/{ChatServiceSid}/Conversations/{ConversationSid}/Messages */ export const createApiV1ServicesChatServiceSidConversationsConversationSidMessages = async (args: CreateApiV1ServicesChatServiceSidConversationsConversationSidMessagesArgs, options?: CreateApiV1ServicesChatServiceSidConversationsConversationSidMessagesOptions) => { const {baseUrl = "https://conversations.twilio.com", fetch: fetchFn = fetch, accountSidAuthToken} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/x-www-form-urlencoded") headers.set('accept', "application/json") if (args['X-Twilio-Webhook-Enabled'] != null) headers.set("X-Twilio-Webhook-Enabled", String(args['X-Twilio-Webhook-Enabled'])) if (accountSidAuthToken !== undefined) headers.set('authorization', `Basic ${btoa(`${accountSidAuthToken.username}:${accountSidAuthToken.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/Services/${encodeURIComponent(String(args.ChatServiceSid))}/Conversations/${encodeURIComponent(String(args.ConversationSid))}/Messages${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: new URLSearchParams(Object.entries(args.body).flatMap(([key, value]) => (value == null ? [] : [[key, String(value)]])))}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(conversationsV1ServiceServiceConversationServiceConversationMessage, await res.json()) };