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

Direct Line Duplicates Posting Messages

Open XelaMon opened this issue 10 years ago • 5 comments

I seem to be having an issue with Direct Line after upgrading to v3 of the Bot Framework. I am getting duplicates of messages that I post using Direct Line.

I've run a remote debugger and noticed that, when I send a message through Direct Line, multiple copies of the message are posted to my bot. Each of these activities have the same text field, but have different IDs and timestamps.

As far as I can tell, this only happens on Direct Line (I don't have any other channels set up, but this isn't happening on the emulator either locally or in the cloud) and only happens when I send the first message in a Direct Line conversation.

I am currently interacting with Direct Line using the published .NET Client Library (1.0.2) and this was not a problem when I was using v1 on the same version of the Client Library.

XelaMon avatar Jul 26 '16 17:07 XelaMon

Some more updates, it seems that this issue isn't necessarily linked to a new Direct Line conversation. When I first use the emulator to make the first message to the bot after restarting my web server, and then use a Direct Line client to make another request, everything works fine.

The problem seems to be when a message is sent to the bot which causes the web server it is on to move out of a suspended state. My bot has to load about 3 GB of data into memory on the first request to it so that it can answer queries. This process takes somewhere between 20 seconds and a minute.

Perhaps Direct Line is noticing that the bot isn't responding within a certain period of time, and then sends a duplicate message to try and get a response? It seems to be a Direct Line specific issue. I just added my bot to the Skype channel as a test, and am not running into the same issue.

XelaMon avatar Jul 26 '16 18:07 XelaMon

Hi @XelaMon this does sound like an HTTP retry issue. If your responses take a long time perhaps you should consider returning a 204 No Content and then replying later.

danmarshall avatar Aug 26 '16 22:08 danmarshall

@XelaMon current implementation that you are describing makes your bot to look unresponsive when first message is coming to your bot. As @danmarshall suggested you should send a 204 and probably an optional message that the bot is warming up. Then send the response whenever your bot is ready as a proactive message to the user.

msft-shahins avatar Sep 02 '16 00:09 msft-shahins

Hey @XelaMon - how'd you go with this?

I'm noticing the same behaviour using DirectLine V3. I'm using React with DirectLineJS to interface with my bot. What I am seeing is multiple messages being sent, the knock-on effect being that my client receives multiple messages back from the bot. If I interact with my bot using the emulator I do not notice this behaviour.

I have tried returning HTTP 204 which made no difference. I have confirmed my suspicion that my application is receiving multiple calls from DirectLine by using Application Insights.

I was wondering if you found any other areas worth investigating as part of this. I will continue investigating my own area and if I found a solution I will post it here.

Update I've tried re-implementing my React component using the botframework-webchat React component but experience the same behaviour of receiving duplicate messages. Except to say, that it's only the very first dialog chain which repeats itself. After the first context.Wait is executed by the server, sent to the client and the following client reply, this behaviour ceases to occur and the bot replies with only one occurrence of the reply.

Update 2 After some more tweaking with my server side code I actually found what was causing this behaviour. I had implemented something like this in my MessageController

if (activity.Type == ActivityTypes.Message)
{
    await Conversation.SendAsync(activity, () => new RootDialog());
}
if (activity.Type == ActivityTypes.ConversationUpdate)
{
     if (activity.MembersAdded.Any(memberAdded => memberAdded.Id != activity.Recipient.Id))
     {
         await Conversation.SendAsync(activity, () => new RootDialog());
     }
}
var response = Request.CreateResponse(HttpStatusCode.NoContent);
return response;

I'd done separate hooks for Message and ConversationUpdate to try and implement a 'greeting' style of behaviour. This appeared to work really well when using the Emulator but not when using DirectLine via REST.

Removing the ConversationUpdate hook appears to have removed the behaviour.

To re-implement a greeting style I could implement an Observor in my React component by pushing an empty message I suppose.

Thanks again for having these communities and resources to help developers work their way through.

Best regards, James

jamesaarondevlin avatar Sep 11 '17 22:09 jamesaarondevlin

I had faced the same issue. But it wasnt because of the Direct line API issue. It was because of code issue. I will explain here with few code snippets. While starting conversation string value user = "testuser" was being passed for the given conversation id.

public async Task StartConversationAsync(string user) { var conversation = await client.Conversations.StartConversationAsync(); Activity userMessage = new Activity { From = new ChannelAccount(user), Text = user, Type = ActivityTypes.Message }; await client.Conversations.PostActivityAsync(conversation.ConversationId, userMessage); }

And while sending messages post the conversation was initialized, string value user = "[email protected]" was being passed for the same conversation id. Due to which the conversation was getting initilized twice for same conversation id but different user resulting into duplicate messages from bot service for same conversation id.

public async Task SendMessageAsync(string text, string user, string conversationId) { Activity userMessage = new Activity { From = new ChannelAccount(user), Text = text,
Type = ActivityTypes.Message }; await client.Conversations.PostActivityAsync(conversationId, userMessage); }

Phew, It took me 3 days to rectify this silly mistake :)

vishnuvardhanpatel1 avatar Dec 14 '17 13:12 vishnuvardhanpatel1