我正在尝试进行一项包含多个问题的调查,其中的响应将通过语音提供,并且必须转录为文本并发送到网络钩子,以供稍后处理。
所有 3 个问题均已重现,但一旦其中一个问题得到解答,通话就会结束,并且仅向 Webhook 发送 1 个响应,而不是全部 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));
要实现多问题调查,其中每个响应都被转录并发送到您的 webhook,您需要 链接问题,以便按顺序询问这些问题,而不是在同一调用中一次性询问所有问题。
在您当前的设置中,所有问题都在单个 TwiML 响应中提出,因此,当第一个
<Gather>
接收输入时,它会处理该输入并结束,而无需等待其他响应。
要解决此问题,请尝试使用
action
动词的 <Gather>
属性,该属性允许您指定 Twilio 在收到 <Gather>
输入后将数据发送到的 URL。然后,您可以使用 TwiML 响应该 Webhook 以提出下一个问题。
以下是如何设置 TwiML 应用程序以一次提出一个问题的概念示例:
询问第一个问题并使用
action
URL 指向您将处理响应并询问后续问题的路线。
当呼叫者回答第一个问题时,Twilio 会将收集的语音输入发送到
action
URL。在服务器端的该路由中,您将处理第一个答案,然后返回 TwiML 以提出第二个问题。
对调查中的每个问题重复此过程。