Reputation: 143
I'm having trouble in one project handling actions from AdaptiveCards. My method works perfectly fine in all my other projects, just not the most recent. I tried rolling back the version of bot.builder and bot.connector to match the other projects but that didn't help.
The card displays fine but as soon as you hit the button you get the good old "Sorry, my bot code is having an issue". I am displaying the card in the RootDialog, and calling context.wait(MessageReceivedAsync)
Debugging, if I type something to the bot after the card has been displayed MessageReceivedAsync gets fired. Clicking the button, never gets there. Putting a break point directly in the MessageController it gets there, but stepping into it just dies immediately and shows "Sory, my bot code is having an issue."
Here is my 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());
}
Here is the adaptive card action:
"actions": [
{
"type": "Action.Submit",
"title": "Create On-Line Request",
"data": {
"action": "requestContact"
}
}
] }
In my RootDialog:
await context.PostAsync(CardHelper.MakeCard(context, "~\\AdaptiveCards\\ContactInfo311.json", replacements));
context.Wait(MessageReceivedAsync);
Upvotes: 1
Views: 353
Reputation: 27825
The card displays fine but as soon as you hit the button you get the good old "Sorry, my bot code is having an issue".
I do a test with the following sample code, which works for me, you can refer to it. And if possible, you can test it on your side to check if it works for you.
In 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>
Test Result:
Besides, to troubleshoot the issue with your project, you can set breakpoint and debug the code step by step to trace the activity
and check if the error is caused by activity.Text=null
.
Upvotes: 2