Overview
I'm trying to make a survey with multiple questions where the response will be provided by speech and has to be transcribed to text and sent to a webhook, for later processing.
Problem
All 3 questions are reproduced, but once one of them is answered the call ends and just 1 response is sent to the webhook instead of all 3
I tried the following code in 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));
To achieve a multi-question survey where each response is transcribed and sent to your webhook, you will need to chain the questions so that they are asked sequentially rather than all at once within the same call.
In your current setup, all questions are being asked within a single TwiML reponse, and as a result, when the first
<Gather>receives input, it processes that input and ends without waiting for other responses.To fix this, try using the
actionattribute of the<Gather>verb, which allows you to specify a URL to which Twilio will send the data once the<Gather>input is received. You can then respond to that webhook with TwiML to ask the next question.Here is a conceptual example of how you can set up your TwiML application to ask one question at a time:
Ask the first question and use the
actionURL to point to a route where you will handle the response and ask the subsequent question.When the caller responds to the first question, Twilio will send the collected speech input to the
actionURL. In that route on your server-side, you will process the first answer and then return TwiML to ask the second question.Repeat this process for each question in the survey.