基于Twilio实现外呼语音交互及ASR功能的技术咨询
Great questions! Let’s break down each one with practical, actionable solutions tailored to your use case.
1. Best Approach to Implement the Outbound Voice Interaction
Twilio offers three solid options depending on your need for customization and code:
Twilio Studio (No-Code/Low-Code): Perfect if you want to launch quickly without writing code. Build a visual flow that:
- Triggers outbound calls (via the "Make Outbound Call" widget or REST API)
- Uses the "Say" widget to play your prompt
- Adds a "Gather Input on Call" widget with speech recognition enabled to capture responses
- Branches the flow based on YES/NO/other inputs
Ideal for simple, straightforward flows where you don’t need complex backend integrations.
Twilio Functions + Programmable Voice API: For full control over custom logic (like connecting to your order system or dynamic prompts), this is the way to go. You’ll:
- Use the Twilio REST API to initiate calls, pointing to a Twilio Function as the endpoint that serves TwiML
- Write Functions to generate TwiML with
<Say>for prompts and<Gather>to capture speech - Handle response branching in a separate Function
Twilio Autopilot: If you need advanced conversational capabilities (beyond basic YES/NO, like natural language follow-ups), Autopilot is your best bet. Define intents (e.g., "ConfirmOrder" or "RejectOrder") and build a bot that understands natural speech—no need to manually handle every possible response.
2. Using ASR in Outbound Calls with Twilio Functions
ASR works perfectly for outbound calls—you just need to use the <Gather> verb in the TwiML served when the call connects. Here’s a step-by-step implementation:
First, create a Function to serve the initial prompt and listen for speech:
exports.handler = function(context, event, callback) { const twiml = new Twilio.twiml.VoiceResponse(); // Play your initial prompt twiml.say({ voice: 'alice' }, "Hey, it seems you didn't finish your order. Would you like to finish by phone?"); // Gather speech input and send results to a processing Function twiml.gather({ input: 'speech', action: '/process-response', method: 'POST', timeout: 5 }); // Repeat prompt if no input is received twiml.redirect('/initial-call-handler'); callback(null, twiml); };
Then, create a /process-response Function to handle the customer’s speech:
exports.handler = function(context, event, callback) { const twiml = new Twilio.twiml.VoiceResponse(); const speechResult = event.SpeechResult.toLowerCase(); if (speechResult.includes('yes')) { // Handle YES: proceed with order completion twiml.say("Great! Let's walk through your order details now."); // Add more TwiML for order processing here } else if (speechResult.includes('no')) { // Handle NO: end the call gracefully twiml.say("No problem! You can finish your order online anytime. Have a great day!"); twiml.hangup(); } else { // Handle unrecognized input: repeat the prompt twiml.say("Sorry, I didn't catch that. Would you like to finish your order by phone?"); twiml.redirect('/initial-call-handler'); } callback(null, twiml); };
To trigger the outbound call, use the Twilio REST API (from another Function or your backend):
const client = context.getTwilioClient(); client.calls.create({ url: 'https://your-function-domain.twil.io/initial-call-handler', to: '+5511999999999', // Customer's Brazilian number from: '+1234567890' // Your Twilio number }) .then(call => console.log(`Call started with SID: ${call.sid}`)) .catch(error => console.error(`Error: ${error}`));
3. Supporting Brazilian Portuguese
Twilio fully supports Brazilian Portuguese for both TTS and ASR. Here’s how to adjust your code:
Text-to-Speech (Prompt):
Use the language parameter set to pt-BR and choose a native Brazilian voice (Twilio integrates with Amazon Polly for natural-sounding options):
twiml.say({ language: 'pt-BR', voice: 'Polly.Camila' // Natural female Brazilian Portuguese voice }, 'Olá, parece que você não finalizou seu pedido. Deseja finalizar por telefone?');
Other valid voices: Polly.Vitoria (female) and Polly.Ricardo (male).
Speech Recognition (Listening):
Set the language parameter in <Gather> to pt-BR so Twilio understands Brazilian Portuguese speech:
twiml.gather({ input: 'speech', language: 'pt-BR', action: '/process-response', method: 'POST' });
Update your response handling to check for Portuguese keywords:
if (speechResult.includes('sim') || speechResult.includes('yes')) { twiml.say({ language: 'pt-BR', voice: 'Polly.Camila' }, 'Perfeito! Vamos continuar com o seu pedido.'); } else if (speechResult.includes('não') || speechResult.includes('no')) { twiml.say({ language: 'pt-BR', voice: 'Polly.Camila' }, 'Tudo bem. Se precisar, é só ligar novamente. Até logo!'); }
内容的提问来源于stack exchange,提问作者Rafael Cronemberger




