I just started using Twilio and Javascript. I am using Twilio's Function product, and am trying to pass an argument from my voice-ivr to my handle-user-input function. I tried to do so by passing the parameter in the action URL, but that doesn't seem to be working. Any idea as to why it isn't working or if there is a better way to pass arguments between these two functions?
Here is my voice-ivr file:
exports.handler = function (context, event, callback,) {
let twiml = new Twilio.twiml.VoiceResponse();
const gather = twiml.gather({
numDigits: 1,
action: 'handle-user-input?room=0',
hints: 'room 1, room 2',
input: 'speech dtmf',
});
gather.say('Press 1 to go to Room 1 or 2 to go to Room 2.');
twiml.say(`Sorry we couldn't understand you`);
twiml.redirect();
callback(null, twiml);
};
And my handle-user-input file:
function getUrlVars() {
var vars = {};
var parts = window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi, function(m,key,value) {
vars[key] = value;
});
return vars;
}
exports.handler = function (context, event, callback) {
let UserInput = event.Digits || event.SpeechResult;
let twiml = new Twilio.twiml.VoiceResponse();
// No User Input
if (!UserInput) {
twiml.say('Sorry something went wrong. Please call again');
return callback(null, twiml);
}
if (UserInput.length > 1) {
if (UserInput.toLowerCase().includes('room 1')) {
UserInput = '1';
} else if (UserInput.toLowerCase().includes('room 2')) {
UserInput = '2';
}
}
var room_number = getUrlVars()["room"];
// User Input is a digit: move to according room
switch (room_number) {
case 1:
switch (UserInput) {
case '1':
twiml.say("Thank you. You will now be forwarded to room 2.")
twiml.redirect('room-2');
break;
case '2':
twiml.say('Thank you. You will now be forwarded to the home.');
twiml.redirect('voice-ivr');
break;
default:
twiml.say('We are sorry, we did not recognize your option. Please try again.');
twiml.redirect('room-1');
}
break;
case 2:
switch (UserInput) {
case '1':
twiml.say("Thank you. You will now be forwarded to the home.")
twiml.redirect('voice-ivr');
break;
default:
twiml.say('We are sorry, we did not recognize your option. Please try again.');
twiml.redirect('room-2');
}
break;
default:
twiml.say("Default case");
switch (UserInput) {
case '1':
twiml.say("Thank you. You will now be moved to Room 1.")
twiml.redirect('room-1');
break;
case '2':
twiml.say('You are now entering Room 2.');
twiml.redirect('room-2');
break;
default:
twiml.say('We are sorry, we did not recognize your option. Please try again.');
twiml.redirect('voice-ivr');
}
}
callback(null, twiml);
}
Thanks in advance for your help.