使用Twilio Gather获取西班牙语语音回复并推送至Webhook的问题排查
问题:通话语音回复转录并推送Webhook配置遗漏点
我编写了如下代码,期望将通话中的语音回复转录为文本并推送至指定Webhook,但目前仅能在通话结束时收到通话详情。我需要收集问卷的语音回复,请问遗漏了哪些配置?
const fromPhoneNumber = '+52xxxxxxx'; const toPhoneNumber = '+52xxxx'; const audioUrl = 'https://twilioaudiohcp.s3.us-east-2.amazonaws.com/tono.mp3'; const VoiceResponse = require('twilio').twiml.VoiceResponse; const response = new VoiceResponse(); const gather = response.gather({ input: 'speech', language: 'es-MX', action: 'https://hook.us1.make.com/8w93jvblq7kel319zvtdkqbqv8mgbwu5/response', }); gather.say('Please speak your response after the beep.'); const twiml = `<Response><Play>${audioUrl}</Play></Response>`; client.calls .create({ twiml: twiml, from: fromPhoneNumber, to: toPhoneNumber, record: true, language: 'es-MX', transcribe: true, statusCallback: 'https://hook.us1.make.com/8w93jvblq7kel319zvtdkqbqv8mgbwu5', statusCallbackEvent: ['completed'], statusCallbackMethod: 'POST' }) .then(call => { console.log(call.sid); }) .catch(error => { console.error('Failed to make the call:', error); }); // Convert the response to XML string const xmlString = response.toString(); console.log(xmlString);
核心问题与遗漏配置
- 未启用构建好的语音收集逻辑:你已经通过
VoiceResponse构建了包含语音收集的XML,但创建通话时传入的是仅播放音频的twiml变量,导致Twilio完全未执行语音收集流程。需将client.calls.create中的twiml参数替换为xmlString。 - Gather未开启转录功能:当前
gather对象没有配置transcribe: true,添加该属性后,Twilio会自动将用户语音回复转录为文本,并通过action指定的Webhook推送结果。 - 缺少语音收集控制参数:建议补充
speechTimeout: 'auto'(自动识别用户停止说话的时间)和speechModel: 'phone_call'(适配通话场景的转录模型),提升语音收集的准确率和体验。 - 可选:扩展状态回调事件:如果需要监控通话全流程事件,可将
statusCallbackEvent扩展为['initiated', 'ringing', 'answered', 'completed'],但核心问题仍聚焦在语音收集逻辑的启用。
修正后的关键代码片段:
// 构建带转录的语音收集逻辑 const response = new VoiceResponse(); const gather = response.gather({ input: 'speech', language: 'es-MX', action: 'https://hook.us1.make.com/8w93jvblq7kel319zvtdkqbqv8mgbwu5/response', transcribe: true, speechTimeout: 'auto', speechModel: 'phone_call' }); gather.say('Please speak your response after the beep.'); // 播放提示音 gather.play(audioUrl); const xmlString = response.toString(); // 创建通话时传入正确的Twiml client.calls .create({ twiml: xmlString, from: fromPhoneNumber, to: toPhoneNumber, record: true, language: 'es-MX', statusCallback: 'https://hook.us1.make.com/8w93jvblq7kel319zvtdkqbqv8mgbwu5', statusCallbackEvent: ['completed'], statusCallbackMethod: 'POST' })
内容的提问来源于stack exchange,提问作者Pedro Padilla
相关产品推荐
相关产品推荐

