如何解决如何在 twiml.gather 中传递参数?
我刚开始使用 Twilio 和 Javascript。我正在使用 Twilio 的 Function 产品,并试图将参数从我的 voice-ivr
传递到我的 handle-user-input
函数。我试图通过在操作 URL 中传递参数来做到这一点,但这似乎不起作用。知道为什么它不起作用,或者是否有更好的方法在这两个函数之间传递参数?
这是我的 voice-ivr
文件:
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);
};
还有我的 handle-user-input
文件:
function getUrlVars() {
var vars = {};
var parts = window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi,function(m,key,value) {
vars[key] = value;
});
return vars;
}
exports.handler = function (context,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);
}
预先感谢您的帮助。
解决方法
URL 查询参数 ?room=0
被传递到事件对象中的 Twilio 函数(当 Gather 动词调用操作 URL 时),因此您可以使用 event.room
访问它。
此外,由于您没有使用 speech
,您应该将其从 Gather 属性中删除。
您使用的是 Node.js,因此没有 Window 对象。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。