我几乎是bot框架的新手。我遇到了一个问题。我需要选择选项并检索用户所选择的内容。但机器人显示错误并退出代码。我知道我几乎在接近什么,正在错过什么,也许是因为我缺乏知识。请帮我解决这个问题。这是代码。使用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中的提示结果,这将有助于在任何其他对话框中处理类似的提示。
这将在您的";Bots";文件夹示例-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);
}
让我知道这是否如你所料