【发布时间】:2019-08-11 16:34:51
【问题描述】:
我想向 slack 频道发送带有按钮的消息。我正在使用机器人框架(c#)。我想使用“块”(根据 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 对象数组。目前该数组仍被大括号(列表对象)包围,但我想我必须提供一个 json 对象数组。
我已经尝试过使用 JsonConvert 类并将 ChannelData 设置为字符串。但随后松弛通道中什么也没有出现。
【问题讨论】:
标签: c# botframework slack slack-api