当Bot托管在Azure上时,我无法在验证器方法内的瀑布对话框中从自适应卡中检索值

时间:2019-07-24 06:12:14

标签: botframework adaptive-cards

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

请帮助我解决问题。

1 个答案:

答案 0 :(得分:0)

问题在于,“在Web聊天中测试”和Web聊天iFrame <embed>代码仍然使用WebChat V3,它不能很好地支持自适应卡。 WebChat V4应该在接下来的几周内同时推广到这两个平台。

关于您的LUIS问题,请访问:

  1. Azure门户网站
  2. 您的资源组
  3. 您的机器人使用的应用服务

enter image description here

  1. 转到配置

enter image description here

  1. 确保所有配置值都与appsettings.json文件中的值匹配。

这不会影响您的其他V4机器人,因为每个机器人应该使用其自己的应用程序服务。