botframework-sdk icon indicating copy to clipboard operation
botframework-sdk copied to clipboard

Bot replies twice at the start of the conversation

Open digitalbath opened this issue 8 years ago • 7 comments

Bot Info

  • SDK Platform: Node.js
  • SDK Version: botbuilder 3.13.1
  • Active Channels: Facebook Messenger
  • Deployment Environment: Azure Bot Service also local development with Emulator

Issue Description

Our Bot is currently hosted on the free Azure pricing tier and so it seems the server goes to sleep when it's not being used. This means that the first time a conversation is initiated, when the server is down, there is a delay while it starts up (fair enough).

This delay allows the user to send multiple messages before the bot can respond. Once the bot does respond however, it seems to begin the welcome dialog twice and so each message in the dialog, is sent twice.

This behaviour can also be replicated by saying hi twice in very quick succession to the bot using the emulator or setting a breakpoint somewhere in the default dialog and sending another message once the first breakpoint is hit.

This may seem like an edge case issue but it's causing us problems getting the app approved by facebook. We do plan on upgrading the server before we go live but we're unable to do that right now.

Code Example

var bot = new builder.UniversalBot(connector, function (session) {
  //this function gets called twice for the same user
  session.beginDialog('welcome:/');
});

Reproduction Steps

  1. run any Sample in the emulator. like this one
  2. copy 'hi ' to the clipboard.
  3. paste 'hi ' into the emulator, hit return, paste 'hi ' again then hit return again.

Expected Behavior

single, initial response from the bot.

Actual Results

The bot replies twice, repeating it's self.

screen shot 2018-08-28 at 15 58 53

digitalbath avatar Aug 28 '18 15:08 digitalbath

Hello @digitalbath. I'd like to clarify a few things about your issue.

  1. Are you trying to have the bot start the conversation by sending the first message? Or when you talk about the welcome dialog do you just mean the root dialog that responds to the first message from the user?
  2. Are you trying to prevent all duplicate messages or are you only concerned with the welcome dialog?
  3. When you say "it seems to begin the welcome dialog twice and so each message in the dialog, is sent twice," it sounds like you're saying after you trigger a double message one time you will continue to get double messages even when you only send one message. Is that what you mean? I ask because your screenshot doesn't appear to show that behavior.
  4. In your screenshot, we see the pattern "user user A A user B." Are you expecting to see "user user A B user B" or just "user user A user B"?

v-kydela avatar Aug 28 '18 23:08 v-kydela

Hi @v-kydela,

Thanks for looking at this.

  1. No. The user currently initiates the conversation by saying "Hi" although I suppose ideally the welcome dialog should be triggered when the facebook user presses the "Get Started" button. Is this possible?

  2. The duplicate messages only occur on the first dialog. It seems like two sessions are started and then messages are sent back by each session but then when you respond you just respond to the latest session and so it's fine after that.

  3. No, it's just the first dialog that repeats but if that dialog has multiple messages, they will all come through twice.

  4. I suppose the desired pattern should be "user user A user B".

digitalbath avatar Aug 30 '18 10:08 digitalbath

Hi again @digitalbath. Based on the context of what you're saying, I think when you say dialog you might mean dialog step. Remember that a single dialog can consist of a lot of back and forth between the bot and the user. When you say dialog it sounds like you're just talking about one set of consecutive messages from the bot to the user.

I'm not sure what you mean by the "Get Started" button. It is possible to have the bot start a conversation but it varies from channel to channel. When you're working with a channel that provides a conversationUpdate event then you can have the bot respond to that, but you need to be careful with it.

As for the main question of how we prevent duplicate messages, there's no builtin functionality to handle this. However, you should be able to write your own code that responds to messages by waiting instead of responding immediately. That way when your bot code finally does respond, it can see if it has received multiple messages and then only respond to one of them. You can even set a counter that increases with each message the bot receives and then decreases when the bot successfully sends a message.

It would help if I knew whether your bot is scaled sideways or scaled up. If it's scaled sideways, that introduces some concurrency problems. If your bot receives multiple messages in quick succession they may go to separate nodes, and then those nodes will try to update your data at the same time.

v-kydela avatar Sep 01 '18 00:09 v-kydela

you need to look at the activities that you are receving. When a conversation is started you get an activity.Type = ConversationUpdated, then you get an activity.Type=Message.

You are responding to all activities as if they are the same, and they are not.

This is a bug in your code

tomlm avatar Sep 15 '18 22:09 tomlm

@tomlm i think what he is saying is true. you can easily replicate it by creating a new enterprise bot project. have this bot configure to run, then put breakpoint to Bot.cs' constructor and OnTurnAsync() method. it will fire twice. i always have this issue when creating new enterprise bot projects, and i have to put some bad tricks just to overcome this, MS does not even have good explanation/solution to this

miojoffey avatar Mar 29 '19 04:03 miojoffey

We also see a similar issue, bot replies twice for a single session.send() during the conversation. We use JS SDK. This is not a deterministic behavior, but does occur a lot of time. We use bindDialogsToBot and then define the individual triggerAction and bot.dialogs. Not sure why this happens.

Sakshams avatar Jun 17 '19 05:06 Sakshams

Hi @Sakshams

This is a closed issue. Closed issues are not monitored by the Bot Framework Support team.

It looks like you are using Bot Builder V3. Please open a new issue here: https://github.com/microsoft/BotBuilder-V3/issues providing as much detail as possible, and sharing relevant code.

EricDahlvang avatar Jun 17 '19 16:06 EricDahlvang