1

我正在将 AWS Lambda (Node.js 8.1) 用于与 facebook 集成的 AWS Lex 聊天机器人,并且想知道如何让机器人首先打印出 confirmIntent 响应(请求 1),然后打印快速回复(请求 2 )。

我尝试了各种方法,例如将“请求 2”添加到与 confirmIntent 函数相同的回调中,但快速回复总是首先打印。我知道这个问题是因为 NodeJS 是异步的,但仍然不知道如何解决它。

function greeting(intentRequest, callback) {
const hello = intentRequest.currentIntent.slots.Hello;
const sessionAttributes = intentRequest.sessionAttributes || {};
const confirmationStatus = intentRequest.currentIntent.confirmationStatus;

var params = null;

var options = {
            uri: 'https://graph.facebook.com/v2.6/'+PSID+'?fields=first_name,last_name,gender&access_token='+process.env.FB_access_token,
            method: 'GET',
            headers: {
                'Content-Type': 'application/json'
            }
        };

var options_2 = {
            uri: 'https://graph.facebook.com/v2.6/me/messages?access_token='+process.env.FB_access_token,
            body: JSON.stringify({
                recipient: {"id":PSID},
                message: {
                    text: "Here is a quick reply!",
                    quick_replies: [
                            {
                                content_type: "text",
                                title: "Search",
                                payload: "yes",
                                image_url: "http://example.com/img/red.png"
                            },
                            {
                                content_type: "location"
                            }
                        ]
                }
            }),
            method: 'POST',
            headers: {
                'Content-Type': 'application/json'
            }
        };

/*request 1 - Get FB user data*/          
request(options, function (error, response, body) {
    console.log(error,response.body);

    body = JSON.parse(body);

    params = {
        first_name: body['first_name'],
    };
    callback(confirmIntent(sessionAttributes, 'GetStarted', 
        {
            Age: null,
        },
        { contentType: 'PlainText', content: 'Hi ' +params.first_name+ ', I will guide you through a series of questions to select your own customized product. Are you ready to start?' }));
});

/*request 2 - Quick replies*/
request(options_2, function (error, response) {
            console.log(error,response.body);
        });
}

我的 confirmIntent 功能

function confirmIntent(sessionAttributes, intentName, slots, message) {
return {
    sessionAttributes,
    dialogAction: {
        type: 'ConfirmIntent',
        intentName,
        slots,
        message,
    },
};
}
4

1 回答 1

1
/*request 1 - Get FB user data*/

request(options, function(error, response, body) {
  console.log(error, response.body);

  body = JSON.parse(body);

  params = {
    first_name: body["first_name"]
  };

  const confirmation = confirmIntent(
    sessionAttributes,
    "GetStarted",
    {
      Age: null
    },
    {
      contentType: "PlainText",
      content:
        "Hi " +
        params.first_name +
        ", I will guide you through a series of questions to select your own customized product. Are you ready to start?"
    }
  );

  /*request 2 - Quick replies*/
  request(options_2, function(error, response) {
    console.log(error, response.body);
    callback(null, confirmation);
  });
});

您可以这样做以确保第二个请求总是在第一个请求之后发生。

如果你有 Node Carbon,你也可以async await这样使用:

/*request 1 - Get FB user data*/
const request = require('request-native-promise') //need this for promises (async await are basically promises)
const response1 = await request(options);
const body = JSON.parse(response1.body);

params = {
    first_name: body["first_name"]
};

const confirmation = confirmIntent(
    sessionAttributes,
    "GetStarted", {
        Age: null
    }, {
        contentType: "PlainText",
        content: "Hi " +
            params.first_name +
            ", I will guide you through a series of questions to select your own customized product. Are you ready to start?"
    }
);

callback(null, confirmation);

/*request 2 - Quick replies*/
const response2 = await request(options_2);
于 2018-05-22T05:38:45.190 回答