简体   繁体   中英

Bot Framework V4 Choice Prompt with Hero Card c#

We used to be able to use choice prompts with Hero Cards as the Prompt in a dialog like below, but for some reason this has stopped working. The card still displays correctly but when you select an option the prompt retries over and over.

var card = new HeroCard
{

    Text = "Please choose an option.",
    Buttons = new List<CardAction>()
};

foreach (var cl in input.ComplexList)
{
    string objString = JsonConvert.SerializeObject(cl.complexObject);
    card.Buttons.Add(new CardAction(
        ActionTypes.PostBack,
        cl.Name,
        null,
        objString,
        objString,
        objString));
}

card.Buttons.Add(new CardAction(
        ActionTypes.PostBack,
        "Cancel",
        null,
        "Cancel",
        "Cancel",
        "Cancel"));

var promptOptions = new PromptOptions
    {
        Prompt = (Activity)MessageFactory.Attachment(card.ToAttachment())                        
    };
            
return await stepContext.PromptAsync(nameof(ChoicePrompt), promptOptions, cancellationToken);

I see that we can now define the style of the prompt with AddDialog(new ChoicePrompt(nameof(ChoicePrompt)) { Style = ListStyle.HeroCard });

Now my issue is that I was using the hero cards to hide some extra information in the button value to use in my next step in the waterfall dialog, is this just not an option anymore, or am I missing something?

If I set the choice prompt style to HeroCard and remove the hero card specified, then the choices only allow for text which is displayed to the user.

you can put the style property in the PromptOptions

var promptOptions = new PromptOptions {
  Prompt = (Activity) MessageFactory.Attachment(card.ToAttachment()),
  Style = ListStyle.HeroCard
};

in BotBuilderv4 the ChoicePrompt need a IList<Choice> in the PromptOptions

using Microsoft.Bot.Builder.Dialogs.Choices;

var choices = new List<Choice> () {
  new Choice() {
    Value = "Value1",
    // Synonyms
    Synonyms = new string[] {};
  },
  new Choice() {
    Value = "Value2",
    Synonyms = new string[] {};
  },
  new Choice() {
    Value = "Value3",
    Synonyms = new string[] {};
  },
};
var promptOptions = new PromptOptions() {
  Prompt = (Activity) reply,
  RetryPrompt = (Activity) retry,
  Choices = choices,
  /* ^^^^^^^^^^^^^^^^^^ */
  Style = ListStyle.HeroCard,

};
return await stepContext.PromptAsync(nameof(ChoicePrompt), promptOptions, cancellationToken);

where the Choice.Value is the value that you put in CardAction's value :

new CardAction(ActionTypes.PostBack,title : "Cancel", value : "Cancel");

in the next step you can get the user's choice with

private async Task<DialogTurnResult> NextStep(WaterfallStepContext stepContext, CancellationToken cancellationToken) {

  var result = (FoundChoice) stepContext.Result;
  // you can parse it as an enum or use it as is
  string UserChoice = result.Value
  // your logic here

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM