/** * Funnelforms → AI Phone Agent (Admin\LeadcallerPage): connect ElevenLabs with its key, bring a * number from Twilio, and create agents, like appointment types: what each one wants to reach, * how it sounds, what it asks and when it calls. Forms only link an agent (action aiCall, AiCall.tsx). * The page talks to Http\LeadcallerController. */ import React, { useEffect, useId, useMemo, useRef, useState } from 'react'; import { localeTag } from '@funnelforms/form-core'; import { AI_GOALS, AI_LANGUAGES, disclosure, goalTexts, newAgent, type AiAgent, type AiCallResult, type AiGoal, type AiQuestion, type AiVoice, type LeadcallerApi, type LeadcallerState, type TwilioAccess } from '../aicall'; import { WpRestAdapter } from '../wp-rest'; import { goalName } from './AiCall'; import { useCountries } from './PhoneProps'; import { __, sprintf } from './i18n'; import { Badge, Button, Dialog, Field, Notice, Select, Spinner, TextInput, Toggle } from './ui'; export interface LeadcallerConfig { root: string; nonce: string; userLocale?: string; /** Funnelforms → Forms, where the agent is linked. */ formsUrl?: string; /** Funnelforms → Appointments, tab appointment types. */ typesUrl?: string; /** Funnelforms → Integrations, where calendars are connected. */ calendarsUrl?: string; } const KEYS_URL = 'https://elevenlabs.io/app/settings/api-keys'; const TWILIO_URL = 'https://console.twilio.com/'; const TWILIO_BUY_URL = 'https://console.twilio.com/us1/develop/phone-numbers/manage/search'; const MAX_QUESTIONS = 8; function message( e: unknown ): string { return e instanceof Error ? e.message : String( e ); } /** The language of the page for names of languages and days. */ function pageLocale( userLocale: string | undefined ): string | undefined { return localeTag( userLocale ); } function languageName( code: string, locale: string | undefined ): string { try { const names = new Intl.DisplayNames( locale ? [ locale ] : undefined, { type: 'language' } ); const name = names.of( 'no' === code ? 'nb' : code ) ?? code; return name.charAt( 0 ).toLocaleUpperCase( locale ) + name.slice( 1 ); } catch { return code; } } /** Short name of an ISO day, 1 = Monday. */ function dayName( day: number, locale: string | undefined ): string { return new Date( 2024, 0, day ).toLocaleDateString( locale, { weekday: 'short' } ); } /** "Mon–Fri, 09:00–19:00": the days in runs, then the hours. */ export function windowText( window: AiAgent[ 'window' ], locale: string | undefined ): string { const days = [ ...window.days ].sort( ( a, b ) => a - b ); const runs: string[] = []; for ( let i = 0; i < days.length; ) { let j = i; while ( j + 1 < days.length && ( days[ j + 1 ] ?? 0 ) === ( days[ j ] ?? 0 ) + 1 ) { j++; } const from = dayName( days[ i ] ?? 1, locale ); runs.push( j - i >= 2 ? from + '–' + dayName( days[ j ] ?? 1, locale ) : days.slice( i, j + 1 ).map( ( d ) => dayName( d, locale ) ).join( ', ' ) ); i = j + 1; } return ( runs.length ? runs.join( ', ' ) : __( 'Every day', 'funnelforms-pro' ) ) + ', ' + window.from + '–' + window.to; } /** Whether greeting and questions are still the starting texts of the goal and language. */ function untouched( agent: AiAgent ): boolean { const texts = goalTexts( agent.goal, agent.language ); const asked = agent.questions.map( ( q ) => ( { label: q.label, hint: q.hint } ) ); // An agent that books has no starting questions: the appointment type brings day, time and kind. return texts.greeting === agent.greeting && ( JSON.stringify( texts.questions ) === JSON.stringify( asked ) || ( 'appointment' === agent.goal && ! asked.length ) ); } function goalHint( goal: AiGoal ): string { const hints: Record = { qualify: __( 'Asks about need, budget and timing, so you know how promising the lead is.', 'funnelforms-pro' ), appointment: __( 'Offers free times from your calendar and books the appointment right in the call.', 'funnelforms-pro' ), confirm: __( 'Confirms the request and clears up missing details.', 'funnelforms-pro' ), followUp: __( 'Asks how your offer was received and what is still missing.', 'funnelforms-pro' ), }; return hints[ goal ]; } /** What keeps an agent from calling, in words. */ function missingText( code: string ): string { const texts: Record = { no_number: __( 'Choose the number it calls from.', 'funnelforms-pro' ), no_remote: __( 'Not set up in ElevenLabs yet: save it again.', 'funnelforms-pro' ), }; return texts[ code ] ?? code; } const PATHS = { key: [ 'M2.586 17.414A2 2 0 0 0 2 18.828V21a1 1 0 0 0 1 1h3a1 1 0 0 0 1-1v-1a1 1 0 0 1 1-1h1a1 1 0 0 0 1-1v-1a1 1 0 0 1 1-1h.172a2 2 0 0 0 1.414-.586l.814-.814a6.5 6.5 0 1 0-4-4z', 'M16.5 7.5h.01' ], call: [ 'M22 16.9v3a2 2 0 0 1-2.2 2 19.8 19.8 0 0 1-8.6-3.1 19.5 19.5 0 0 1-6-6A19.8 19.8 0 0 1 2.1 4.2 2 2 0 0 1 4.1 2h3a2 2 0 0 1 2 1.7c.1 1 .4 1.9.7 2.8a2 2 0 0 1-.5 2.1L8.1 9.9a16 16 0 0 0 6 6l1.3-1.3a2 2 0 0 1 2.1-.4c.9.3 1.8.6 2.8.7a2 2 0 0 1 1.7 2z' ], agent: [ 'M12 8V4H8', 'M6 8h12a2 2 0 0 1 2 2v8a2 2 0 0 1-2 2H6a2 2 0 0 1-2-2v-8a2 2 0 0 1 2-2z', 'M2 14h2M20 14h2M15 13v2M9 13v2' ], form: [ 'M15 2H6a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V7z', 'M14 2v4a2 2 0 0 0 2 2h4', 'M10 9H8M16 13H8M16 17H8' ], goal: [ 'M12 22a10 10 0 1 0 0-20 10 10 0 0 0 0 20z', 'M12 18a6 6 0 1 0 0-12 6 6 0 0 0 0 12z', 'M12 14a2 2 0 1 0 0-4 2 2 0 0 0 0 4z' ], voice: [ 'M12 2a3 3 0 0 0-3 3v7a3 3 0 0 0 6 0V5a3 3 0 0 0-3-3z', 'M19 10v2a7 7 0 0 1-14 0v-2M12 19v3' ], says: [ 'M7.9 20A9 9 0 1 0 4 16.1L2 22z' ], asks: [ 'M12 22a10 10 0 1 0 0-20 10 10 0 0 0 0 20z', 'M9.1 9a3 3 0 0 1 5.8 1c0 2-3 3-3 3M12 17h.01' ], calendar: [ 'M8 2v4M16 2v4M3 10h18', 'M5 4h14a2 2 0 0 1 2 2v14a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2V6a2 2 0 0 1 2-2z', 'M9 15l2 2 4-4' ], person: [ 'M19 21v-2a4 4 0 0 0-4-4H9a4 4 0 0 0-4 4v2', 'M12 11a4 4 0 1 0 0-8 4 4 0 0 0 0 8z' ], hours: [ 'M12 22a10 10 0 1 0 0-20 10 10 0 0 0 0 20z', 'M12 6v6l4 2' ], after: [ 'M16 3h5v5', 'M8 3H3v5', 'M12 22v-8.3a4 4 0 0 0-1.172-2.872L3 3', 'm15 9 6-6' ], play: [ 'M6 4l14 8-14 8z' ], stop: [ 'M7 7h10v10H7z' ], check: [ 'M5 12l5 5 9-10' ], }; /** Name of a voice without its tagline: "Marcel - Friendly German Explainer" gives Marcel and the rest. */ export function voiceTitle( name: string ): { title: string; tagline: string } { const [ title = '', ...rest ] = name.split( /\s+[-–—|]\s+/ ); return { title: title.trim() || name, tagline: rest.join( ' – ' ).trim() }; } /** * The voices that fit a call in a language, best first: those of the language, conversational * ones before others. */ export function voicesFor( voices: readonly AiVoice[], language: string ): AiVoice[] { const rank = ( v: AiVoice ): number => ( 'conversational' === v.use ? 0 : 1 ); return voices.filter( ( v ) => v.language === language ).sort( ( a, b ) => rank( a ) - rank( b ) || a.name.localeCompare( b.name ) ); } const VOICE_LIBRARY_URL = 'https://elevenlabs.io/app/voice-library'; function genderName( gender: string ): string { return ( { female: __( 'Female', 'funnelforms-pro' ), male: __( 'Male', 'funnelforms-pro' ), neutral: __( 'Neutral', 'funnelforms-pro' ) } as Record )[ gender ] ?? ''; } /** * The voice of an agent: voices of the call's language first, filters for all voices, female or * male and a search, each with a sample to listen to. */ function VoicePicker( { voices, value, valueName, language, languageLabel, onPick, }: { voices: AiVoice[] | null; value: string; valueName: string; language: string; languageLabel: string; onPick: ( voice: AiVoice | null ) => void; } ) { const uid = useId(); const matching = useMemo( () => voicesFor( voices ?? [], language ), [ voices, language ] ); const [ scope, setScope ] = useState<'language' | 'all'>( 'language' ); const [ gender, setGender ] = useState<'' | 'female' | 'male'>( '' ); const [ query, setQuery ] = useState( '' ); const [ playing, setPlaying ] = useState( '' ); const audio = useRef( null ); useEffect( () => () => audio.current?.pause(), [] ); // Another language shows its voices first again. useEffect( () => setScope( 'language' ), [ language ] ); if ( null === voices ) { return (
); } const listen = ( v: AiVoice ): void => { audio.current?.pause(); if ( playing === v.id ) { setPlaying( '' ); return; } audio.current = new Audio( v.preview ); audio.current.onended = () => setPlaying( '' ); setPlaying( v.id ); void audio.current.play().catch( () => setPlaying( '' ) ); }; const effective = 0 === matching.length ? 'all' : scope; const base = 'language' === effective ? matching : [ ...voices ].sort( ( a, b ) => a.name.localeCompare( b.name ) ); const needle = query.trim().toLowerCase(); const shown = base.filter( ( v ) => ( ! gender || v.gender === gender ) && ( ! needle || `${ v.name } ${ v.accent }`.toLowerCase().includes( needle ) ) ); const current = voices.find( ( v ) => v.id === value ); const row = ( v: AiVoice ) => { const { title, tagline } = voiceTitle( v.name ); const details = [ genderName( v.gender ), tagline || ( 'standard' !== v.accent ? v.accent : '' ) ].filter( Boolean ).join( ' · ' ); return (
  • { 'conversational' === v.use ? { __( 'Good for calls', 'funnelforms-pro' ) } : null } { v.preview ? ( ) : null }
  • ); }; return (
    { matching.length > 0 ? ( ) : null }
    { base.length > 8 ? setQuery( e.target.value ) } /> : null }
    { 0 === matching.length ? (

    { sprintf( /* translators: %s: language, e.g. German */ __( 'Your ElevenLabs account has no voice for %s yet. Every voice speaks every language; for a natural accent add one from the voice library of ElevenLabs, then open this window again.', 'funnelforms-pro' ), languageLabel ) }{ ' ' } { __( 'Open voice library', 'funnelforms-pro' ) }

    ) : null }
      { 'all' === effective && ! gender && ! needle ? (
    • ) : null } { value && ! current ? (
    • ) : null } { /* The chosen voice stays in sight when the filter would hide it. */ } { current && ! shown.includes( current ) ? row( current ) : null } { shown.map( row ) } { 0 === shown.length ?
    • { __( 'No voice matches.', 'funnelforms-pro' ) }
    • : null }

    { __( 'The voices of your ElevenLabs account. Voices that ElevenLabs does not allow for agents are left out.', 'funnelforms-pro' ) }{ ' ' } { __( 'More voices', 'funnelforms-pro' ) }

    ); } function LineIcon( { name }: { name: keyof typeof PATHS } ) { return ( ); } /** A part of the agent editor with its icon and title (the look of the appointment type editor). */ function Part( { icon, title, hint, children }: { icon: keyof typeof PATHS; title: string; hint?: string; children: React.ReactNode } ) { const id = useId(); return (

    { title }

    { hint ?

    { hint }

    : null }
    { children }
    ); } export function LeadcallerScreen( { config, api: given }: { config: LeadcallerConfig; api?: LeadcallerApi } ) { const api = useMemo( () => given ?? new WpRestAdapter( { root: config.root, nonce: config.nonce } ).leadcaller, [ given, config.root, config.nonce ] ); const locale = pageLocale( config.userLocale ); const [ state, setState ] = useState( null ); const [ error, setError ] = useState( null ); const [ said, say ] = useState( '' ); const [ editing, setEditing ] = useState( null ); const [ testing, setTesting ] = useState( null ); const [ addingNumber, setAddingNumber ] = useState( false ); useEffect( () => { let live = true; api.state().then( ( s ) => live && setState( s ), ( e: unknown ) => live && setError( message( e ) ) ); return () => { live = false; }; }, [ api ] ); if ( ! state ) { return error ? (

    { error }

    ) : ( ); } const ready = state.agents.filter( ( a ) => ! a.missing?.length ); const step = ! state.connected || state.error ? 1 : ! state.numbers.length ? 2 : ! state.agents.length ? 3 : 4; const fresh = (): AiAgent => { const agent = newAgent( ( config.userLocale ?? 'de' ).slice( 0, 2 ), state.site ); const only = state.numbers[ 0 ]; if ( 1 === state.numbers.length && only ) { agent.number = only.id; agent.numberLabel = only.label || only.number; agent.numberProvider = 'sip_trunk' === only.provider ? 'sip_trunk' : 'twilio'; } return agent; }; return (

    { said }

    { __( 'Set up in four steps', 'funnelforms-pro' ) }

    { __( 'You do this once. After that every form can use your agents.', 'funnelforms-pro' ) }

    { 4 === step && ready.length ? { __( 'Ready for calls', 'funnelforms-pro' ) } : null }
      0 } current={ 2 === step } icon="call" title={ __( 'Add the phone number the agent calls from', 'funnelforms-pro' ) }> { state.numbers.length ? (
        { state.numbers.map( ( n ) => (
      • { n.number } { n.label && n.label !== n.number ? { n.label } : null } { 'sip_trunk' === n.provider ? 'SIP' : 'Twilio' }
      • ) ) }
      ) : (

      { __( 'The agent needs its own number to call your leads. Buy one at Twilio (with “Voice”); Funnelforms connects it to ElevenLabs for you.', 'funnelforms-pro' ) }

      ) }
      0 } current={ 3 === step } icon="agent" title={ __( 'Create an agent', 'funnelforms-pro' ) }>

      { __( 'Choose what the agent should reach, how it sounds and what it asks. Then call yourself to try it.', 'funnelforms-pro' ) }

      { __( 'In the form builder select the contact step, open “After sending” and add “AI Phone Agent”. Choose the agent; the form needs a phone field and a consent box, which one click adds.', 'funnelforms-pro' ) }

      { config.formsUrl ? ( ) : null }

    { __( 'Agents', 'funnelforms-pro' ) }

    { __( 'Each agent calls new leads of the forms it is linked to, within its call hours.', 'funnelforms-pro' ) }

    { state.agents.length ? ( ) : null }
    { state.agents.length ? (
      { state.agents.map( ( a ) => (
    • { a.name } { windowText( a.window, locale ) } { [ goalName( a.goal ), languageName( a.language, locale ), ( a.voiceName ? voiceTitle( a.voiceName ).title : __( 'Standard voice', 'funnelforms-pro' ) ), a.numberLabel ].filter( Boolean ).join( ' · ' ) } { a.missing?.length ? { a.missing.map( missingText ).join( ' ' ) } : null }
      { a.missing?.length ? { __( 'Not ready', 'funnelforms-pro' ) } : { __( 'Ready', 'funnelforms-pro' ) } }
    • ) ) }
    ) : (

    { __( 'No agents yet', 'funnelforms-pro' ) }

    { state.connected ? __( 'Create your first agent: it takes about two minutes.', 'funnelforms-pro' ) : __( 'Connect ElevenLabs first, then create your first agent.', 'funnelforms-pro' ) }

    ) }

    { __( 'What happens with a new lead', 'funnelforms-pro' ) }

    1. { __( 'The lead sends the form and checks the consent box.', 'funnelforms-pro' ) }
    2. { __( 'A few minutes later, within the call hours, the agent calls. It says first that it is an AI assistant.', 'funnelforms-pro' ) }
    3. { __( 'It asks your questions, one at a time, and leaves out what the form already answers.', 'funnelforms-pro' ) }
    4. { __( 'After the call the entry gets a note with the result and a summary, and each answer as its own detail. If nobody answers, the agent tries again later.', 'funnelforms-pro' ) }

    { sprintf( /* translators: %s: name of the service, ElevenLabs */ __( 'Calls, voice and transcript are processed by %s for you. Mention it in your privacy policy and conclude its data processing agreement.', 'funnelforms-pro' ), 'ElevenLabs' ) }

    { editing ? (
    setAddingNumber( true ) } onClose={ () => setEditing( null ) } onSaved={ ( saved, created ) => { setState( ( s ) => s && { ...s, agents: s.agents.some( ( a ) => a.id === saved.id ) ? s.agents.map( ( a ) => ( a.id === saved.id ? saved : a ) ) : [ ...s.agents, saved ] } ); setEditing( null ); say( sprintf( /* translators: %s: name of the agent */ __( '%s was saved.', 'funnelforms-pro' ), saved.name ) ); if ( created && ! saved.missing?.length ) { setTesting( saved ); } } } onDeleted={ ( gone ) => { setState( ( s ) => s && { ...s, agents: s.agents.filter( ( a ) => a.id !== gone.id ) } ); setEditing( null ); say( sprintf( /* translators: %s: name of the agent */ __( '%s was deleted.', 'funnelforms-pro' ), gone.name ) ); } } />
    ) : null } { addingNumber ? (
    setAddingNumber( false ) } onAdded={ ( next, number ) => { setState( next ); setAddingNumber( false ); say( sprintf( /* translators: %s: phone number */ __( '%s is connected.', 'funnelforms-pro' ), number ) ); } } />
    ) : null } { testing ? (
    setTesting( null ) } />
    ) : null }
    ); } function Step( { n, done, current, icon, title, children }: { n: number; done: boolean; current: boolean; icon: keyof typeof PATHS; title: string; children: React.ReactNode } ) { return (
  • { sprintf( /* translators: %d: number of the step */ __( 'Step %d:', 'funnelforms-pro' ), n ) } { title } { done ? { __( '(done)', 'funnelforms-pro' ) } : null }

    { children }
  • ); } function ConnectStep( { api, state, onState, say }: { api: LeadcallerApi; state: LeadcallerState; onState: ( s: LeadcallerState ) => void; say: ( text: string ) => void } ) { const [ key, setKey ] = useState( '' ); const [ busy, setBusy ] = useState( false ); const [ error, setError ] = useState( null ); const [ changing, setChanging ] = useState( false ); const connect = async (): Promise => { setBusy( true ); setError( null ); try { const next = await api.connect( key.trim() ); onState( next ); setKey( '' ); setChanging( false ); say( __( 'ElevenLabs is connected.', 'funnelforms-pro' ) ); } catch ( e ) { setError( message( e ) ); } finally { setBusy( false ); } }; const disconnect = async (): Promise => { setBusy( true ); try { onState( await api.disconnect() ); say( __( 'ElevenLabs was disconnected.', 'funnelforms-pro' ) ); } catch ( e ) { setError( message( e ) ); } finally { setBusy( false ); } }; if ( state.connected && ! changing ) { return ( <> { state.error ? { state.error } :

    { __( 'Connected. Funnelforms sets up your agents in your ElevenLabs account; you do not have to do anything there.', 'funnelforms-pro' ) }

    } { error ? { error } : null }
    ); } return ( <>

    { __( 'ElevenLabs gives the agent its voice and makes the calls. You only need an API key:', 'funnelforms-pro' ) }

    1. { __( 'Sign in to ElevenLabs and open Developers → API keys.', 'funnelforms-pro' ) }{ ' ' } { __( 'Open API keys', 'funnelforms-pro' ) }
    2. { __( 'Click “Create key”. Allow “ElevenLabs Agents” (write) and “Voices” (read), or leave all access on.', 'funnelforms-pro' ) }
    3. { __( 'Copy the key and paste it here.', 'funnelforms-pro' ) }
    { error ? { error } : null }
    { changing ? ( ) : null }
    ); } function NumberDialog( { api, state, onClose, onAdded }: { api: LeadcallerApi; state: LeadcallerState; onClose: () => void; onAdded: ( state: LeadcallerState, number: string ) => void } ) { const saved = state.twilio[ 0 ]; const [ ref, setRef ] = useState( saved?.ref ?? '' ); const [ sid, setSid ] = useState( '' ); const [ token, setToken ] = useState( '' ); const [ found, setFound ] = useState | null>( null ); const [ chosen, setChosen ] = useState( '' ); const [ busy, setBusy ] = useState( false ); const [ error, setError ] = useState( null ); const name = useId(); const have = new Set( state.numbers.map( ( n ) => n.number ) ); const access: TwilioAccess = ref ? { connectionRef: ref } : { accountSid: sid.trim(), authToken: token.trim() }; const complete = ref ? true : /^AC[0-9a-fA-F]{32}$/.test( sid.trim() ) && token.trim().length >= 16; const load = async (): Promise => { setBusy( true ); setError( null ); try { const list = await api.twilioNumbers( access ); setFound( list ); setChosen( list.find( ( n ) => ! have.has( n.number ) )?.number ?? '' ); } catch ( e ) { setError( message( e ) ); } finally { setBusy( false ); } }; const add = async (): Promise => { setBusy( true ); setError( null ); try { onAdded( await api.importNumber( access, chosen ), chosen ); } catch ( e ) { setError( message( e ) ); setBusy( false ); } }; return ( { found ? ( ) : ( ) } } > { error ? { error } : null } { found ? ( found.length ? (
    { __( 'Which number should the agent call from?', 'funnelforms-pro' ) } { found.map( ( n ) => ( ) ) }
    ) : ( { __( 'In Twilio open Phone Numbers → Buy a number and choose one with “Voice”. Then load your numbers again.', 'funnelforms-pro' ) }{ ' ' } { __( 'Buy a number', 'funnelforms-pro' ) } ) ) : ( <>

    { __( 'ElevenLabs does not sell numbers, Twilio does. Once Funnelforms knows your Twilio access, it connects the number to ElevenLabs for you.', 'funnelforms-pro' ) }

    1. { __( 'Create a Twilio account and buy a number with “Voice” (Phone Numbers → Buy a number). For numbers from Germany Twilio asks for your address and company details first.', 'funnelforms-pro' ) }{ ' ' } { __( 'Buy a number', 'funnelforms-pro' ) }
    2. { __( 'Copy the Account SID and the Auth Token from the start page of the Twilio console (Account Info).', 'funnelforms-pro' ) }{ ' ' } { __( 'Open Twilio console', 'funnelforms-pro' ) }
    { state.twilio.length ? ( set( { name: e.target.value } ) } /> } { tried && nameMissing ?

    { __( 'Please enter a name.', 'funnelforms-pro' ) }

    : null }
    { __( 'What should the call reach?', 'funnelforms-pro' ) } { AI_GOALS.map( ( g ) => ( ) ) }
    { 'appointment' === agent.goal ? ( { types.length ? ( <> ( { value: l, label: languageName( l, locale ) } ) ) } onChange={ ( v ) => retext( { goal: agent.goal, language: v } ) } />
    { __( 'Voice', 'funnelforms-pro' ) } { autoVoice.current = false; set( { voice: v?.id ?? '', voiceName: v?.name ?? '' } ); } } />
    set( { company: v } ) } hint={ __( 'The agent says it calls on behalf of this company.', 'funnelforms-pro' ) } /> set( { greeting: v } ) } hint={ sprintf( /* translators: %s: the first sentence of each call, e.g. "Hello, this is the AI assistant of Example Ltd." */ __( 'Comes right after the sentence the law asks for: “%s”', 'funnelforms-pro' ), disclosure( agent.language, company ) ) } /> set( { about: v } ) } hint={ __( 'In your words, as you would brief a new colleague. The agent follows these notes in every call; the rules of the law and of Funnelforms always come first.', 'funnelforms-pro' ) } />
      { agent.questions.map( ( q, i ) => (
    1. setQuestion( i, { label: v } ) } hint={ __( 'Short, as it should appear in the entry, e.g. “Budget”.', 'funnelforms-pro' ) } /> setQuestion( i, { hint: v } ) } />
    2. ) ) }
    { agent.questions.length < MAX_QUESTIONS ? ( ) : null }
    { state.numbers.length ? ( ( { value: z, label: z.replace( /_/g, ' ' ) } ) ) ] } onChange={ ( v ) => set( { timezone: v } ) } hint={ __( 'Call hours, call backs and the time the agent knows go by this time zone.', 'funnelforms-pro' ) } /> set( { delayMinutes: Number( v ) } ) } hint={ __( 'after the form was sent', 'funnelforms-pro' ) } /> set( { retryMinutes: Number( v ) } ) } /> : null }

    { __( 'If the lead has no time and names another day and time, the agent calls again then, within the call hours.', 'funnelforms-pro' ) }

    set( { transfer: v } ) } hint={ agent.transfer.trim() && ! e164( agent.transfer ) ? __( 'With the country code, e.g. +49 30 1234567.', 'funnelforms-pro' ) : __( 'Optional. When the lead wants to talk to your team right away, the agent connects the call to this number, for example your office.', 'funnelforms-pro' ) } /> String( n ) ) } onChange={ ( v ) => set( { dailyLimit: Number( v ) } ) } hint={ __( 'Protects your ElevenLabs and Twilio credit.', 'funnelforms-pro' ) } /> ) : null }
    ); } /** A number as typed, in the form +4917012345678, or null. */ export function e164( typed: string ): string | null { const n = typed.replace( /[\s()./-]/g, '' ).replace( /^00/, '+' ); return /^\+[1-9][0-9]{6,14}$/.test( n ) ? n : null; } function TestCallDialog( { api, agent, onClose }: { api: LeadcallerApi; agent: AiAgent; onClose: () => void } ) { const [ to, setTo ] = useState( '' ); const [ name, setName ] = useState( '' ); const [ busy, setBusy ] = useState( false ); const [ error, setError ] = useState( null ); const [ conversation, setConversation ] = useState( null ); const [ result, setResult ] = useState( null ); const number = e164( to ); useEffect( () => { if ( ! conversation ) { return; } let live = true; let tries = 0; let timer: ReturnType | undefined; const poll = (): void => { api.callResult( conversation, agent.id ).then( ( r ) => { if ( ! live ) { return; } if ( r.done || ++tries > 200 ) { setResult( r ); } else { timer = setTimeout( poll, 4000 ); } }, ( e: unknown ) => live && setError( message( e ) ) ); }; timer = setTimeout( poll, 4000 ); return () => { live = false; if ( timer ) { clearTimeout( timer ); } }; }, [ api, agent.id, conversation ] ); const call = async (): Promise => { if ( ! number ) { return; } setBusy( true ); setError( null ); try { setConversation( ( await api.testCall( agent.id, number, name.trim() ) ).conversation ); } catch ( e ) { setError( message( e ) ); } finally { setBusy( false ); } }; return ( { __( 'Close', 'funnelforms-pro' ) } ) : ( <> ) } > { error ? { error } : null } { ! conversation ? ( <>

    { __( 'The agent calls you right away, also outside its call hours, and acts as if you had just sent a form. Afterwards you see here what would be written to the entry.', 'funnelforms-pro' ) }

    { 'appointment' === agent.goal && agent.bookingType ?

    { __( 'It offers the real free times of the appointment type, but a test call books nothing.', 'funnelforms-pro' ) }

    : null } ) : ! result ? ( ) : ( ) }
    ); } function CallResult( { result }: { result: AiCallResult } ) { if ( ! result.done ) { return { __( 'The call is still running or ElevenLabs has no result yet. You find it later in your ElevenLabs account under Conversations.', 'funnelforms-pro' ) }; } return (
    { ! result.reached ? ( { __( 'Not reached: nobody answered, or the mailbox did. With a lead the agent would try again later.', 'funnelforms-pro' ) } ) : true === result.goal ? ( { __( 'Goal reached. This is what the entry would get:', 'funnelforms-pro' ) } ) : ( { false === result.goal ? __( 'Call finished, goal not reached. This is what the entry would get:', 'funnelforms-pro' ) : __( 'Call finished. This is what the entry would get:', 'funnelforms-pro' ) } ) } { result.summary ? (

    { __( 'Summary', 'funnelforms-pro' ) }: { result.summary }

    ) : null } { result.details?.length ? (
    { result.details.map( ( d ) => (
    { d.label }
    { d.value }
    ) ) }
    ) : null } { result.transcript?.length ? (
    { __( 'Conversation', 'funnelforms-pro' ) } { result.transcript.map( ( line, i ) => (

    { 'agent' === line.role ? __( 'Agent', 'funnelforms-pro' ) : __( 'Lead', 'funnelforms-pro' ) }: { line.text }

    ) ) }
    ) : null }
    ); }