2
votes

I have a welcoming message configured to appear in MessagesController the first time my bot is started.

    private Activity HandleSystemMessage(Activity message)
    {
        if (message.Type == ActivityTypes.ConversationUpdate)
        {
            // returning a msg here to the Post method in MessagesController.
        }
    }

When I debug it would seem that at start time, TWO threads are working the bot and both are executing in the Post method, and consequently both are calling HandleSystemMessage. This is a problem for me, because with two threads executing the method, my welcoming message is being printed twice on screen.

I tried locking the print msg and putting one of the threads to sleep, but none have worked. I don't know why there are two threads executing to begin with.

Are they necessary? they are both running an identical execution. Could I kill one of them? Or is there a different way to print a welcome message for the bot?

4

4 Answers

1
votes

it's possible that you are returning a message for the bot joining chat and the user as well. It's hard to tell without seeing the code in your conversation update part of the if-else statement in root dialog. You can use the following code to post just a single message

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)
            {
                var reply = ((Activity) iConversationUpdated).CreateReply(
                    $"Hi! I'm Botty McBotface and this is a welcome message");
                await connector.Conversations.ReplyToActivityAsync(reply);
            }
        }
    }
}
2
votes

When the first conversation is established between the web channel and the bot, the ConversationUpdate activity is raised twice. One by the user and another by the channel, therefore we get a welcome message twice. We need to make sure that we send the welcome message for the activity raised by the user.

This piece of code helped me to avoid the issue.

 private async Task GreetUserAsync(ITurnContext turnContext, CancellationToken cancellationToken)
    {
        if (turnContext.Activity.Type == ActivityTypes.ConversationUpdate && 
            turnContext.Activity.MembersAdded[0].Id.Contains(turnContext.Activity.Recipient.Id))
            await turnContext.SendActivityAsync(MessageFactory.Text("Hi, how can I help you."));
    }
1
votes

Alternatively you can try this code

 else if (message.Type == ActivityTypes.ConversationUpdate)
                {

                    ConnectorClient connector = new ConnectorClient(new Uri(message.ServiceUrl));
                    var reply = message.CreateReply(BotMessages.WelcomeMessage);
                    connector.Conversations.SendToConversation(reply);
                    reply = message.CreateReply();
                    reply.AttachmentLayout = AttachmentLayoutTypes.Carousel;
                    reply.Attachments = HelpTextDialog.GetMessageCard();
                    connector.Conversations.SendToConversation(reply);



                }
0
votes

I think the simple way(using Linq) to define in

ActivityTypes.ConversationUpdate is(even it works in Azure Bot Service),

,

            var client = new ConnectorClient(new Uri(message.ServiceUrl));
            **if (message.MembersAdded.Any(o => o.Id == message.Recipient.Id))**
            {
                var reply = message.CreateReply();
                reply.Text = $"Welcome User! May I know your First and Last name?";
                reply.Text += $"{Environment.NewLine}How can i help you?";
                client.Conversations.ReplyToActivityAsync(reply);
            }