简体   繁体   English

AWS Lambda函数执行多次(无服务器)

[英]AWS Lambda Function Executing Multiple Times (Serverless)

I'm in the process of creating a facebook messenger bot using AWS Lambda and the Serverless framework. 我正在使用AWS Lambda和无服务器框架创建facebook messenger机器人。 For now I just want it to repeat whatever is sent right back to the user. 现在我只想让它重复发送给用户的任何内容。 Here is the code: 这是代码:

'use strict';
var https = require('https');
const axios = require('axios');


var VERIFY_TOKEN = "VERIFY";
var PAGE_ACCESS_TOKEN = "TOKEN";

module.exports.hello = (event, context, callback) => {
    const response = {
        statusCode: 200,
        body: JSON.stringify({
            message: 'Go Serverless v1.0! Your function executed successfully!',
            input: event,
        }),
    };

    callback(null, response);

    // Use this code if you don't use the http event with the LAMBDA-PROXY integration
    // callback(null, { message: 'Go Serverless v1.0! Your function executed successfully!', event });
};

// Receive user messages
module.exports.botReply = (event, context, callback) => {

    var data = JSON.parse(event.body);
    console.log("BOT REPLY")

    // Make sure this is a page subscription
    if (data.object === 'page') {

        // Iterate over each entry - there may be multiple if batched
        data.entry.forEach(function(entry) {
            var pageID = entry.id;
            var timeOfEvent = entry.time;
            // Iterate over each messaging event
            entry.messaging.forEach(function(msg) {
                if (msg.message) {
                    console.log("received message");
                    const payload = {
                    recipient: {
                      id: msg.sender.id
                    },
                    message: {
                      text: "test"
                    }
                  };
                    const url = "https://graph.facebook.com/v2.6/me/messages?access_token=" + PAGE_ACCESS_TOKEN;
                    axios.post(url, payload).then((response) => callback(null, response));

                } else {
                    console.log("Webhook received unknown event: ", event);
                    var response = {
                        'body': "ok",
                        'statusCode': 200
                    };

                    callback(null, response);
                }
            });
        });
    }

}

So the bot does successfully echo back the messages, but in my logs I can see it getting executed multiple times. 因此机器人成功回显消息,但在我的日志中,我可以看到它被多次执行。 Sometimes the message has no "message" key in the JSON for some reason, so the multiple executions have different outcomes. 有时,由于某种原因,消息在JSON中没有“消息”键,因此多次执行会产生不同的结果。 I believe it has something to do with me sending the message back to the user because when I comment out the axios.post, the problem stops. 我认为这与我将消息发送回用户有关,因为当我注释掉axios.post时,问题就会停止。 Any idea why this is happening? 知道为什么会这样吗?

@Asanka figured it out in the comments. @Asanka在评论中发现了这一点。 Basically, facebook was sending multiple events over that I had not accounted for in my code. 基本上,Facebook发送了多个事件,而我的代码中没有考虑到这些事件。 Stuff like "message delivered" and "message read" were also events I didn't know about. 像“传递信息”和“阅读信息”这样的东西也是我不知道的事件。 I just had to unsubscribe to them in the developer console. 我只需要在开发者控制台中取消订阅它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将aws-serverless-express与node js一起使用时,我们可以创建多个aws lambda函数吗? - can we create multiple aws lambda function when using aws-serverless-express with node js AWS :: Lambda无服务器调用本地函数不反映我的新wcode - AWS::Lambda serverless invoke local function not reflecting my new wcode 使用 OAuth2 身份验证的无服务器应用程序(AWS Lambda 功能) - Serverless application (AWS Lambda function) using OAuth2 authentication AWS Lambda节点肥皂功能(无服务器框架) - AWS Lambda node-soap function (serverless framework) 如何使用无服务器框架将环境变量传递给AWS Lambda函数? - How to pass an environment variable to an AWS Lambda function using the Serverless framework? 无服务器和 AWS Lambda 的环境变量 - Environment Variables with Serverless and AWS Lambda Sendgrid和AWS Lambda无服务器表单 - Sendgrid and AWS Lambda Serverless Form 无服务器 AWS Lambda 边缘:MalformedHandlerName - Serverless AWS Lambda Edge: MalformedHandlerName 在无服务器中处理 aws lambda 中的错误 - Handling errors in aws lambda in serverless 为什么AWS Dynamo SDK的getItem方法多次执行我的回调函数? - Why is the AWS Dynamo SDK's getItem method executing my callback function multiple times?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM