Facebook Messenger Bot API发送多条消息并显示错误

时间:2016-08-25 21:33:00

标签: node.js facebook-graph-api heroku

我正在使用Facebook Messenger Bot API,我正在使用我的Facebook帐户(管理员)和虚拟Facebook帐户测试我的Heroku托管的Node.js机器人,该帐户被添加为测试人员。每次我向ChatBot发送消息时,Chatbot都能够回复我。但是当我检查Heroku日志时,我发现机器人正在为收到的一条消息发送两条消息。我只收到了机器人的一个回复,但为什么机器人会发送第二条消息?

这是post web hook和sendMessage函数的代码

app.post('/webhook', function (req, res) {
var events = req.body.entry[0].messaging;
for (i = 0; i < events.length; i++) {
    var event = events[i];
    var sender = event.sender.id
    if (event.message && event.message.text) {
        sendMessage(sender, {text: "Hey, did you just say: " + event.message.text});
        console.log("sent message to: " + sender);
    }
}
res.sendStatus(200);
});

function sendMessage(recipientId, message) {
request({
    url: 'https://graph.facebook.com/v2.6/me/messages',
    qs: {access_token: process.env.PAGE_ACCESS_TOKEN},
    method: 'POST',
    json: {
        recipient: {id: recipientId},
        message: message,
    }
}, function(error, response, body) {
    if (error) {
        console.log('Error sending message: ', error);
    } else if (response.body.error) {
        console.log('Error: ', response.body.error);
    }
});
}; 

这是我在Heroku中构建我的Chatbot应用程序时的日志,我从管理员帐户发送了消息

2016-08-25T21:16:02.303705+00:00 heroku[web.1]: Restarting
2016-08-25T21:16:02.304448+00:00 heroku[web.1]: State changed from up to      starting
2016-08-25T21:16:03.588649+00:00 heroku[web.1]: Starting process with command `node index.js`
2016-08-25T21:16:05.731964+00:00 heroku[web.1]: Stopping all processes with SIGTERM
2016-08-25T21:16:06.609304+00:00 heroku[web.1]: Process exited with status 143
2016-08-25T21:16:07.266798+00:00 heroku[web.1]: State changed from starting to up
2016-08-25T21:16:39.746253+00:00 app[web.1]: sent message to: 1160285130676682
2016-08-25T21:16:40.082592+00:00 app[web.1]: sent message to: 284635001929051
2016-08-25T21:16:40.206265+00:00 app[web.1]: Error:  { message: '(#100) No matching user found',
2016-08-25T21:16:40.206267+00:00 app[web.1]:   type: 'OAuthException',
2016-08-25T21:16:40.206268+00:00 app[web.1]:   code: 100,
2016-08-25T21:16:40.206268+00:00 app[web.1]:   fbtrace_id: 'BSxdusFbWB6' }
2016-08-25T21:16:39.756640+00:00 heroku[router]: at=info method=POST path="/webhook" host=warm-depths-81393.herokuapp.com request_id=7035f788-f103-49d7-99ec-21cf58d30674 fwd="69.63.188.113" dyno=web.1 connect=1ms service=66ms status=200 bytes=196
2016-08-25T21:16:40.265285+00:00 heroku[router]: at=info method=POST path="/webhook" host=warm-depths-81393.herokuapp.com request_id=71deb2ab-f1db-40cc-a5d1-af926a35b6de fwd="173.252.120.112" dyno=web.1 connect=0ms service=8ms status=200 bytes=196
2016-08-25T21:16:40.084913+00:00 heroku[router]: at=info method=POST path="/webhook" host=warm-depths-81393.herokuapp.com request_id=61be8207-456c-4337-8c2b-ced6ff742f52 fwd="66.220.158.102" dyno=web.1 connect=0ms service=7ms status=200 bytes=196

这是日志的第二部分,显示我从我的虚拟帐户(测试人员)发送了另一条消息

2016-08-25T21:16:44.788335+00:00 app[web.1]: sent message to: 1107394752663896
2016-08-25T21:16:45.195498+00:00 app[web.1]: sent message to: 284635001929051
2016-08-25T21:16:45.307382+00:00 app[web.1]: Error:  { message: '(#100) No matching user found',
2016-08-25T21:16:45.307384+00:00 app[web.1]:   type: 'OAuthException',
2016-08-25T21:16:45.307385+00:00 app[web.1]:   code: 100,
2016-08-25T21:16:45.307385+00:00 app[web.1]:   fbtrace_id: 'GKqZtMZawEv' }
2016-08-25T21:16:45.194037+00:00 heroku[router]: at=info method=POST path="/webhook" host=warm-depths-81393.herokuapp.com request_id=fce94f53-bd7e-4020-a0b0-ff05638e0b25 fwd="173.252.120.121" dyno=web.1 connect=0ms service=12ms status=200 bytes=196
2016-08-25T21:16:44.786566+00:00 heroku[router]: at=info method=POST path="/webhook" host=warm-depths-81393.herokuapp.com request_id=3d650955-c9db-47c2-8b69-b919e70bb94e fwd="173.252.120.100" dyno=web.1 connect=2ms service=4ms status=200 bytes=196
2016-08-25T21:16:45.515394+00:00 heroku[router]: at=info method=POST path="/webhook" host=warm-depths-81393.herokuapp.com request_id=f497c7b2-a9ee-4f9c-83b9-c23a2e358cc1 fwd="66.220.158.111" dyno=web.1 connect=0ms service=2ms status=200 bytes=196

编辑:我刚发现机器人重复发送消息的第二个用户实际上是聊天机器人Facebook页面本身。聊天机器人也在向自己发送消息的原因是什么?

1 个答案:

答案 0 :(得分:1)

问题是webhook订阅了传递消息等。