问题
Here's my code, tried to add more questions in a call when I pass test2 from test1, it won't redirect, it still goes to test1 because event.digits exist. How can I differentiate Digits to call to new function?
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)
});
}
It won't redirect to test2() function. Is there any issue in my code? I need to know how to use functions. Is there any way to find how many user inputs in a single call?
回答1:
Twilio developer evangelist here.
As I said in my previous question, to differentiate between answers you can start putting parameters in the URL. You don't need to redirect to a new function (I really meant a new Twilio Function, but we can do this all within one if that's going to be easier).
This time around I assumed the path for your Twilio Function was /voice. I use the action attribute of the <Gather> TwiML to direct answers to the same Twilio Function, but add a parameter to tell which question we are on. You can extend this further yourself if you need, this is just an example:
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);
}
};
Let me know if this helps at all.
来源:https://stackoverflow.com/questions/45351943/twilio-call-function-multiple-user-input