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

bot.on('conversationUpdate') repeats itself in webchat

Open ajayp opened this issue 9 years ago • 8 comments

This issue only started occurring in the past few days.

I am using bot.on('conversationUpdate') as a greeting when the webchat iframe first appears. I see my greeting, but if I then type something into webchat and hit enter, the greeting is output again, this only happens when the first conversation is initiated in web chat.

bot.on('conversationUpdate', function (message) {
    var instructions = "Hi there?";
    var reply = new builder.Message()
        .address(message.address)
        .text(instructions);
    bot.send(reply);
});

ajayp avatar Jan 23 '17 07:01 ajayp

I also ran into this problem. The webchat is sending the ConversationUpdate twice. I think the WebChat got updated some days ago (buttons were not working before) and this bug slipped into it with the update... I hope it gets fixed soon.

fingejo avatar Jan 23 '17 09:01 fingejo

Hi @ajayp and @fingejo, the Direct Line channel sends multiple ConversationUpdate messages.

The first ConversationUpdate is sent when the bot is added to the conversation. After that, each additional ConversationUpdate is sent when a new user joins the conversation.

You can read more about this here: https://github.com/Microsoft/BotBuilder/issues/2065

(You can tell if the member added was a bot by comparing each conversation.MembersAdded[i].Id to activity.Recipient.Id.)

dandriscoll avatar Jan 24 '17 04:01 dandriscoll

@dandriscoll - Thanks, I was using bot.on('conversationUpdate') to send a custom greeting to the user when the bot is initiated, is there another way to present a custom greeting?

ajayp avatar Jan 24 '17 05:01 ajayp

@ajayp, @dandriscoll Would you be able to check if the your bot is one of the members added? If so, do you have any idea on how to do this? Thanks!

m-hou avatar Jan 24 '17 06:01 m-hou

@ajayp and @mushroomcakes, you can tell the difference between your bot and a user by checking the ID field of each element of activity.membersAdded.

{
    "activity": "conversationUpdate",
    "recipient": {
        "id": "MYBOTID" // bot ID here
    },
    "membersAdded": [
        { "id": "MYBOTID" } // this means your bot was added to the conversation, compare with Recipient.Id
        { "id": "user1" } // any other IDs are users
    ]
}

dandriscoll avatar Jan 24 '17 15:01 dandriscoll

If you need to send a message when the bot joins a conversation, you may use the following snippet:

bot.on('conversationUpdate', function (activity) {
    if (activity.membersAdded) {
        activity.membersAdded.forEach((identity) => {
            if (identity.id === activity.address.bot.id) {
                var reply = new builder.activity()
                    .address(activity.address)
                    .text('Hi there!');
                bot.send(reply);
            }
        });
    }
});

pcostantini avatar Jan 24 '17 20:01 pcostantini

@dandriscoll @pcostantini - Thanks for the information, that works.

ajayp avatar Jan 25 '17 06:01 ajayp

In node I had a small issue, I had to write builder.Message() instead of builder.activity() (Code from @pcostantini

Nordes avatar Dec 05 '17 02:12 Nordes