Reputation: 1568
I'm working with the Facebook Messenger Bot API and I am testing my Heroku hosted Node.js bot with both my Facebook account (admin) and a dummy Facebook account which is added as a tester. Every time I send a message to the ChatBot, the Chatbot is able to reply me. But when I check back the Heroku log, I found out the bot is sending two messages for one message that it received. I have only received one reply from the bot but why is there a second message being sent by the bot?
This is the code for both the post web hook and sendMessage function
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);
}
});
};
This is the log when I built my Chatbot app in Heroku and I sent my message from my admin account
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
This is the second part of the log that show that I sent another message from my dummy account (tester)
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
Edit: I just found that the second user that the bot is repeatedly sending message is actually the chatbot Facebook page itself. Is there a reason why the chatbot is sending the message to itself too?
Upvotes: 1
Views: 1212
Reputation: 21
The issue was that the webhook was subscribed to delivery messages etc.
Upvotes: 1