2017-07-27 53 views
0

這裏是我的代碼,試圖在test1中通過test2時添加更多問題,它不會重定向,它仍然會去test1,因爲event.digits存在。如何區分數字以呼叫新功能?Twilio調用函數 - 多用戶輸入

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()函數。我的代碼有問題嗎?我需要知道如何使用函數。有什麼方法可以在單個呼叫中查找多少個用戶輸入?

回答

1

Twilio開發者佈道者在這裏。

正如我在上一個問題中所說的,要區分答案,您可以開始在URL中輸入參數。你不需要重定向到一個新的功能(我真的意味着一個新的Twilio功能,但如果這將更容易,我們可以在一個內部完成)。

這次我假定你的Twilio函數的路徑是/voice。我使用<Gather> TwiMLaction attribute將答案指​​向相同的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); 
    } 
}; 

讓我知道這是否有幫助。

+0

謝謝... ...將檢查並讓你知道烏拉圭回合時間:)謝謝 –

+0

錯誤說:「很抱歉,出現未知錯誤」,在回答第一個問題 –

+0

好吧,我打碎了什麼東西,然後。我沒有自己運行這些代碼,所以恐怕要進行調試了。我給你足夠的玩嗎? – philnash