【问题标题】:AWS Lambda - How handle the order of request processing in a functionAWS Lambda - 如何处理函数中的请求处理顺序
【发布时间】:2018-05-22 04:48:22
【问题描述】:

我将 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,
    },
};
}

【问题讨论】:

  • 这取决于你的 confirmIntent 函数 - 你从哪里得到这个?
  • 并且作为一般规则,callback 函数具有错误优先样式 - 这意味着您可能应该执行类似 callback(null, confirmIntent()) 的操作
  • 另外,confirmIntent() 是异步函数吗?
  • 我在主要问题中添加了confirmIntent函数,它不是异步函数。

标签: javascript node.js aws-lambda facebook-chatbot aws-lex


【解决方案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);

【讨论】:

  • 问题是第二个请求在第一个请求之前完成
  • 在这种情况下不会发生,因为在第一个请求完成之前,第二个请求不会开始。注意第二个请求是如何在第一个请求的回调中的。
  • 我试过你的代码,它仍然首先打印快速回复,我想是因为当第二个请求被调用时,它首先记录function (error, response),然后进行 API 调用(打印快速回复按钮fb),然后执行回调中的任何操作。
  • 需要想办法在callback(confirmation)之后发生第二个请求
  • 不,在第一个请求在这里完成之前,第二个请求不会发生。确保您拥有正确的代码。否则,我不知道为什么会这样。
猜你喜欢
  • 2018-01-28
  • 2019-02-28
  • 2022-01-13
  • 2020-06-27
  • 2022-06-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-01-27
相关资源
最近更新 更多