Bot-docs: Can't cast Choiceprompt result into string

Created on 28 Jan 2019  路  3Comments  路  Source: MicrosoftDocs/bot-docs

Hi

Following the documentation to create a choiceprompt (example used: Location) : https://docs.microsoft.com/en-us/azure/bot-service/bot-builder-prompts?view=azure-bot-service-4.0&tabs=csharp

I can't seem to correctly cast stepContext.Result from the choice into a string containing the choice.
Instead,
stepContext.Result as string;
results into the object type name : Microsoft.Bot.Builder.Dialogs.Choices.FoundChoices;

I can see stepContext.Result contains the Value property with the selected choice, but I can't access it.
In V3 you had to await the activity to get the string value instead of the object name, but here I can't await stepContext.Result and cast it into a string variable.

What am I doing wrong?

PS: Here's the previous Choice prompt:

`
private static async Task FAQChoicePromptAsync(
WaterfallStepContext stepContext,
CancellationToken cancellationToken = default(CancellationToken))
{

        return await stepContext.PromptAsync(FAQPrompt,
            new PromptOptions
            {
                Prompt = MessageFactory.Text("What do you want to know?"),
                RetryPrompt = MessageFactory.Text("Selected Option not available . Please try again."),
                Choices = ChoiceFactory.ToChoices(questions),
            },
        cancellationToken);



    }`
platform SDK v4

Most helpful comment

Ok, found the answer, but this should be added to the documentation about prompts and handling the results on the next step

var FAQChoice = ((FoundChoice)stepContext.Result).Value.ToString();

All 3 comments

The only way I can make it work is to use SendActivityAsync along with SuggestedActions:
await stepContext.Context.SendActivityAsync( MessageFactory.SuggestedActions(questions, "What do you want to know?"), cancellationToken: cancellationToken); return Dialog.EndOfTurn;

However this defeats the purpose of using the framework in an efficient way, making use of everything at my disposal, like Prompts.
I'm guessing i'm missing something here in order to use the return values of a prompt.

Ok The problem is I'm not sure how to cast if the prompt option was obtained from a list of strings.

Here's the prompt:

public static List<string> questions;
return await stepContext.PromptAsync(FAQPrompt,
                new PromptOptions
                {
                    Prompt = MessageFactory.Text("What do you want to know?"),
                    RetryPrompt = MessageFactory.Text("Selected Option not available . Please try again."),
                    Choices = ChoiceFactory.ToChoices(questions),

                },
            cancellationToken);

and here's what crashes the code:
var FAQChoice = (List<string>)stepContext.Result;

What am I doing wrong?

Ok, found the answer, but this should be added to the documentation about prompts and handling the results on the next step

var FAQChoice = ((FoundChoice)stepContext.Result).Value.ToString();

Was this page helpful?
0 / 5 - 0 ratings

Related issues

wilmol picture wilmol  路  3Comments

mdrichardson picture mdrichardson  路  6Comments

blueelvis picture blueelvis  路  6Comments

SarahSexton picture SarahSexton  路  3Comments

amthomas46 picture amthomas46  路  4Comments