2017-07-27 12 views
0

ここに私のコードは、私がtest1からtest2を渡すときに呼び出すには、多くの質問を追加しようとしました、それはリダイレクトされません、event.digitsが存在するので、まだtest1に行く。新しい機能を呼び出すために桁を区別するにはどうすればよいですか?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回の通話でユーザー入力の数を確認する方法はありますか?

答えて

1

ここではTwilioの開発者のエバンジェリストです。

私が前の質問で言ったように、回答を区別するために、URLにパラメータを入れることができます。あなたは新しい機能にリダイレクトする必要はありません(私は本当に新しいTwilio機能を意味しましたが、もっと簡単にするにはこれを1つの中ですべて実行できます)。

今回は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