从下拉列表中获取值 C# Bot Framework

Get values from drop down List C# Bot Framework

我几乎是机器人框架的新手。我一直陷入困境。我需要 select 选项并检索用户已被 select 编辑的内容。但是机器人向我显示错误并退出代码。我知道我几乎接近某个东西并且丢失了一些东西,这可能是由于我缺乏知识。请帮我解决这个问题。这是代码。使用 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);
        }

在下一个瀑布步骤中,代码是

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);
    }

请帮我解决问题。提前致谢

您需要从消息中解析用户的选择,如下所示

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);
}

此外,您还需要在 DefaultActivityHandler 中添加以下内容来处理提示结果,这将有助于在任何其他对话框中处理此类类似提示。

这将在您的“机器人”文件夹下。 示例- 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);
    }

让我知道是否如您所愿