Twilio call function - Always repeating “Please enter your ID”?

╄→гoц情女王★ 提交于 2019-12-08 13:57:53

问题


Here's my code

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)
        })


};

I got successful response from url , but i need to ask second question . How to continue from here.

Thanks


回答1:


Twilio developer evangelist here.

If you want to use just the one function for this, then you need it to do different things depending on whether the user has just called or if they have entered some digits.

I've rearranged your code a bit and left comments to guide you:

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);
  }
};

Let me know how that works for you. You might find that if you then need to do even more work that a single Function is not the best idea here and you should direct the user to a new Function to continue the call after the case ID is entered.

Let me know if this helps at all.



来源:https://stackoverflow.com/questions/45348637/twilio-call-function-always-repeating-please-enter-your-id

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!