处理AdaptiveCard操作得到"抱歉,我的机器人代码存在问题。"

时间:2018-05-18 21:57:51

标签: botframework adaptive-cards

我在处理AdaptiveCards操作的一个项目中遇到了问题。我的方法在我所有的其他项目中运行得非常好,只是不是最新的。我尝试回滚bot.builder和bot.connector的版本以匹配其他项目,但这没有帮助。

卡显示正常但是只要你按下按钮就会得到好的旧的"抱歉,我的机器人代码出现问题"。我在RootDialog中显示卡,并调用context.wait(MessageReceivedAsync)

调试,如果我在显示卡后向机器人输入内容,则会触发MessageReceivedAsync。单击按钮,永远不会到达那里。直接在MessageController中放置一个断点,它就会到达那里,但是踩到它就会立刻死掉并显示" Sory,我的机器人代码有问题。"

这是我的MessageController:

    public async Task<HttpResponseMessage> Post([FromBody]Activity activity)
    {

        if (activity.Type == ActivityTypes.Message)
        {
            ConnectorClient connector = new ConnectorClient(new Uri(activity.ServiceUrl));
            await Conversation.SendAsync(activity, () => new RootDialog());
        }

以下是自适应卡片操作:

  "actions": [
{
  "type": "Action.Submit",
  "title": "Create On-Line Request",
  "data": {
    "action": "requestContact"
  }
}

] }

在我的RootDialog中:

            await context.PostAsync(CardHelper.MakeCard(context, "~\\AdaptiveCards\\ContactInfo311.json", replacements));
            context.Wait(MessageReceivedAsync);

1 个答案:

答案 0 :(得分:2)

  

该卡显示正常,但只要您按下该按钮,您就会看到旧的“抱歉,我的机器人代码出现问题”。

我使用以下示例代码进行测试,这对我有用,您可以参考它。如果可能的话,您可以在您身边进行测试,以检查它是否适合您。

在RootDialog中:

[Serializable]
public class RootDialog : IDialog<object>
{
    public Task StartAsync(IDialogContext context)
    {
        context.Wait(MessageReceivedAsync);

        return Task.CompletedTask;
    }

    private async Task MessageReceivedAsync(IDialogContext context, IAwaitable<object> result)
    {
        var activity = await result as Activity;

        var replymes = context.MakeMessage();

        if (activity.Text != null)
        {
            if (activity.Text.ToLower().Contains("cards"))
            {
                replymes = context.MakeMessage();

                var cardjson = await GetCardText("ContactInfo311");

                var results = AdaptiveCard.FromJson(cardjson);
                var card = results.Card;

                replymes.Attachments.Add(new Attachment()
                {
                    Content = card,
                    ContentType = AdaptiveCard.ContentType,
                    Name = "Card"
                });
            }
            else
            {
                replymes.Text = $"You sent {activity.Text}";
            }
        }
        else if (activity.Value != null)
        {
            replymes.Text = $"submit data: {activity.Value}";
        }

        // return our reply to the user
        await context.PostAsync(replymes);

        context.Wait(MessageReceivedAsync);
    }

    public async Task<string> GetCardText(string cardName)
    {
        var path = System.Web.HttpContext.Current.Server.MapPath($"~/AdaptiveCards/{cardName}.json");

        if (!File.Exists(path))
            return string.Empty;

        using (var f = File.OpenText(path))
        {
            return await f.ReadToEndAsync();
        }
    }
}

<强> ContactInfo311.json

{
  "type": "AdaptiveCard",
  "version": "1.0",
  "body": [
    {
      "type": "TextBlock",
      "size": "large",
      "weight": "bolder",
      "text": "This is Adaptive Card"
    }
  ],
  "actions": [
    {
      "type": "Action.Submit",
      "data": {
        "action": "requestContact"
      },
      "title": "Clike me"
    }
  ]
}

<强> packages.config

<?xml version="1.0" encoding="utf-8"?>
<packages>
  <package id="AdaptiveCards" version="1.0.0" targetFramework="net46" />
  <package id="Autofac" version="3.5.2" targetFramework="net46" />
  <package id="Chronic.Signed" version="0.3.2" targetFramework="net46" />
  <package id="Microsoft.AspNet.WebApi" version="5.2.3" targetFramework="net46" />
  <package id="Microsoft.AspNet.WebApi.Client" version="5.2.3" targetFramework="net46" />
  <package id="Microsoft.AspNet.WebApi.Core" version="5.2.3" targetFramework="net46" />
  <package id="Microsoft.AspNet.WebApi.WebHost" version="5.2.3" targetFramework="net46" />
  <package id="Microsoft.Bot.Builder" version="3.15.2.2" targetFramework="net46" />
  <package id="Microsoft.Bot.Connector" version="3.15.2.2" targetFramework="net46" />
  <package id="Microsoft.CSharp" version="4.4.0" targetFramework="net46" />
  <package id="Microsoft.IdentityModel.Logging" version="1.1.4" targetFramework="net46" />
  <package id="Microsoft.IdentityModel.Protocol.Extensions" version="1.0.4.403061554" targetFramework="net46" />
  <package id="Microsoft.IdentityModel.Protocols" version="2.1.4" targetFramework="net46" />
  <package id="Microsoft.IdentityModel.Protocols.OpenIdConnect" version="2.1.4" targetFramework="net46" />
  <package id="Microsoft.IdentityModel.Tokens" version="5.1.4" targetFramework="net46" />
  <package id="Microsoft.Rest.ClientRuntime" version="2.3.2" targetFramework="net46" />
  <package id="Microsoft.WindowsAzure.ConfigurationManager" version="3.1.0" targetFramework="net46" />
  <package id="Newtonsoft.Json" version="9.0.1" targetFramework="net46" />
  <package id="System.IdentityModel.Tokens.Jwt" version="5.1.4" targetFramework="net46" />
</packages>

测试结果:

enter image description here

此外,要解决项目问题,您可以设置断点并逐步调试代码以跟踪activity并检查错误是否由activity.Text=null引起。

enter image description here