【问题标题】:Cannot refresh Actionable Message无法刷新可操作消息
【发布时间】:2021-01-26 18:57:22
【问题描述】:

我的可操作消息卡未引用。这是我已经检查过的内容

{
    "$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 响应数据

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

非常感谢任何帮助!

【问题讨论】:

    标签: adaptive-cards actionable-notification


    【解决方案1】:

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

    为了创建自适应卡 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);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-02-14
      • 1970-01-01
      • 2019-12-21
      • 2012-01-17
      • 1970-01-01
      • 1970-01-01
      • 2014-09-03
      • 2020-06-01
      相关资源
      最近更新 更多