在 bot 框架中正确格式化消息以实现 slack
Properly format a message for slack in bot framework
我想向 Slack 频道发送带有按钮的消息。我正在使用机器人框架 (c#)。我想使用 "blocks"(根据 slack api 文档不推荐使用附件)。所以我在 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"
}
]
}
]
据我了解,我必须在发送到频道的消息的 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 的代码如下所示:
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 });
}
此方法的结果 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"
},
"action_id": "9e8ea9fb9267484a9f02b1837f716f69",
"value": "Red"
},
{
"type": "button",
"text": {
"type": "plain_text",
"text": "Blue"
},
"action_id": "34c3d9509fc04e2ea37ed54a70b78486",
"value": "Blue"
}
]
}
]}
所以,基本上我想知道我应该如何使用 c# 生成这个 json object 的数组。目前数组仍然被大括号包围(列表 object),但我想我必须提供一个 json objects.
的数组
我已经尝试使用 JsonConvert class 并将 ChannelData 设置为字符串。但是随后 slack 频道中什么也没有出现。
channelData
属性 允许您传递完整的 Slack 消息,但您缺少所需的顶级属性。
如果要包含块,则必须在 blocks
属性.
下定义这些块
所以你的 JSON 需要看起来更像这样(不包括 channelData
属性):
{
"blocks":
[
{
"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"
}
]
}
]
}
有关 botframework 的相关文档,请参阅 here。
并且 here 您可以看到 Slack 的消息负载是如何定义的。
更新
如@mdrichardson 所述,botframework 目前不支持块。 (参见 this issue 关于他们的 github)
因此,虽然在语法上正确,但此解决方案目前 不起作用。
在 botframework 支持块之前,我建议使用 secondary attachments。
我想向 Slack 频道发送带有按钮的消息。我正在使用机器人框架 (c#)。我想使用 "blocks"(根据 slack api 文档不推荐使用附件)。所以我在 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"
}
]
}
]
据我了解,我必须在发送到频道的消息的 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 的代码如下所示:
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 });
}
此方法的结果 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"
},
"action_id": "9e8ea9fb9267484a9f02b1837f716f69",
"value": "Red"
},
{
"type": "button",
"text": {
"type": "plain_text",
"text": "Blue"
},
"action_id": "34c3d9509fc04e2ea37ed54a70b78486",
"value": "Blue"
}
]
}
]}
所以,基本上我想知道我应该如何使用 c# 生成这个 json object 的数组。目前数组仍然被大括号包围(列表 object),但我想我必须提供一个 json objects.
的数组我已经尝试使用 JsonConvert class 并将 ChannelData 设置为字符串。但是随后 slack 频道中什么也没有出现。
channelData
属性 允许您传递完整的 Slack 消息,但您缺少所需的顶级属性。
如果要包含块,则必须在 blocks
属性.
所以你的 JSON 需要看起来更像这样(不包括 channelData
属性):
{
"blocks":
[
{
"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"
}
]
}
]
}
有关 botframework 的相关文档,请参阅 here。
并且 here 您可以看到 Slack 的消息负载是如何定义的。
更新
如@mdrichardson 所述,botframework 目前不支持块。 (参见 this issue 关于他们的 github)
因此,虽然在语法上正确,但此解决方案目前 不起作用。
在 botframework 支持块之前,我建议使用 secondary attachments。