该卡显示正常,但一旦您按下按钮,您就会得到旧的“对不起,我的机器人代码有问题”。
我用下面的示例代码做了一个测试,它对我有用,你可以参考它。如果可能的话,你可以在你身边测试它,看看它是否适合你。
在 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
.