想要在自适应卡片中显示点击按钮的文字

Want to display the text of the button clicked in adaptive cards

我创建了一个 Teams 机器人,并且有几个提交操作按钮。 单击这些按钮时,我想让用户知道按钮已被单击。 我正在使用自适应卡片并提交操作。

card.Actions.Add(new AdaptiveSubmitAction()
            {
                Title = item.Key,
                Data = item.Value,
                DataJson = "{\"Type\": \"Sort\"}"

            });

单击“排序”按钮后,我希望机器人回发“排序”。

这就是我对团队的看法

提前致谢

可以使用特殊的“msteams”有效载荷,并指定“messageBack”或“imBack”作为消息类型,类似于此:

{
  "type": "Action.Submit",
  "title": "Click me for messageBack",
  "data": {
    "msteams": {
        "type": "messageBack",
        "displayText": "I clicked this button",
        "text": "text to bots",
        "value": "{\"bfKey\": \"bfVal\", \"conflictKey\": \"from value\"}"
    }
  }
}

您可以阅读更多相关信息 here

因为您使用的是 C#,所以您需要一个实际的 class 来表示它,因此您可以这样做:

public class msteams
{
   public string type {get; set;} = "messageBack";
   public string displayText {get; set;}
   public string text {get; set;}
   public string value {get; set;}
}

那么你可以这样设置:

card.Actions.Add(new AdaptiveSubmitAction()
            {
                Title = item.Key,
                Data = new msTeams() { displayText = "...", ... }
            });

显然,您可以使用属性来更改 class、属性 名称等的名称。对于更简单的方法,您可以只使用“imBack”选项,我已经下面也用属性包裹起来:

    public class AdaptiveCardImBackButton
    {
        [JsonProperty("type")]
        public string Type { get; set; } = "imBack";

        [JsonProperty("value")]
        public string Value { get; set; }
    }

然后我再次包装它,让它序列化外部“msteams”属性,如下所示:

 public class AdaptiveCardImBackButtonContainer
    {
        [JsonProperty("msteams")]
        public AdaptiveCardImBackButton AdaptiveCardImBackButton { get; private set; }

        public AdaptiveCardImBackButtonContainer(string value)
        {
            AdaptiveCardImBackButton = new AdaptiveCardImBackButton() { Value = value };
        }
    }

您代码中的最终用法非常简单:

            card.Actions.Add(new AdaptiveSubmitAction()
            {
                Title = "sort",
                Data = new AdaptiveCardImBackButtonContainer("sort")
            });