1

我在一个项目处理来自 AdaptiveCards 的操作时遇到了麻烦。我的方法在我所有其他项目中都非常有效,只是不是最近的。我尝试回滚 bot.builder 和 bot.connector 的版本以匹配其他项目,但这并没有帮助。

该卡显示正常,但一旦您按下按钮,您就会得到旧的“对不起,我的机器人代码有问题”。我在 RootDialog 中显示卡片,并调用 context.wait(MessageReceivedAsync)

调试,如果我在显示卡后向机器人键入内容 MessageReceivedAsync 被触发。单击按钮,永远不会到达那里。直接在 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);
4

1 回答 1

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"
    }
  ]
}

包.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>

测试结果:

在此处输入图像描述

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

在此处输入图像描述

于 2018-05-21T07:33:30.433 回答