botkit icon indicating copy to clipboard operation
botkit copied to clipboard

Receiving duplicate messages on bot.reply

Open liornaar opened this issue 10 years ago • 32 comments

Hi, Does anyone else encounter this issue? Whenever i do bot.reply i get duplicate message:

image

also in logs i see:

debug: CUSTOM FIND CONVO 1180150065384201 1180150065384201 debug: I HEARD [ 'hi', 'hello', 'bot', 'whats up', 'sup' ] debug: No handler for stats:heard_trigger debug: No handler for tick debug: GOT A MESSAGE HOOK debug: RECEIVED MESSAGE debug: CUSTOM FIND CONVO 1180150065384201 1180150065384201 debug: I HEARD [ 'hi', 'hello', 'bot', 'whats up', 'sup' ] debug: No handler for stats:heard_trigger debug: WEBHOOK SUCCESS { recipient_id: '1180150065384201', message_id: 'mid.1478100520671:324459a102' } debug: WEBHOOK SUCCESS { recipient_id: '1180150065384201', message_id: 'mid.1478100520939:d3d430cb89' }

when sending only one message...

liornaar avatar Nov 02 '16 15:11 liornaar

Can you submit a simple code snippet with a bot that performs this behaviour?

Also, which integration are you using? and what version?

agamrafaeli avatar Nov 03 '16 14:11 agamrafaeli

Yes, Sorry for lack of details.. I'm using the bot with Facebook Messenger It happened since i started using Botkit.. even when I just pasted the code example from the readme file although im using my own express server

Botkit version: └── [email protected]

var Botkit = require('botkit');
var controller = Botkit.facebookbot({
        access_token: process.env.access_token,
        verify_token: process.env.verify_token,
})

var bot = controller.spawn({
});

//using my own server
server.listen(443, function () {
    console.log('gonna try to add web hook . . .');
    controller.createWebhookEndpoints(app, bot, function () {
        console.log('ONLINE!');
    });
});

// this is triggered when a user clicks the send-to-messenger plugin
controller.on('facebook_optin', function(bot, message) {

    bot.reply(message, 'Welcome to my app!');

});

// user said hello
controller.hears(['hello'], 'message_received', function(bot, message) {

    bot.reply(message, 'Hey there.');

});

controller.hears(['cookies'], 'message_received', function(bot, message) {

    bot.startConversation(message, function(err, convo) {

        convo.say('Did someone say cookies!?!!');
        convo.ask('What is your favorite type of cookie?', function(response, convo) {
            convo.say('Golly, I love ' + response.text + ' too!!!');
            convo.next();
        });
    });
});

liornaar avatar Nov 03 '16 14:11 liornaar

  1. Hmmm you said this only happens with bot.reply, does it happen on every code block using a bot.reply, or just some? Does it happen every time within those offending code blocks, or intermittently?
  2. EDIT: This is inaccurate, whenever a message comes in botkit looks to see if its part of a conversation, hence the FIND CONVO in logs. Can you share the code and corresponding logs for a duplicate reply? To my understanding, you should only see CUSTOM FIND CONVO in logs within a conversation, and the code you shared here does not use a conversation to handle a hello.
  3. Can you confirm what events your webhook is subscribed to? I thought for a minute maybe it was responding to a message_echo event which your webhook can subscribe to, but that shouldn't allow a second message to be sent, since it will fail while trying to send the bot itself a message. However, since echos are still received as messages (which may be a bug, will look into it) it could be the echo itself triggering a response. I realize that does not explain the same message being sent twice, but could be a factor

jonchurch avatar Nov 04 '16 00:11 jonchurch

Thanks Jonathan, these are the details: 1)It happens every time im using bot.reply 3) these are the facebook events the app is registered to:

image

this is the full code snippet:


var Botkit = require('botkit');
var express = require('express');
var https = require('https');
var bodyParser = require('body-parser');
var request = require('request');


const fs = require('fs');

const options = {
    key: fs.readFileSync('server.key', 'utf8'),
    cert: fs.readFileSync('server.crt', 'utf8'),
    ca: [fs.readFileSync('server.ca-bundle', 'utf8')]
};

// --------------------- Bot Configuration ----------------------- //

var controller = Botkit.facebookbot({
    debug: true,
    access_token: 'my-access-token',
    verify_token: 'my-varify-token',
})

var bot = controller.spawn({});

// --------------------- Bot Configuration End ----------------------- //


// --------------------- Server Configuration ----------------------- //
var app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: true}));

var server = https.createServer(options, app);

server.listen(443, function () {
    console.log('gonna try to add web hook . . .');
    controller.createWebhookEndpoints(app, bot, function () {
        console.log('ONLINE!');
    });
});

request.post('https://graph.facebook.com/me/subscribed_apps?access_token=' + 'my-access-token',
    function (err, res, body) {
        if (err) {
            console.log('Could not subscribe to page messages');
        } else {
            console.log('Successfully subscribed to Facebook events:', body);
            controller.startTicking();
        }
    });

// --------------------- Server Configuration End ----------------------- //

var title;
var artist;

// this is triggered when a user clicks the send-to-messenger plugin
controller.on('facebook_optin', function (bot, message) {
    bot.say(message, 'Welcome!');
});

// user said hello
controller.hears(['hi', 'hello', 'bot', 'whats up', 'sup'], ['message_received'], function (bot, message) {

    bot.reply(message, 'Hi there!');
});

When sending messages like this:

image

the debug screen looks like this:

image

You can see already in the debug logs that it seems like the app receives two messages... something in Botkit configuration? Should I update or reinstall Botkit?

Thanks for the help!

liornaar avatar Nov 04 '16 08:11 liornaar

You might want to try

controller.hears(['hi', 'hello', 'bot', 'whats up', 'sup'], 'message_received', function(bot, message) {
    bot.reply(message, 'Hi there!');
});

Razvan-Miereanu avatar Nov 06 '16 02:11 Razvan-Miereanu

changing ['message_received'] to 'message_received' didn't seem to do anything different...

liornaar avatar Nov 06 '16 09:11 liornaar

The reason of the issue is the 'message_received' event. It triggers with a lot of user's actions like type a message, and this is the reason you get double reply from bot, because the hear with the 'message_received' triggered 2 times: the first when you are typing the word 'hello', and the second when you press enter and send the word 'hello'. ~~Just change the ['message_received'] for ['direct_message','direct_mention','mention']~~

ghost avatar Nov 08 '16 12:11 ghost

When I do that this is what I get in debug

debug: No handler for message_received

liornaar avatar Nov 08 '16 12:11 liornaar

~~As i said, i really don't like 'message_received' because it is triggered with a lot of actions. DON'T USE IT. Use~~

controller.hears(['hi', 'hello', 'bot', 'whats up', 'sup'], ['direct_message','direct_mention','mention'], function (bot, message) {
    bot.reply(message, 'Hi there!');
});

ghost avatar Nov 08 '16 12:11 ghost

This is what I did, and that's what I get in the debug output when sending a message... and no message is sent back...

liornaar avatar Nov 08 '16 12:11 liornaar

Can you try this one:

controller.hears(['quick'], 'message_received', function(bot, message) {

    bot.reply(message, {
        text: 'Hey! This message has some quick replies attached.',
        quick_replies: [
            {
                "content_type": "text",
                "title": "Yes",
                "payload": "yes",
            },
            {
                "content_type": "text",
                "title": "No",
                "payload": "no",
            }
        ]
    });

});

What do you get?

Also have you tried updating your botkit framework to 0.4.2 ?

alexbudin avatar Nov 24 '16 04:11 alexbudin

@EloyRedondo does ['direct_message','direct_mention','mention'] is supposed to work only for Slack? In Facebook we don't have any direct_mention or mention

thierryskoda avatar Dec 15 '16 02:12 thierryskoda

@thierryskoda Yes, I was wrong... :(

ghost avatar Dec 15 '16 07:12 ghost

This happens to me as well - with a Slack bot. It doesn't always happen - it usually happens after a while the bot is already running. Just bumped into this and will try to collect more details and update.

itamarro avatar Jan 02 '17 21:01 itamarro

Happening here using Facebook Messenger.

The only way around this that I've found is to detect the User ID of the bot and force it to ignore it in a hears()

A few ways to get the ID.

Print the id for all users, when the bot repeats it'll show it's own ID and you can block it

Bot.hears('.*', 'message_received', function(speech, message) {
    console.log(message.user); // when the bot repeats the message it'll output it's ID

    if (message.user == 234234234234) return;

    speech.reply(message, 'Hello');
});

Another option if you're using Messenger.com is to look at the URL when you're talking to the bot eg https://www.messenger.com/t/1333481079742333 where 1333481079742333 is the ID

Dan-Wood avatar Jan 15 '17 07:01 Dan-Wood

So, have been experiencing this with a Slack bot and unsure how to reproduce consistently or what the cause is. Below is the debug log output. What happened is that the user sent "here?" once, but the bot heard it three times.

Any insights would be appreciated.

0|newsbot_ | debug: RECEIVED MESSAGE 0|newsbot_ | debug: CUSTOM FIND CONVO U033SK4SX D1JKYB259 0|newsbot_ | debug: DEFAULT SLACK MSG RECEIVED RESPONDER 0|newsbot_ | debug: No handler for user_typing 0|newsbot_ | debug: RECEIVED MESSAGE 0|newsbot_ | debug: CUSTOM FIND CONVO U033SK4SX D1JKYB259 0|newsbot_ | debug: DEFAULT SLACK MSG RECEIVED RESPONDER 0|newsbot_ | debug: No handler for user_typing 0|newsbot_ | debug: RECEIVED MESSAGE 0|newsbot_ | debug: CUSTOM FIND CONVO U033SK4SX D1JKYB259 0|newsbot_ | debug: DEFAULT SLACK MSG RECEIVED RESPONDER 0|newsbot_ | debug: No handler for user_typing 0|newsbot_ | debug: RECEIVED MESSAGE 0|newsbot_ | debug: CUSTOM FIND CONVO U033SK4SX D1JKYB259 0|newsbot_ | debug: DEFAULT SLACK MSG RECEIVED RESPONDER 0|newsbot_ | debug: I HEARD [ /^here/i, /are you there/i, /are you here/i ] 0|newsbot_ | debug: SAY { text: 'For you - always.', channel: 'D1JKYB259' } 0|newsbot_ | debug: chat.postMessage { type: 'message', 0|newsbot_ | channel: 'D1JKYB259', 0|newsbot_ | text: 'For you - always.', 0|newsbot_ | username: null, 0|newsbot_ | parse: null, 0|newsbot_ | link_names: null, 0|newsbot_ | attachments: null, 0|newsbot_ | unfurl_links: null, 0|newsbot_ | unfurl_media: null, 0|newsbot_ | icon_url: null, 0|newsbot_ | icon_emoji: null, 0|newsbot_ | as_user: true, 0|newsbot_ | token: '' } 0|newsbot_ | info: ** API CALL: https://slack.com/api/chat.postMessage 0|newsbot_ | debug: No handler for heard_trigger 0|newsbot_ | debug: RECEIVED MESSAGE 0|newsbot_ | debug: CUSTOM FIND CONVO U033SK4SX D1JKYB259 0|newsbot_ | debug: DEFAULT SLACK MSG RECEIVED RESPONDER 0|newsbot_ | debug: I HEARD [ /^here/i, /are you there/i, /are you here/i ] 0|newsbot_ | debug: SAY { text: 'For you - always.', channel: 'D1JKYB259' } 0|newsbot_ | debug: chat.postMessage { type: 'message', 0|newsbot_ | channel: 'D1JKYB259', 0|newsbot_ | text: 'For you - always.', 0|newsbot_ | username: null, 0|newsbot_ | parse: null, 0|newsbot_ | link_names: null, 0|newsbot_ | attachments: null, 0|newsbot_ | unfurl_links: null, 0|newsbot_ | unfurl_media: null, 0|newsbot_ | icon_url: null, 0|newsbot_ | icon_emoji: null, 0|newsbot_ | as_user: true, 0|newsbot_ | token: '' } 0|newsbot_ | info: ** API CALL: https://slack.com/api/chat.postMessage 0|newsbot_ | debug: No handler for heard_trigger 0|newsbot_ | debug: RECEIVED MESSAGE 0|newsbot_ | debug: CUSTOM FIND CONVO U033SK4SX D1JKYB259 0|newsbot_ | debug: DEFAULT SLACK MSG RECEIVED RESPONDER 0|newsbot_ | debug: I HEARD [ /^here/i, /are you there/i, /are you here/i ] 0|newsbot_ | debug: SAY { text: 'For you - always.', channel: 'D1JKYB259' } 0|newsbot_ | debug: chat.postMessage { type: 'message', 0|newsbot_ | channel: 'D1JKYB259', 0|newsbot_ | text: 'For you - always.', 0|newsbot_ | username: null, 0|newsbot_ | parse: null, 0|newsbot_ | link_names: null, 0|newsbot_ | attachments: null, 0|newsbot_ | unfurl_links: null, 0|newsbot_ | unfurl_media: null, 0|newsbot_ | icon_url: null, 0|newsbot_ | icon_emoji: null, 0|newsbot_ | as_user: true, 0|newsbot_ | token: '***' } 0|newsbot_ | info: ** API CALL: https://slack.com/api/chat.postMessage 0|newsbot_ | debug: No handler for heard_trigger 0|newsbot_ | debug: RECEIVED MESSAGE 0|newsbot_ | debug: CUSTOM FIND CONVO U1JMPP23U D1JKYB259 0|newsbot_ | debug: DEFAULT SLACK MSG RECEIVED RESPONDER 0|newsbot_ | debug: RECEIVED MESSAGE 0|newsbot_ | debug: CUSTOM FIND CONVO U1JMPP23U D1JKYB259 0|newsbot_ | debug: DEFAULT SLACK MSG RECEIVED RESPONDER 0|newsbot_ | debug: RECEIVED MESSAGE 0|newsbot_ | debug: CUSTOM FIND CONVO U1JMPP23U D1JKYB259 0|newsbot_ | debug: DEFAULT SLACK MSG RECEIVED RESPONDER 0|newsbot_ | debug: RECEIVED MESSAGE 0|newsbot_ | debug: CUSTOM FIND CONVO U1JMPP23U D1JKYB259 0|newsbot_ | debug: DEFAULT SLACK MSG RECEIVED RESPONDER 0|newsbot_ | debug: RECEIVED MESSAGE 0|newsbot_ | debug: CUSTOM FIND CONVO U1JMPP23U D1JKYB259 0|newsbot_ | debug: DEFAULT SLACK MSG RECEIVED RESPONDER 0|newsbot_ | debug: RECEIVED MESSAGE 0|newsbot_ | debug: CUSTOM FIND CONVO U1JMPP23U D1JKYB259 0|newsbot_ | debug: DEFAULT SLACK MSG RECEIVED RESPONDER 0|newsbot_ | debug: RECEIVED MESSAGE 0|newsbot_ | debug: CUSTOM FIND CONVO U1JMPP23U D1JKYB259 0|newsbot_ | debug: DEFAULT SLACK MSG RECEIVED RESPONDER 0|newsbot_ | debug: RECEIVED MESSAGE 0|newsbot_ | debug: CUSTOM FIND CONVO U1JMPP23U D1JKYB259 0|newsbot_ | debug: DEFAULT SLACK MSG RECEIVED RESPONDER 0|newsbot_ | debug: RECEIVED MESSAGE 0|newsbot_ | debug: CUSTOM FIND CONVO U1JMPP23U D1JKYB259 0|newsbot_ | debug: DEFAULT SLACK MSG RECEIVED RESPONDER 0|newsbot_ | debug: Got response null {"ok":true,"channel":"D1JKYB259","ts":"1485560409.001805","message":{"type":"message","user":"U1JMPP23U","text":"For you - always.","bot_id":"B1JMPP21Y","ts":"1485560409.001805"}} 0|newsbot_ | debug: No handler for tick 0|newsbot_ | debug: Got response null {"ok":true,"channel":"D1JKYB259","ts":"1485560409.001806","message":{"type":"message","user":"U1JMPP23U","text":"For you - always.","bot_id":"B1JMPP21Y","ts":"1485560409.001806"}} 0|newsbot_ | debug: Got response null {"ok":true,"channel":"D1JKYB259","ts":"1485560409.001807","message":{"type":"message","user":"U1JMPP23U","text":"For you - always.","bot_id":"B1JMPP21Y","ts":"1485560409.001807"}} 0|newsbot_ | debug: No handler for tick 0|newsbot_ | debug: No handler for tick 0|newsbot_ | debug: RECEIVED MESSAGE 0|newsbot_ | debug: CUSTOM FIND CONVO undefined D1JKYB259 0|newsbot_ | debug: DEFAULT SLACK MSG RECEIVED RESPONDER 0|newsbot_ | debug: No handler for desktop_notification 0|newsbot_ | debug: RECEIVED MESSAGE 0|newsbot_ | debug: CUSTOM FIND CONVO undefined D1JKYB259 0|newsbot_ | debug: DEFAULT SLACK MSG RECEIVED RESPONDER 0|newsbot_ | debug: No handler for desktop_notification 0|newsbot_ | debug: RECEIVED MESSAGE 0|newsbot_ | debug: CUSTOM FIND CONVO undefined D1JKYB259 0|newsbot_ | debug: DEFAULT SLACK MSG RECEIVED RESPONDER 0|newsbot_ | debug: No handler for desktop_notification

itamarro avatar Jan 28 '17 00:01 itamarro

By the way, the above suggested solution for FB Messenger doesn't seem to be applicable to the Slack module. The code there already checks if the message sender is the bot, in Slackbot.js:

                    if (message.user == bot.identity.id && message.bot_id) {
                        return false;
                    }

itamarro avatar Jan 28 '17 01:01 itamarro

@itamarro could it be that bot.identity isn't set? Or message.bot_id for that matter.

Dan-Wood avatar Jan 31 '17 07:01 Dan-Wood

@Dan-Wood bot.identity is set correctly. message.bot_id isn't set, but both messages seem to be coming from the actual user per the user ID in them - it seems like the sender isn't the bot.

I've compared the double messages and sadly they seem almost the same. The only different values are two members of Receiver (member of the WebSocket): expectBuffer and processing. Haven't yet looked at Botkit code to see if this is of any use for me to hack around or understand what's going on, but I'm not too optimistic given that it's the websocket internals. Insights welcome :)

itamarro avatar Feb 13 '17 01:02 itamarro

So, this issue seems related: https://github.com/howdyai/botkit/issues/393

What I'll do is simply store the last TS for each kind of the message my bot handles, if I receive it again with the same TS I'll ignore it. FYI.

itamarro avatar Feb 13 '17 01:02 itamarro