Search code examples
c#azuredrop-down-menubotframeworkwaterfall

Get values from drop down List C# Bot Framework


I am almost new to bot framework. I have been stuck in a problem. I need to select the options and retrieve what the user has been selected. But the bot shows me error and exits the code. I know i am almost near something and is missing something, maybe due to my lack of knowledge. Please help me to solve this. Here is the code. Using SDK v4.

private async Task<DialogTurnResult> cards(WaterfallStepContext stepContext, CancellationToken cancellationToken)
        {
            var details = (Details)stepContext.Options;
            var card = new AdaptiveCard();
            card.Body.Add(new AdaptiveChoiceSetInput()
            {
                Id = "choiceset1",
                Choices = new List<AdaptiveChoice>()
                {
                    new AdaptiveChoice(){
                        Title="answer1",
                        Value="answer1"
                    },
                    new AdaptiveChoice(){
                        Title="answer2",
                        Value="answer2"
                    },
                    new AdaptiveChoice(){
                        Title="answer3",
                        Value="answer3"
                    }
                },
                Style = AdaptiveChoiceInputStyle.Expanded,
                IsMultiSelect = true
            });
            card.Actions.Add(new AdaptiveSubmitAction()
            {
                Title = "submit",
                Type = "Action.Submit",
            });
var message = MessageFactory.Text("");
message.Attachments.Add(new Attachment() { Content = card, ContentType = "application/vnd.microsoft.card.adaptive" });
            return await stepContext.PromptAsync(nameof(ChoicePrompt), new PromptOptions { Prompt = message  }, cancellationToken);
        }

and in the next waterfall step the code is

private async Task<DialogTurnResult> options(WaterfallStepContext stepContext, CancellationToken cancellationToken)
    {
        await stepContext.Context.SendActivityAsync(MessageFactory.Text("Selected Options must be displayed here."));
        return await stepContext.EndDialogAsync(cancellationToken);
    }

Please help me in solving the problem. Thanks in advance


Solution

  • You will need to parse user's selection from the message as below

    private async Task<DialogTurnResult> options(WaterfallStepContext stepContext, CancellationToken cancellationToken)
    {
        var selectedChoice = JObject.Parse((string)stepContext.Result)["choiceset1"];
        await stepContext.Context.SendActivityAsync(MessageFactory.Text("Selected Options is: "+ selectedChoice.ToString()));
        return await stepContext.EndDialogAsync(cancellationToken);
    }
    

    And also, additionally, you need to add the below for handling the prompt results in your DefaultActivityHandler, which will help to handle such similar prompts in any other dialogs.

    This would be under your "Bots" folder. Example- https://github.com/microsoft/botframework-solutions/blob/master/samples/csharp/assistants/virtual-assistant/VirtualAssistantSample/Bots/DefaultActivityHandler.cs

        protected override Task OnMessageActivityAsync(ITurnContext<IMessageActivity> turnContext, CancellationToken cancellationToken)
        {
            // directline speech occasionally sends empty message activities that should be ignored
            var activity = turnContext.Activity;
            if (activity.ChannelId == Channels.DirectlineSpeech && activity.Type == ActivityTypes.Message && string.IsNullOrEmpty(activity.Text))
            {
                return Task.CompletedTask;
            }
    
    
           //the new condition
            if (string.IsNullOrWhiteSpace(activity.Text) && activity.Value != null)
            {
                activity.Text = JsonConvert.SerializeObject(activity.Value);
            }
    
    
            return _dialog.RunAsync(turnContext, _dialogStateAccessor, cancellationToken);
        }
    

    Let me know if that worked as you expected