I'm trying to activate the main root dialog on the conversation update method, my idea is to call main root dialog when the user opens the chat bot, something like proactive bot but not to send welcome message but to activate the root dialog. I've tried this, it works but it sends multiple times the same message to main root:
IConversationUpdateActivity update = activity;
using (var scope = DialogModule.BeginLifetimeScope(Conversation.Container, activity))
{
ConnectorClient connector = new ConnectorClient(new Uri(activity.ServiceUrl));
if (update.MembersAdded.Any())
{
activity.Text = "talk to me";
await Conversation.SendAsync(activity, MainRoot);
}
}
Change the following line:
if (update.MembersAdded.Any())
to:
if (update.MembersAdded.Any(o => o.Id == update.Recipient.Id))
For more details, see @dandriscoll reply here: https://github.com/Microsoft/BotBuilder/issues/2093#issuecomment-274708994 :
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: #2065
(You can tell if the member added was a bot by comparing each conversation.MembersAdded[i].Id to activity.Recipient.Id.)
As pointed out by nrobert, ConversationUpdate is sent once for the bot, and once for the user. I'm closing this issue, since the question has been answered.
Yes but I still get two replays instead of one. Before I changed if (update.MembersAdded.Any()) with
if (update.MembersAdded.Any(o => o.Id == update.Recipient.Id)) it was giving me 3 replays, and after I changed it I get 2 replays. Can be something because I push message to Luis Dialog, which is my root doalog, and the method is like these:
[LuisIntent("MainMenu")]
public async Task MainMenu(IDialogContext context, LuisResult result)
{
var tb = result.Query;
await context.PostAsync(WELCOME);
var reply = context.MakeMessage();
reply.AttachmentLayout = AttachmentLayoutTypes.Carousel;
reply.Attachments = new List<Microsoft.Bot.Connector.Attachment>();
List<string> lst = new List<string>();
lst.Add("Let's Start!");
reply.AddHeroCard(
string.Empty,
lst
);
await context.PostAsync(reply);
context.Wait(Start); //here it needs to go to Start, but again it returns the code above and the next time goes to Start
}
This question should be on StackOverflow with more code details as it seems to be an error in your implementation, not a bug in the framework
This is basically the same thing @nrobert already said, but this is the code I have been using to send welcome messages correctly:
else if (message.Type == ActivityTypes.ConversationUpdate)
{
// Handle conversation state changes, like members being added and removed
// Use Activity.MembersAdded and Activity.MembersRemoved and Activity.Action for info
// Not available in all channels
IConversationUpdateActivity iConversationUpdated = message as IConversationUpdateActivity;
if (iConversationUpdated != null)
{
ConnectorClient connector = new ConnectorClient(new System.Uri(message.ServiceUrl));
foreach (var member in iConversationUpdated.MembersAdded ?? System.Array.Empty<ChannelAccount>())
{
// if the bot is added, then
if (member.Id == iConversationUpdated.Recipient.Id)
{
//Do the stuff you want to do here
await connector.Conversations.ReplyToActivityAsync(reply);
}
}
}
}