这是我的代码
exports.handler = function(context, event, callback) {
let twiml = new Twilio.twiml.VoiceResponse();
let gather = twiml.gather({
input: 'dtmf',
finishOnKey: '#'
});
gather.play("Please enter your user ID");
callback(null, twiml);
var got = require('got');
var requestPayload = event;
got.post('http://www.test.com/test.php?test=' + JSON.stringify(requestPayload), {
body: JSON.stringify(requestPayload),
headers: {
'accept': 'application/json'
},
json: true
})
.then(function(response) {
console.log(response.body)
callback(null, response.body);
})
.catch(function(error) {
callback(error)
})
};
我从网址得到了成功的回复,但我需要问第二个问题。如何从这里继续
由于
答案 0 :(得分:1)
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();
let gather = twiml.gather({
input: 'dtmf',
finishOnKey: '#'
});
if(event.Digits) {
// 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://www.test.com/test.php', {
body: JSON.stringify(event),
headers: {
'accept': 'application/json'
},
json: true
})
.then(function(response) {
// Check the response and ask your second question here
gather.say("Please enter your case ID.");
callback(null, twiml);
})
.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.say("Please enter your user ID");
callback(null, twiml);
}
};
让我知道这对你有用。您可能会发现,如果您需要做更多的工作,那么单个函数不是最好的想法,您应该将用户引导到新函数以在输入案例ID后继续调用。
让我知道这是否有帮助。