/** * Handle the first outbound attempt vs automatic recall attempts. * * Recall calls are still **online** SIP sessions (`getScriptPhase` → `'online'`). * Use `context.attempt` (from `dialog.params.attempt`, starts at 0) to branch inside * your single `defineScript` handler — the host does not invoke a separate function. */ import { defineScript, getScriptPhase } from '@voctiv/agent-sdk'; export default defineScript(async ({ channel, context, logger }) => { const phase = getScriptPhase(context); const attempt = context.attempt ?? 0; logger.log('Call leg', { phase, attempt, headless: context.headless, recallCount: context.recallCount, recallDelay: context.recallDelay, }); channel.sip.answer(); if (!context.headless && attempt > 0) { await channel.audio.say( `This is follow-up attempt number ${attempt + 1}. Please stay on the line.`, ); } else if (!context.headless) { await channel.audio.say('Hello, this is our first call to you today.'); } const asr = await channel.createAsr({ language: context.language || 'ru-RU' }); return new Promise((resolve) => { asr.result$.subscribe(async (text) => { if (!text.trim()) return; if (/voicemail|leave a message/i.test(text)) { logger.log('Voicemail detected — host may schedule recall', { attempt }); channel.sip.hangup(); resolve({ output: { outcome: 'voicemail', attempt } }); return; } await channel.audio.say('Thank you. Goodbye.'); channel.sip.hangup(); resolve({ output: { outcome: 'answered', attempt } }); }); channel.events.terminated$.subscribe(() => { asr.destroy(); resolve({ output: { outcome: 'terminated', attempt } }); }); }); });