user64870
user64870

Reputation: 47

Not able to pass the selected value to the main dialog in bot framework V4 C#

I have set of adaptive cards which are shown once the bot loads. On action.submit I'm able to get the value of the card selected by the user, but I'm not able to pass that value to the next step in main dialog.

OnTurnAsync following code is written:

public override async Task OnTurnAsync(ITurnContext turnContext, CancellationToken cancellationToken = default(CancellationToken))
    {
        await base.OnTurnAsync(turnContext, cancellationToken);
        var activity = turnContext.Activity;

        if (string.IsNullOrWhiteSpace(activity.Text) && activity.Value != null)
        {
            activity.Text = JsonConvert.SerializeObject(activity.Value);
        }

        // Save any state changes that might have occured during the turn.
        await ConversationState.SaveChangesAsync(turnContext, false, cancellationToken);
        await UserState.SaveChangesAsync(turnContext, false, cancellationToken);
        switch (activity.Text)
        {
            case "{\"choice\":1}":
                break;
            case "{\"choice\":2}":
               // var msg = $"Platform Assistance";
               // await turnContext.SendActivityAsync(MessageFactory.Text(msg), cancellationToken);
                var welcomeCard = CreateAdaptiveCardAttachment();
                var response = CreateResponse(turnContext.Activity, welcomeCard);
                await turnContext.SendActivityAsync(response, cancellationToken);
                //var response = CreateResponse(turnContext.Activity, welcomeCard);
                break;
        }
        await turnContext.SendActivityAsync(activity, cancellationToken);
    }

And in Main Dialog

private async Task<DialogTurnResult> HandleResponseAsync(WaterfallStepContext stepContext, CancellationToken cancellationToken)
    {
        var userResult = stepContext.Result;              

        await stepContext.Context.SendActivityAsync($"INPUT: {stepContext.Result}");
        return await stepContext.NextAsync();
    }

I expect I should get the choice selected by the user once all the cards are shown, which will be further passes to the LUIS intents

I have tried using the text prompt as well but the code doesnt seems to go into the "HandleResponseAsync" method.

Upvotes: 1

Views: 236

Answers (1)

mdrichardson
mdrichardson

Reputation: 7241

You've got a couple issues.

The main one

You're calling these too early:

// Save any state changes that might have occured during the turn.
await ConversationState.SaveChangesAsync(turnContext, false, cancellationToken);
await UserState.SaveChangesAsync(turnContext, false, cancellationToken);

They need to be the very last thing in OnTurnAsync(). Move them to the end and the user's response will go through to the next step.

Additional

await turnContext.SendActivityAsync(activity, cancellationToken);

You have this at the end of OnTurnAsync(), which is going to make it look like everything from the bot comes from the user. Just delete it.

Resources

We just wrote a blog post about using Adaptive Cards that you may find useful.

I also wrote an AdaptiveCardPrompt that you could use instead of a Text/Choice Prompt. Getting it to work in C# is a little tricky since some of the classes are internal/protected. You'd either need to change some of the constants from Prompt or fork the Dotnet SDK. Not recommended. I've also got some Samples that use the AdaptiveCardPrompt. Again, C# has issues. The hope is that this will be in the SDK, soon, but this is still TBD.

Upvotes: 1

Related Questions