Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Question Bot C#]How to stop sending multiple messages in the beginning to activate itself #2941

Closed
msBot1111 opened this issue Jun 14, 2017 · 5 comments

Comments

@msBot1111
Copy link

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);
                }
            }
@nrobert
Copy link

nrobert commented Jun 14, 2017

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: #2093 (comment) :

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.)

@EricDahlvang
Copy link
Member

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.

@msBot1111
Copy link
Author

msBot1111 commented Jun 14, 2017

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
            }

@nrobert
Copy link

nrobert commented Jun 14, 2017

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

@JasonSowers
Copy link
Contributor

JasonSowers commented Jun 14, 2017

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);
                                        }
                                    }
                                }
            }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants