I am having an error with the message bot api. For some reason it fires multiple like 20 at a time of the messages to a certain user. I am using heroku on node. I haven't the clue why it is doing this? Any help would be much appreciated.

"use strict";
var express = require("express");
var app = express();
var bodyParser = require('body-parser');
var request = require('request');
app.use(bodyParser.json());
app.get('/', function(req, res){
res.send('hello');
});
app.get('/webhook', function (req, res) {
if (req.query['hub.verify_token'] === 'my_voice_is_my_password_verify_me') {
res.send(req.query['hub.challenge'])
}
res.send('Error, wrong token')
})
app.post('/webhook', function (req, res) {
let messaging_events = req.body.entry[0].messaging;
for (let i = 0; i < messaging_events.length; i++) {
let event = req.body.entry[0].messaging[i]
let sender = event.sender.id
if (event.message && event.message.text) {
let text = event.message.text
sendTextMessage(sender, "Text received, echo: " + text.substring(0, 200))
}
}
res.sendStatus(200)
})
function sendTextMessage(sender, text) {
let messageData = { text:text }
request({
url: 'https://graph.facebook.com/v2.6/me/messages',
qs: {access_token: process.env.PAGE_ACCESS_TOKEN},
method: 'POST',
json: {
recipient: {id:sender},
message: messageData,
}
}, function(error, response, body) {
if (error) {
console.log('Error sending messages: ', error)
} else if (response.body.error) {
console.log('Error: ', response.body.error)
}
})
}
app.listen(process.env.PORT || 8080);
It is probably a bug in the Messenger's client. They are having a lot of issues with the Android app. Check this bug and this bug.
I recommend you to use a rate limiter with a low limit -- max 1 request per second, for example -- to solve the issue. This will also prevent possible bugs in the Messenger's clients in the future. Or you can wait for them to solve the problem in the clients, but keep in mind that users with an outdated version will cause trouble to your bot.
Also, consider that you can receive delivery receipts in your message events. Consider to insert the snippet below in your code to ignore these events.
...
for (let i = 0; i < messaging_events.length; i++) {
let event = req.body.entry[0].messaging[i]
if (event.hasOwnProperty('delivery')) {
continue;
}
...
}
...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With