概述
我正试图使多个问题的回答将通过语音提供的调查,并已被转录为文本,并发送到一个webhook,为以后的处理.
问题
所有3个问题都被复制,但是一旦其中一个被回答,调用就结束了,只有1个响应被发送到webhook,而不是所有3个
我在JS中尝试了以下代码
const audioFiles = [
{ url: 'https://twilioaudiohcp.s3.us-east-2.amazonaws.com/1.mp3', question: 'Q1' },
{ url: 'https://twilioaudiohcp.s3.us-east-2.amazonaws.com/2.mp3', question: 'Q2' },
{ url: 'https://twilioaudiohcp.s3.us-east-2.amazonaws.com/3.mp3', question: 'Q3' },
];
const webhookEndpoint = 'webhook';
const phoneNumberList = JSON.parse(fs.readFileSync('test.json', 'utf8')).map(item => item.celular);
const makeCalls = async () => {
let callCount = 0;
for (const phoneNumber of phoneNumberList) {
if (callCount >= 40) {
await new Promise(resolve => setTimeout(resolve, 60000));
callCount = 0;
}
const response = new twilio.twiml.VoiceResponse();
for (const audioFile of audioFiles) {
// Play the audio
response.play(audioFile.url);
response.say(audioFile.question, { voice: 'alice', language: 'es-MX' });
response.gather({
input: 'speech',
language: 'es-MX',
action: webhookEndpoint,
speechtimeout: 1,
method: 'POST',
});
response.pause({ length: 1 });
}
const twiml = response.toString();
await client.calls.create({
twiml: twiml,
from: fromPhoneNumber,
to: `+52${phoneNumber}`,
record: true,
transcribe: true,
language: 'es-MX',
})
.then(call => {
console.log('Call initiated:', call.sid);
callCount++;
})
.catch(error => {
console.error('Failed to make the call:', error);
});
}
};
makeCalls()
.then(() => console.log('All calls completed.'))
.catch(error => console.error('An error occurred:', error));
字符串
1条答案
按热度按时间4szc88ey1#
为了实现一个多问题的调查,其中每个回答都被转录并发送到您的webhook,您需要将问题链接起来,以便顺序提问,而不是在同一个调用中同时提问。
在您当前的设置中,所有问题都在一个TwiML响应中被询问,因此,当第一个
<Gather>
接收到输入时,它会处理该输入并结束,而无需等待其他响应。要解决这个问题,请尝试使用
<Gather>
动词的action
属性,该属性允许您指定一个URL,一旦收到<Gather>
输入,Twilio将向该URL发送数据。然后您可以使用TwiML响应该webhook以询问下一个问题。下面是一个概念性的例子,说明如何设置TwiML应用程序,使其一次只问一个问题:
1.询问第一个问题,并使用
action
URL指向一个路由,您将在该路由中处理响应并询问后续问题。1.当呼叫者回答第一个问题时,Twilio会将收集到的语音输入发送到
action
URL。在服务器端的该路由中,您将处理第一个答案,然后返回TwiML来询问第二个问题。1.对调查中的每个问题重复此过程。