我在Bot中使用的是自适应卡版本1.2 ,而 Bot Builder对话框版本是4.5.1 。目前,我正在使用TextPrompt
在瀑布对话框中调用自适应卡。我已经编写了一个验证器方法来验证从卡返回的值。在Bot Emulator中可以正常工作。但是,当我将其托管在Azure上时,出现了错误。
在验证器方法中,自适应卡值在promptContext.Recognized.Value
中捕获。但是当托管在Azure上时,它将返回null
,这会导致对象引用未设置为对象的实例异常。
//DialogClass
AddDialog(new WaterfallDialog(nameof(WaterfallDialog), new WaterfallStep[]
{
AdaptiveCardAsync,
}));
AddDialog(new TextPrompt("AdaptiveCard", CardValidator));
private async Task<DialogTurnResult> SelectedOptionAsync(WaterfallStepContext stepContext, CancellationToken cancellationToken)
{
// Get Adaptive Card
JObject card = AdaptiveCard();
return await stepContext.PromptAsync("AdaptiveCard",
new PromptOptions
{
Prompt = (Activity)MessageFactory.Attachment(new Attachment
{
ContentType = AdaptiveCard.ContentType,
Content = card,
}),
}, cancellationToken);
}
//Calling adaptive card.
public JObject AdaptiveCard()
{
string fileName = "GetValues.json";
// combine path for cross platform support
string[] paths = { ".", "AdaptiveCards", fileName };
string fullPath = Path.Combine(paths);
var adaptiveCard = File.ReadAllText(fullPath);
JObject card = JObject.Parse(adaptiveCard);
}
// To validate values received from adaptive card.
private async Task<bool> CardValidator(PromptValidatorContext<string> promptContext, CancellationToken cancellationToken)
{
var result = JObject.Parse(promptContext.Recognized.Value);
}
//Calling Dialog - DialogExtension.cs
public static class DialogExtension
{
public static async Task Run(this Dialog dialog, ITurnContext turnContext, IStatePropertyAccessor<DialogState> accessor, CancellationToken cancellationToken = default(CancellationToken))
{
try
{
var dialogSet = new DialogSet(accessor);
dialogSet.Add(dialog);
var dialogContext = await dialogSet.CreateContextAsync(turnContext, cancellationToken);
// Ensure that message is a postBack (like a submission from Adaptive Cards)
if (dialogContext.Context.Activity.GetType().GetProperty("ChannelData") != null)
{
var channelData = JObject.Parse(dialogContext.Context.Activity.ChannelData.ToString());
if (channelData.ContainsKey("postBack") || channelData.ContainsKey("postback"))
{
var postbackActivity = dialogContext.Context.Activity;
// Convert the user's Adaptive Card input into the input of a Text Prompt
// Must be sent as a string
postbackActivity.Text = postbackActivity.Value.ToString();
//await dialogContext.Context.SendActivityAsync(postbackActivity);
}
}
var results = await dialogContext.ContinueDialogAsync(cancellationToken);
if (results.Status == DialogTurnStatus.Empty)
{
await dialogContext.BeginDialogAsync(dialog.Id, null, cancellationToken);
}
}
catch(Exception ex)
{
await turnContext.SendActivityAsync(MessageFactory.Text(ex.Message));
}
}
请帮助我解决问题。