mercredi 3 janvier 2018

MS Bot greets twice after added in webchat

I have the following scenario: A bot is provided via webchat in a webpage. When the webpage is loaded, the bot greets and immediately displays some options a user can pick from. Here is the code, used in MessagesController:

    [ResponseType(typeof(void))]
    public virtual async Task<HttpResponseMessage> Post([FromBody] Activity activity)
    {
        if (activity.Type == ActivityTypes.Message || activity.Type == ActivityTypes.ConversationUpdate)
        {
            if (activity.Type == ActivityTypes.ConversationUpdate &&
               !activity.MembersAdded.Any(r => r.Name == "Bot"))
                return Request.CreateResponse(System.Net.HttpStatusCode.OK);
            await Conversation.SendAsync(activity, () =>
            new RootDialog());
        }
        else
        {
            HandleSystemMessage(activity);
        }
        var response = Request.CreateResponse(System.Net.HttpStatusCode.OK);
        return response;
    }

and part from the RootDialog class:

[Serializable]
public class RootDialog : IDialog<object>
{        
public async Task StartAsync(IDialogContext context)
    {
        var message = context.MakeMessage();
        var attachment = GetHeroCard();
        message.Attachments.Add(attachment);
        await context.PostAsync(message);
        context.Wait(this.ShowFirstCategoryRefinement);
    }
    public virtual async Task ShowFirstCategoryRefinement(IDialogContext context, IAwaitable<IMessageActivity> activity)
    {
        var message = await activity;
        IEnumerable<string> options = this.mainCategories;

        PromptDialog.Choice(
            context: context,
            resume: ChoiceReceivedAsync,
            options: options,
            prompt: "Please select product category:",
            retry: "Selected category not available. Please try again.",
            promptStyle: PromptStyle.Auto
            );
    }
...
}

What actually happens is: user opens the page, the bot greets and shows some options. When the user picks an option, the bot greets again and shows the same options. If the user again picks an option the dialog continues normally.

How shall this be fixed? Thanks.




Aucun commentaire:

Enregistrer un commentaire