C# 在bot框架中为slack正确设置消息格式

C# 在bot框架中为slack正确设置消息格式,c#,botframework,slack,slack-api,C#,Botframework,Slack,Slack Api,我想用按钮向slack频道发送一条消息。我正在使用bot框架(c#)。我想使用“块”(根据SlackAPI文档,附件是不推荐使用的)。因此,我在slack“Bot Kit Builder”中编写了一条示例消息: 它的json如下所示: [ { "type": "section", "text": { "type": "mrkdwn", "text": "Which pill do you want to t

我想用按钮向slack频道发送一条消息。我正在使用bot框架(c#)。我想使用“块”(根据SlackAPI文档,附件是不推荐使用的)。因此,我在slack“Bot Kit Builder”中编写了一条示例消息:

它的json如下所示:

[
    {
        "type": "section",
        "text": {
            "type": "mrkdwn",
            "text": "Which pill do you want to take?"
        }
    },
    {
        "type": "actions",
        "elements": [
            {
                "type": "button",
                "text": {
                    "type": "plain_text",
                    "text": "Red",
                    "emoji": true
                },
                "value": "red"
            },
            {
                "type": "button",
                "text": {
                    "type": "plain_text",
                    "text": "Blue",
                    "emoji": true
                },
                "value": "blue"
            }
        ]
    }
]
public static dynamic Create(string text, params string[] choices)
{
   var blocks = new List<Block> { new Section { Text = new Text { TextValue = text } } };
   var elements = choices.Select(
                c => new Button { Text = new Text { TextValue = c, Type = "plain_text" }, Value = c });
   blocks.Add(new Actions { Elements = elements.ToArray() });
   return JArray.FromObject(blocks, new JsonSerializer { NullValueHandling = NullValueHandling.Ignore });
}

{[
  {
    "type": "section",
    "text": {
      "type": "mrkdwn",
      "text": "Which pill do you want to take?"
    }
  },
  {
    "type": "actions",
    "elements": [
      {
        "type": "button",
        "text": {
          "type": "plain_text",
          "text": "Red"
        },
        "action_id": "9e8ea9fb9267484a9f02b1837f716f69",
        "value": "Red"
      },
      {
        "type": "button",
        "text": {
          "type": "plain_text",
          "text": "Blue"
        },
        "action_id": "34c3d9509fc04e2ea37ed54a70b78486",
        "value": "Blue"
      }
    ]
  }
]}
据我所知,我必须在我发送到频道的消息的
ChannelData
属性中提供以下内容:


if (turnContext.Activity.ChannelId == Channels.Slack)
{
   message = turnContext.Activity.CreateReply();
   message.ChannelData = ChannelDataBuilder.Create("Which pill do you want to take?", "Red", "Blue");
}
ChannelDataBuilder的代码如下所示:

[
    {
        "type": "section",
        "text": {
            "type": "mrkdwn",
            "text": "Which pill do you want to take?"
        }
    },
    {
        "type": "actions",
        "elements": [
            {
                "type": "button",
                "text": {
                    "type": "plain_text",
                    "text": "Red",
                    "emoji": true
                },
                "value": "red"
            },
            {
                "type": "button",
                "text": {
                    "type": "plain_text",
                    "text": "Blue",
                    "emoji": true
                },
                "value": "blue"
            }
        ]
    }
]
public static dynamic Create(string text, params string[] choices)
{
   var blocks = new List<Block> { new Section { Text = new Text { TextValue = text } } };
   var elements = choices.Select(
                c => new Button { Text = new Text { TextValue = c, Type = "plain_text" }, Value = c });
   blocks.Add(new Actions { Elements = elements.ToArray() });
   return JArray.FromObject(blocks, new JsonSerializer { NullValueHandling = NullValueHandling.Ignore });
}

{[
  {
    "type": "section",
    "text": {
      "type": "mrkdwn",
      "text": "Which pill do you want to take?"
    }
  },
  {
    "type": "actions",
    "elements": [
      {
        "type": "button",
        "text": {
          "type": "plain_text",
          "text": "Red"
        },
        "action_id": "9e8ea9fb9267484a9f02b1837f716f69",
        "value": "Red"
      },
      {
        "type": "button",
        "text": {
          "type": "plain_text",
          "text": "Blue"
        },
        "action_id": "34c3d9509fc04e2ea37ed54a70b78486",
        "value": "Blue"
      }
    ]
  }
]}
因此,基本上我想知道如何使用c#生成这个json对象数组。目前,数组仍然被花括号(list对象)包围,但我想我必须提供一个json对象数组


我已经尝试过使用JsonConvert类并将ChannelData设置为string。但是松弛通道中没有显示任何内容。

信道数据属性允许您传递完整的松弛消息,但缺少所需的顶级属性

如果要包括块,则必须在
属性下定义这些块

因此,您的JSON需要看起来更像这样(不包括
channelData
属性):

{
“区块”:
[
{
“类型”:“节”,
“文本”:{
“类型”:“mrkdwn”,
“文本”:“您想服用哪种药丸?”
}
},
{
“类型”:“操作”,
“要素”:[
{
“类型”:“按钮”,
“文本”:{
“类型”:“纯文本”,
“文本”:“红色”
},
“操作id”:“9e8ea9fb9267484a9f02b1837f716f69”,
“值”:“红色”
},
{
“类型”:“按钮”,
“文本”:{
“类型”:“纯文本”,
“文本”:“蓝色”
},
“行动id”:“34c3d9509fc04e2ea37ed54a70b78486”,
“值”:“蓝色”
}
]
}
]
}
有关botframework的相关文档,请参阅

您可以看到Slack的消息负载是如何定义的

更新 正如@mdrichardson所提到的,botframework目前不支持块。(请参见其github上的)

因此,虽然语法正确,但该解决方案目前不起作用


在botframework支持块之前,我建议使用。

然而,这是正确的。不过,它还在工作中。啊,这解释了为什么我设法使用附件样式使它工作,但不使用块;-)谢谢你的澄清