【问题标题】:Properly format a message for slack in bot framework为 bot 框架中的 slack 正确格式化消息
【发布时间】: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


    【解决方案1】:

    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"
                }
                ]
            }
        ]
    }
    

    有关机器人框架的相关文档,请参阅 here

    here 你可以看到 Slack 的消息负载是如何定义的。

    更新

    正如@mdrichardson 所提到的,botframework 目前不支持块。 (请参阅他们的 github 上的 this issue

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

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

    【讨论】:

    • 这是真的,但是Slack Block Kit isn't currently supported。不过,它正在开发中。
    • 啊,这解释了为什么我设法使用附件样式使其工作,而不是使用块 ;-) 谢谢你的澄清
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-12
    • 1970-01-01
    • 1970-01-01
    • 2018-06-30
    • 1970-01-01
    • 2019-02-28
    相关资源
    最近更新 更多