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

Bot Framework, Post Method in MessageController is getting called twice

Open tinuCoder opened this issue 8 years ago • 3 comments

What i am trying to do.

I want to proactively post welcome message to user. I am trying to achieve the same by following code

            if (activity.Type == ActivityTypes.ConversationUpdate)
            {
                Activity welcomeMessage = activity.CreateReply();
                welcomeMessage.Type = "message";
                welcomeMessage.Text = $"Hey, how’s it going? how can i help you today?";
                           
                await connector.Conversations.SendToConversationAsync(welcomeMessage);
            }

I am using conversationUpdate condition for checking if the conversation is new conversation. and if passes the check of conversationUpdate i am pushing welcome message to the user.

##Issue faced.

When i run the bot Post method in messageController files gets called twice. Like if someone starts a new conversation with the bot or refresh the chat conversationUpdate is called twice and that is pushing welcome message 2 times to the user. PFB

image

Code Example

    public async Task<HttpResponseMessage> Post([FromBody]Activity activity)
    {
        if (activity.Type == ActivityTypes.Message)
        {
            await HandleUserMessage(activity);
        }
        else
        {
            await HandleSystemMessage(activity);
        }
        var response = Request.CreateResponse(HttpStatusCode.OK);
        return response;
    }


    private async Task HandleSystemMessage(Activity activity)
    {
            if (activity.Type == ActivityTypes.ConversationUpdate)
            {
                Activity welcomeMessage = activity.CreateReply();
                welcomeMessage.Type = "message";
                welcomeMessage.Text = $"Hey, how’s it going? how can i help you today?";
                           
                await connector.Conversations.SendToConversationAsync(welcomeMessage);
            }
    }

Expected Behavior

I need to push welcome message to the user on every new conversation "once"

Actual Results

currently welcome message is pushed twice.

Thanks in advance :)

tinuCoder avatar Feb 27 '18 13:02 tinuCoder

Anyone who can help me on this?

tinuCoder avatar Feb 28 '18 11:02 tinuCoder

ConversationUpdate is sent once when the bot is added, and once when the user is added. More information can be found here: https://docs.microsoft.com/en-us/bot-framework/dotnet/bot-builder-dotnet-activities#conversationupdate

So, only show the welcome message if either the user, or the bot is added by checking the MembersAdded:

            if (message.Type == ActivityTypes.ConversationUpdate)
            {
                var iConversationUpdate = message.AsConversationUpdateActivity();
                Activity welcomeMessage = message.CreateReply();
                welcomeMessage.Type = "message";
                welcomeMessage.Text = $"Hey, how’s it going? how can i help you today?";
                foreach (var member in iConversationUpdate.MembersAdded)
                {
                    // if the bot is added, then 
                    if (member.Id == iConversationUpdate.Recipient.Id)
                    {
                        await connector.Conversations.ReplyToActivityAsync(welcomeMessage);
                    }
                }
            }

EricDahlvang avatar Feb 28 '18 18:02 EricDahlvang

Thanks alot @EricDahlvang this worked perfect. 🥇 🥇

tinuCoder avatar Mar 01 '18 05:03 tinuCoder