I'm currently trying to output an Adaptive Card to the end user in Microsoft Teams but whenever I try to post this card, it returns an error. On top of this, any little tweaks I make, it also returns an error so instead posting an array of errors I believe there must be something funamentally wrong that Im doing and so I'm seeking clarification on this area as I'am new to it. So here is the scenario, i) End user has entered in a reply and hit send, which sends their message to the function below,
private async Task CallCreatePart2(IDialogContext context, IAwaitable<object> email)
{
var callemail = await email as string;
...
var cardJson = GetCardJson();
AdaptiveCardParseResult result = AdaptiveCard.FromJson(cardJson);
AdaptiveCard card = result.Card;
IMessageActivity message = Activity.CreateMessageActivity();
message.Attachments = new List<Microsoft.Bot.Connector.Attachment>();
Microsoft.Bot.Connector.Attachment plAttachment = new Microsoft.Bot.Connector.Attachment()
{
ContentType = AdaptiveCard.ContentType,
Content = card
};
message.Attachments.Add(plAttachment);
message.ReplyToId = context.Activity.Id;
await context.PostAsync(message);
}
So this will throw the following,
System.NullReferenceException: 'Object reference not set to an instance of an object.'
My adaptive card is as follows,
{
"type": "AdaptiveCard",
"body": [
{
"type": "TextBlock",
"size": "Medium",
"weight": "Bolder",
"text": "My Notification"
},
{
"type": "TextBlock",
"weight": "Bolder",
"text": "New ticket",
"wrap": true
},
{
"type": "ActionCard",
"name": "Visit",
"actions": [
{
"type": "OpenUri",
"name": "Visit",
"targets": [
{
"os": "default",
"uri": "https://www.microsoft.com"
}
]
}
]
},
{
"type": "FactSet",
"facts": [
{
"title": "Test1",
"value": "hello"
},
{
"title": "Test2:",
"value": "test"
}
]
}
],
"$schema": "http://adaptivecards.io/schemas/adaptive-card.json",
"version": "1.0"
}
The ReplyToId value is set here as it complained that it was null before. The thing is after this I want to output a PromptDialog.Confirm object and so the user will be reply to this. The Adaptive Card is purely to output a nice looking card with the json that is created using information from an external source that is provided. So with this being said, is there anything fundamentally wrong with what I'am trying to achieve? Am I not allowed to post an adaptive card to the end user using context.PostAsync()?
So ReplyToId() needs to be set on the message, which I gathered after doing some research, however once this hurdle was passed I kept getting the following error,
"Microsoft.Bot.Connector.ErrorResponseException: 'Operation returned an invalid status code 'BadRequest''".
Turns out the "BadRequest" error was actually all down to the data that was being passed into the AdaptiveCard.
string callCardJson = CreateJsonPayloadAsDataForAdapativeCard();
var myInfo = JsonConvert.DeserializeObject<JsonPayLoad>(callCardJson);
var templateJson = await WebhookController.GetCardText("my_template");
#if DEBUG
templateJson = await WebhookController.GetCardText("my_template_test");
#endif
AdaptiveCardTemplate template = new AdaptiveCardTemplate(templateJson);
var myData = new
{
title = myInfo.title,
name = myInfo.name,
token = myInfo.token,
reference = myInfo.reference,
info = my.info
};
string cardJson = template.Expand(myData);
AdaptiveCardParseResult result = AdaptiveCard.FromJson(cardJson);
card = result.Card;
So the AdaptiveCard was being used as a generic card in multiple scenarios but not all the data fields would be required in all the scenarios so initially I was passing blank values into the json data object for things such as token and reference above. When this was then deserialized above and then passed into the AdapativeCard, it caused the "BadRequest" error. I fixed the issue by removing the blank entries in the json data object!