Twilio调用功能-多用户输入
这是我的代码,当我从test1通过test2时,试图在一个调用中添加更多问题,它不会重定向,它仍然转到test1,因为Twilio调用功能-多用户输入,twilio,Twilio,这是我的代码,当我从test1通过test2时,试图在一个调用中添加更多问题,它不会重定向,它仍然转到test1,因为event.digits存在。如何区分数字以调用新函数 const got = require('got'); exports.handler = function(context, event, callback) { console.log(context); // We can set up our initial TwiML here let twim
event.digits
存在。如何区分数字以调用新函数
const got = require('got');
exports.handler = function(context, event, callback) {
console.log(context);
// We can set up our initial TwiML here
let twiml = new Twilio.twiml.VoiceResponse();
let gather = twiml.gather({
input: 'dtmf',
finishOnKey: '#'
});
if (event.Digits) {
var requestPayload = event;
// The user has entered some digits to answer the question so we post to
// your API and only callback when we get the results
got.post('http://test.com/test.php?test=' + JSON.stringify(requestPayload), {
body: JSON.stringify(event),
headers: {
'accept': 'application/json'
},
json: true
})
.then(function(response) {
test(context,event,callback,twiml,gather);
})
.catch(function(error) {
// Boo, there was an error.
callback(error)
});
} else {
// The user hasn't entered anything yet, so we ask for user ID
gather.play('Please enter user ID');
callback(null, twiml);
}
};
function test2(context,event,callback,twiml,gather){
twiml.say("start recording");
callback(null, twiml);
}
function test(context,event,callback,twiml,gather){
// Check the response and ask your second question here
gather.say("Please enter your case ID and then press star to continue.");
callback(null, twiml);
var requestPayload = event;
// The user has entered some digits to answer the question so we post to
// your API and only callback when we get the results
got.post('http://test.com/test.php?test=' + JSON.stringify(requestPayload), {
body: JSON.stringify(event),
headers: {
'accept': 'application/json'
},
json: true
})
.then(function(response) {
test2(context,event,callback,twiml,gather);
})
.catch(function(error) {
// Boo, there was an error.
callback(error)
});
}
它不会重定向到
test2()
函数。我的代码有问题吗?我需要知道如何使用函数。有没有办法找到一次通话中有多少用户输入?这里是Twilio开发者福音传道者
正如我在上一个问题中所说,要区分答案,可以开始在URL中输入参数。你不需要重定向到一个新函数(我真的是指一个新的Twilio函数,但如果这更容易的话,我们可以在一个函数中完成这一切)
这次我假设Twilio函数的路径是/voice
。我使用的来直接回答同一个Twilio函数,但添加了一个参数来告诉我们在哪个问题上。如果需要,您可以自己进一步扩展,这只是一个示例:
const got = require('got');
exports.handler = function(context, event, callback) {
// We can set up our initial TwiML here
let twiml = new Twilio.twiml.VoiceResponse();
if(event.Digits) {
// We've answered a question, but which one?
// We can set the current question in the URL, so let's retrieve the
// current question, or default to question 1.
const currentQuestion = parseInt(event.currentQuestion, 10) || 1;
let url, question;
if (currentQuestion === 1) {
// If it's question 1 we can do things like set the next question or
// the URL to post the results to.
url = 'http://test.com/question1';
question = 'Enter your case ID';
} else if (currentQuestion == 2) {
// If it's question 2 then we set different options, depending on what
// you need.
url = 'http://test.com/question2';
question = 'What\'s the next question';
} // This could go on.
got.post(url, {
body: JSON.stringify(event),
headers: {
'accept': 'application/json'
},
json: true
})
.then(function(response) {
// When we get a response from the API request we then set up the next
// Gather. This time we do so with an `action` attribute to point back
// to this URL again, but with the currentQuestion incremented.
const gather = twiml.gather({
input: 'dtmf',
finishOnKey: '#',
action: `/voice?currentQuestion=${currentQuestion + 1}`
});
gather.say(question);
callback(null, twiml);
})
.catch(function(error) {
// Boo, there was an error.
callback(error)
});
} else {
// Our first Gather should setup an action to this URL with the
// current question set to 1.
const gather = twiml.gather({
input: 'dtmf',
finishOnKey: '#',
action: `/voice?currentQuestion=1`
});
// The user hasn't entered anything yet, so we ask for user ID
gather.say("Please enter your user ID");
callback(null, twiml);
}
};
让我知道这是否有帮助。这里是Twilio开发者福音传道者 正如我在上一个问题中所说,要区分答案,可以开始在URL中输入参数。你不需要重定向到一个新函数(我真的是指一个新的Twilio函数,但如果这更容易的话,我们可以在一个函数中完成这一切) 这次我假设Twilio函数的路径是
/voice
。我使用的来直接回答同一个Twilio函数,但添加了一个参数来告诉我们在哪个问题上。如果需要,您可以自己进一步扩展,这只是一个示例:
const got = require('got');
exports.handler = function(context, event, callback) {
// We can set up our initial TwiML here
let twiml = new Twilio.twiml.VoiceResponse();
if(event.Digits) {
// We've answered a question, but which one?
// We can set the current question in the URL, so let's retrieve the
// current question, or default to question 1.
const currentQuestion = parseInt(event.currentQuestion, 10) || 1;
let url, question;
if (currentQuestion === 1) {
// If it's question 1 we can do things like set the next question or
// the URL to post the results to.
url = 'http://test.com/question1';
question = 'Enter your case ID';
} else if (currentQuestion == 2) {
// If it's question 2 then we set different options, depending on what
// you need.
url = 'http://test.com/question2';
question = 'What\'s the next question';
} // This could go on.
got.post(url, {
body: JSON.stringify(event),
headers: {
'accept': 'application/json'
},
json: true
})
.then(function(response) {
// When we get a response from the API request we then set up the next
// Gather. This time we do so with an `action` attribute to point back
// to this URL again, but with the currentQuestion incremented.
const gather = twiml.gather({
input: 'dtmf',
finishOnKey: '#',
action: `/voice?currentQuestion=${currentQuestion + 1}`
});
gather.say(question);
callback(null, twiml);
})
.catch(function(error) {
// Boo, there was an error.
callback(error)
});
} else {
// Our first Gather should setup an action to this URL with the
// current question set to 1.
const gather = twiml.gather({
input: 'dtmf',
finishOnKey: '#',
action: `/voice?currentQuestion=1`
});
// The user hasn't entered anything yet, so we ask for user ID
gather.say("Please enter your user ID");
callback(null, twiml);
}
};
如果有任何帮助,请告诉我。谢谢。。。将检查并让您知道:)谢谢您的时间错误在回答第一个问题时说“对不起,发生了未知错误”。好的,我弄坏了一些东西。我自己没有运行这段代码,所以恐怕由你来调试。我给你足够的钱玩了吗?谢谢你。。。将检查并让您知道:)谢谢您的时间错误在回答第一个问题时说“对不起,发生了未知错误”。好的,我弄坏了一些东西。我自己没有运行这段代码,所以恐怕由你来调试。我给你的钱够你玩吗?