bot.on('conversationUpdate') repeats itself in webchat
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);
});
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.
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 - 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, @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!
@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
]
}
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);
}
});
}
});
@dandriscoll @pcostantini - Thanks for the information, that works.
In node I had a small issue, I had to write builder.Message() instead of builder.activity() (Code from @pcostantini