Search code examples
.net-corebotframework

Send CardActions in HeroCard as a welcome message and direct to the respective dialog


I'm using Dialogs and waterfallstep to organize the dialog logic, and I want a HeroCard with CardActions as a welcome message. It works fine just sending a HeroCard as a welcome message, but my problem is to direct to the right dialog using turnContext when the user click on one of the options in the CardAction.

Here's my code in Bots.WelcomeBot.cs where I'm stuck. These two methods are after OnMembersAddedAsync which works fine.

private static async Task MainMenuAsync(ITurnContext turnContext, CancellationToken cancellationToken)
    {
        var card = new HeroCard
        {
            Text = "Welcome! What can I help you with?",
            Buttons = new List<CardAction>
            {
                new CardAction() { Title = "Contact", Type = ActionTypes.ImBack, Value = "Contact" },
                new CardAction() { Title = "Newsletter", Type = ActionTypes.ImBack, Value = "Newsletter" },
                new CardAction() { Title = "Products", Type = ActionTypes.ImBack, Value = "Products" },
            },
        };
        var reply = MessageFactory.Attachment(card.ToAttachment());
        await turnContext.SendActivityAsync(reply, cancellationToken);
        await MainMenuChoisesAsync(turnContext, reply, cancellationToken);
    }

    private static async Task MainMenuChoisesAsync(ITurnContext turnContext, IMessageActivity reply, CancellationToken cancellationToken) {

        string choice = reply.ToString().ToLowerInvariant();

        switch (choice) {
            case "contact":
                {
                    //Direct to ContactDialog.cs
                }
        }
    }

Originally this menu of options are in my MainDialog which is triggered when the user has written anything in the start of the chat. The next waterfallstep then redirect to the right Dialog.


Solution

  • If you’re using WebChat or directline, the bot’s ConversationUpdate is sent when the conversation is created and the user sides’ ConversationUpdate is sent when they first send a message. When ConversationUpdate is initially sent, there isn’t enough information in the message to construct the dialog stack. The reason that this appears to work in the emulator, is that the emulator simulates a sort of pseudo DirectLine, but both conversationUpdates are resolved at the same time in the emulator, and this is not the case for how the actual service performs. (source: How to properly send a greeting message and common issues from customers)

    Currently it is not possible to use the conversationUpdate event for the scenario you describe. You can solve this by sending a custom event when the WebChat is fully loaded, however you can’t use the default iframe provided by the Bot Service. Have a look at implementing the Web Chat v4.

    Have a look at a sample which shows how to implement a welcome activity when the bot first starts: