0

我的可操作消息卡没有引用。这是我已经检查过的

{
    "$schema": "http://adaptivecards.io/schemas/adaptive-card.json",
    "version": "1.0",
    "type": "AdaptiveCard",
    "originator": "<guid>",
    "body": [
        {
            "type": "TextBlock",
            "text": "The action has been recorded."
        }
    ]
}

响应从 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);

我也试过这个,没有成功

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();

提交初始卡片的 POST 操作时,Action accepted, thank you.会显示横幅显示,但卡片本身并未刷新。

这是示例 API 响应数据 在此处输入图像描述

你发现任何明显的错误吗?研究这种行为,我发现最常见的错误是没有提供相关的标题,但我肯定会添加它们。

任何帮助是极大的赞赏!

4

1 回答 1

0

我只是自己解决了这个问题。我是数据序列化问题。

为了创建自适应卡 json,我正在使用这两个包

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

具体代码是

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);

card.Expand()返回一个转义的 json 字符串,即类似这样的东西

"{\"key\": \"value\"}"

显然return new OkObjectResult(card)在我上面的问题中尝试再次序列化该字符串,结果只是一个字符串。哪个不是application/json

我的解决方法是只将对象而不是字符串传递给return new OkObjectResult()方法(顾名思义:D)。但是,由于我没有使用任何卡片模板的类型,因此我无法使用 System.Text.Json 进行反序列化,因为匿名类型不支持此操作。因此我使用了 Newtonsoft.Json。以下内容按预期工作并更新了可操作的消息。

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);
于 2021-01-26T10:57:22.143 回答