【问题标题】:Facebook Messenger Bot wait to respond after many request and resumeFacebook Messenger Bot 在多次请求后等待响应并恢复
【发布时间】:2018-02-21 02:40:32
【问题描述】:

当我在我的 Facebook 机器人上写字母时,我的机器人会等待响应。

我写例如:我写“a”......机器人响应“a”,我写“b”......机器人响应“b”,等等......

但是,例如在字母“l”上,机器人等待响应,并在大约几分钟后恢复:

大约几分钟后,机器人会响应:

我用的是官方代码,来自https://developers.facebook.com/docs/messenger-platform/guides/quick-start/

app.js

app.get('/webhook', function(req, res) {
    if (req.query['hub.mode'] === 'subscribe' &&
        req.query['hub.verify_token'] === CONFIGURATION.webhook_token) {
        console.log("Validating webhook");
        res.status(200).send(req.query['hub.challenge']);
    } else {
        console.error("Failed validation. Make sure the validation tokens match.");
        res.sendStatus(403);
    }
});

app.post('/webhook', function (req, res) {
    let data = req.body;

    // 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) {
            let pageID = entry.id;
            let timeOfEvent = entry.time;

            // Iterate over each messaging event
            entry.messaging.forEach(function(event) {
                if (event.message) {
                    receivedMessage(event);
                } else {
                    console.log("Webhook received unknown event: ", event);
                }
            });
        });

        // Assume all went well.
        //
        // You must send back a 200, within 20 seconds, to let us know
        // you've successfully received the callback. Otherwise, the request
        // will time out and we will keep trying to resend.
        res.sendStatus(200);
    }
});

function receivedMessage(event) {
    let senderID = event.sender.id;
    let recipientID = event.recipient.id;
    let timeOfMessage = event.timestamp;
    let message = event.message;

    console.log("Received message for user %d and page %d at %d with message:",
        senderID, recipientID, timeOfMessage);
    console.log(JSON.stringify(message));

    let messageId = message.mid;

    let messageText = message.text;
    let messageAttachments = message.attachments;

    if (messageText) {

        // If we receive a text message, check to see if it matches a keyword
        // and send back the example. Otherwise, just echo the text we received.
        switch (messageText) {
            case 'generic':
                sendGenericMessage(senderID);
                break;

            default:
                sendTextMessage(senderID, messageText);
        }
    } else if (messageAttachments) {
        sendTextMessage(senderID, "Message with attachment received");
    }
}

function sendGenericMessage(recipientId) {
    let messageData = {
        recipient: {
            id: recipientId
        },
        message: {
            attachment: {
                type: "template",
                payload: {
                    template_type: "generic",
                    elements: [{
                        title: "rift",
                        subtitle: "Next-generation virtual reality",
                        item_url: "https://www.oculus.com/en-us/rift/",
                        image_url: "http://messengerdemo.parseapp.com/img/rift.png",
                        buttons: [{
                            type: "web_url",
                            url: "https://www.oculus.com/en-us/rift/",
                            title: "Open Web URL"
                        }, {
                            type: "postback",
                            title: "Call Postback",
                            payload: "Payload for first bubble",
                        }],
                    }, {
                        title: "touch",
                        subtitle: "Your Hands, Now in VR",
                        item_url: "https://www.oculus.com/en-us/touch/",
                        image_url: "http://messengerdemo.parseapp.com/img/touch.png",
                        buttons: [{
                            type: "web_url",
                            url: "https://www.oculus.com/en-us/touch/",
                            title: "Open Web URL"
                        }, {
                            type: "postback",
                            title: "Call Postback",
                            payload: "Payload for second bubble",
                        }]
                    }]
                }
            }
        }
    };

    callSendAPI(messageData);
}

function sendTextMessage(recipientId, messageText) {
    let messageData = {
        recipient: {
            id: recipientId
        },
        message: {
            text: messageText
        }
    };

    callSendAPI(messageData);
}

function callSendAPI(messageData) {
    request({
        uri: 'https://graph.facebook.com/v2.6/me/messages',
        qs: { access_token: CONFIGURATION.access_token },
        method: 'POST',
        json: messageData

    }, function (error, response, body) {
        if (!error && response.statusCode == 200) {
            let recipientId = body.recipient_id;
            let messageId = body.message_id;

            console.log("Successfully sent generic message with id %s to recipient %s",
                messageId, recipientId);
        } else {
            console.error("Unable to send message.");
            console.error(response);
            console.error(error);
        }
    });
}

let server = app.listen(port, function () {
    InterfaceLogin.process();

    console.log('App listening on port 8080!');
});

server.timeout = 1000;

在我的控制台中,在最后一次响应中,我看到:

Received message for user xxx and page xxx at xxx with message:
{"mid":"mid.$xxx","seq":xxxx,"text":"k"}
Successfully sent generic message with id mid.$xxx to recipient xxxxx

机器人没有响应,几分钟后我看到了:

Received message for user xxx and page xxx at xxx with message:
{"mid":"mid.$xxx","seq":xxxx,"text":"l"}
Successfully sent generic message with id mid.$xxx to recipient xxxx
Webhook received unknown event:  { sender: { id: 'xxx' },
  recipient: { id: 'xxx' },
  timestamp: xxx,
  delivery: 
   { mids: [ 'mid.$xxx' ],
     watermark: xxx,
     seq: 0 } }

机器人响应

我最后的回复是:

Received message for user xxx and page xxx at xxx with message:
{"mid":"mid.$xxx","seq":xx,"text":"k"}
Successfully sent generic message with id mid.$xxx to recipient xxx
Received message for user xxx and page xxx at xxx with message:
{"mid":"mid.$xxx","seq":xx,"text":"l"}
Successfully sent generic message with id mid.$xxx to recipient xxxx
Webhook received unknown event:  { sender: { id: 'xxx' },
  recipient: { id: 'xxx' },
  timestamp: xxx,
  delivery: 
   { mids: [ 'mid.$xxx' ],
     watermark: xxx,
     seq: 0 } }

【问题讨论】:

  • 问题是您的机器人正在等待响应,还是接收到的 webhook 事件延迟导致您的机器人响应?
  • @amuramoto 机器人响应但在 x 请求后有延迟
  • 所以机器人发送了一条消息,但它需要时间才能出现在对话中?
  • @amuramoto 我更新了我的帖子,我不知道机器人是否会立即返回消息,但对话中需要时间才能得到答案
  • @amuramoto 我用 console.log 更新了我的帖子

标签: node.js facebook bots facebook-messenger facebook-messenger-bot


【解决方案1】:

messenger bot 平台有速率限制,如果超过它,请求将排队等待稍后执行。

https://developers.facebook.com/docs/messenger-platform/reference/send-api/

【讨论】:

    猜你喜欢
    • 2016-09-08
    • 1970-01-01
    • 2019-08-02
    • 1970-01-01
    • 2018-10-12
    • 2014-04-24
    • 2013-11-18
    • 2018-11-13
    • 1970-01-01
    相关资源
    最近更新 更多