Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
436 views
in Technique[技术] by (71.8m points)

adaptive cards - Cannot refresh Actionable Message

my Actionable Message card is not refereshing. Here's what I have already checked

  • the refesher card has valid syntax according to the actionable card designer
  • the refresher card has the originator property
{
    "$schema": "http://adaptivecards.io/schemas/adaptive-card.json",
    "version": "1.0",
    "type": "AdaptiveCard",
    "originator": "<guid>",
    "body": [
        {
            "type": "TextBlock",
            "text": "The action has been recorded."
        }
    ]
}

The response is returned as following from the API (Azure Functions)

string card = "<card-json>";
req.HttpContext.Response.Headers.Add("CARD-ACTION-STATUS", "Action accepted, thank you.");
req.HttpContext.Response.Headers.Add("CARD-UPDATE-IN-BODY", "true");
return new OkObjectResult(card);

Also I have tried this, without success

string card = "<card-json>";
req.HttpContext.Response.ContentType = "application/json; charset=utf-8";
req.HttpContext.Response.Headers.Add("CARD-ACTION-STATUS", "Action accepted, thank you.");
req.HttpContext.Response.Headers.Add("CARD-UPDATE-IN-BODY", "true");
req.HttpContext.Response.Body = new MemoryStream(Encoding.UTF8.GetBytes(card));
return new OkResult();

When submitting the inital card's POST action, see banner displaying Action accepted, thank you. is displayed, however the card itself is not refreshed.

Here's the sample API response data enter image description here

Do you spot any obvious mistakes? Researching this behaviour, I've found that the most common error is not supplying the relevant headers, but I was sure to add them.

Any help is greatly appreciated!

question from:https://stackoverflow.com/questions/65896278/cannot-refresh-actionable-message

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I just solved the problem myself. I was a data serialization issue.

For creating the adaptive card json I am using these two packages

  <ItemGroup>
    <PackageReference Include="AdaptiveCards" Version="2.0.0" />
    <PackageReference Include="AdaptiveCards.Templating" Version="1.0.1" />
  </ItemGroup>

Specifically the code is

string templateFilePath = Path.Combine(ctx.FunctionAppDirectory, "ActionableMessageCards", cardTemplateName);
string template = await File.ReadAllTextAsync(templateFilePath);
var card = new AdaptiveCardTemplate(template);
var context = new EvaluationContext() { Root = cardData };
return card.Expand(context);

and card.Expand() returns an escaped json-string, i.e. something like this

"{"key": "value"}"

Apparently return new OkObjectResult(card) in my question above tries to serialize that string again, resulting in just a string. Which is not application/json.

My workaround was to just pass an object rather than a string to the return new OkObjectResult() method (as the name suggests :D). However, since I do not have types of any card-template I use, I could not use System.Text.Json for deserialization, because this is not supported for anonymous types. Therefore I used Newtonsoft.Json. The following works and updates actionable message as expected.

req.HttpContext.Response.Headers.Add("Content-Type", "application/json");
req.HttpContext.Response.Headers.Add("CARD-ACTION-STATUS", "Action accepted, thank you.");
req.HttpContext.Response.Headers.Add("CARD-UPDATE-IN-BODY", "true");
var obj = Newtonsoft.Json.JsonConvert.DeserializeObject(card);
return new OkObjectResult(obj);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...