Twilio呼叫功能 - 多用户输入

时间:2017-07-27 13:16:19

标签: twilio

这是我的代码,当我从test1传递test2时,尝试在调用中添加更多问题,它不会重定向,因为{ "_id" : ObjectId("5979dbd157634341fc8cb03a"), "count" : 2, "room_status" : true, "room_id" : "s7d8a9s7d8a90sd", "users" : [ { "user_status" : true, "user_id" : 1501158353821 }, { "user_id" : 1501158361890, "user_status" : false }, { "user_id" : 1501158369102, "user_status" : true } ], "__v" : 0 } 存在,它仍然会转到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 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) }); } 功能。我的代码中有任何问题吗?我需要知道如何使用函数。有没有办法在一次通话中找到多少用户输入?

1 个答案:

答案 0 :(得分:2)

Twilio开发者传道者在这里。

正如我在上一个问题中所说,要区分答案,您可以开始在URL中添加参数。你不需要重定向到一个新函数(我真的是一个新的Twilio函数,但如果这样做更容易,我们可以在一个函数内完成这一任务。)

这次我假设你的Twilio函数的路径是/voice。我使用action attribute<Gather> TwiML来指导相同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);
  }
};

让我知道这是否有帮助。

相关问题