import {z} from 'zod' export type ElevenLabsVoiceSettings = {type: 'elevenlabs', api_key_ref?: string | undefined}; export type TelnyxVoiceSettings = {type: 'telnyx', voice_speed?: number | undefined}; export type AWSVoiceSettings = {type: 'aws'}; export type MinimaxVoiceSettings = {type: 'minimax', speed?: number | undefined, vol?: number | undefined, pitch?: number | undefined, language_boost?: ('null' | 'auto' | 'Chinese' | 'Chinese,Yue' | 'English' | 'Arabic' | 'Russian' | 'Spanish' | 'French' | 'Portuguese' | 'German' | 'Turkish' | 'Dutch' | 'Ukrainian' | 'Vietnamese' | 'Indonesian' | 'Japanese' | 'Italian' | 'Korean' | 'Thai' | 'Polish' | 'Romanian' | 'Greek' | 'Czech' | 'Finnish' | 'Hindi' | 'Bulgarian' | 'Danish' | 'Hebrew' | 'Malay' | 'Persian' | 'Slovak' | 'Swedish' | 'Croatian' | 'Filipino' | 'Hungarian' | 'Norwegian' | 'Slovenian' | 'Catalan' | 'Nynorsk' | 'Tamil' | 'Afrikaans' | null) | undefined}; export type AzureVoiceSettings = {type: 'azure', api_key_ref?: string | undefined, region?: string | undefined, deployment_id?: string | undefined, effect?: ('eq_car' | 'eq_telecomhp8k') | undefined, gender?: ('Male' | 'Female') | undefined}; export type ResembleSampleRate = '8000' | '16000' | '22050' | '32000' | '44100' | '48000'; export type ResembleVoiceSettings = {type: 'resemble', precision?: ('PCM_16' | 'PCM_24' | 'PCM_32' | 'MULAW') | undefined, sample_rate?: ResembleSampleRate | undefined, format?: ('wav' | 'mp3') | undefined}; export type InworldVoiceSettings = {type: 'inworld', delivery_mode?: ('STABLE' | 'BALANCED' | 'CREATIVE') | undefined}; export type XAIVoiceSettings = {type: 'xai', language?: string | undefined}; export type SonioxVoiceSettings = {type: 'soniox', speed?: number | undefined, reduce_silence?: boolean | undefined}; export type GatherUsingSpeakRequest = {payload: string, invalid_payload?: string | undefined, payload_type?: ('text' | 'ssml') | undefined, service_level?: ('basic' | 'premium') | undefined, voice: string, voice_settings?: (ElevenLabsVoiceSettings | TelnyxVoiceSettings | AWSVoiceSettings | MinimaxVoiceSettings | AzureVoiceSettings | ResembleVoiceSettings | InworldVoiceSettings | XAIVoiceSettings | SonioxVoiceSettings) | undefined, language?: ('arb' | 'cmn-CN' | 'cy-GB' | 'da-DK' | 'de-DE' | 'en-AU' | 'en-GB' | 'en-GB-WLS' | 'en-IN' | 'en-US' | 'es-ES' | 'es-MX' | 'es-US' | 'fr-CA' | 'fr-FR' | 'hi-IN' | 'is-IS' | 'it-IT' | 'ja-JP' | 'ko-KR' | 'nb-NO' | 'nl-NL' | 'pl-PL' | 'pt-BR' | 'pt-PT' | 'ro-RO' | 'ru-RU' | 'sv-SE' | 'tr-TR') | undefined, minimum_digits?: number | undefined, maximum_digits?: number | undefined, maximum_tries?: number | undefined, timeout_millis?: number | undefined, terminating_digit?: string | undefined, valid_digits?: string | undefined, inter_digit_timeout_millis?: number | undefined, client_state?: string | undefined, command_id?: string | undefined}; export type CreateApiCallsCallControlIdActionsGatherUsingSpeakArgs = {call_control_id: string, body: GatherUsingSpeakRequest}; export type CreateApiCallsCallControlIdActionsGatherUsingSpeakOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const callControlCommandResult = z.object({result: z.string().optional()}); export const createApiCallsCallControlIdActionsGatherUsingSpeakResponse = z.object({data: callControlCommandResult.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 }; /** * Gather using speak * * POST /calls/{call_control_id}/actions/gather_using_speak */ export const createApiCallsCallControlIdActionsGatherUsingSpeak = async (args: CreateApiCallsCallControlIdActionsGatherUsingSpeakArgs, options?: CreateApiCallsCallControlIdActionsGatherUsingSpeakOptions) => { const {baseUrl = "https://api.telnyx.com/v2", 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 (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/calls/${encodeURIComponent(String(args.call_control_id))}/actions/gather_using_speak${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(createApiCallsCallControlIdActionsGatherUsingSpeakResponse, await res.json()) };