本文介绍了在Bot框架中正确设置消息格式以便松弛的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将带有按钮的消息发送到备用频道.我正在使用机器人框架(C#).我想使用块"(根据Slack API文档,不推荐使用附件).因此,我在松弛的"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设置为字符串.但是,在闲置通道中什么也没出现.

解决方案

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的相关文档.

在这里,您将看到如何定义Slack的消息有效负载./p>

更新

如@mdrichardson所述,botframework当前不支持块. (请参阅他们的github上的此问题)

因此,尽管语法上正确,但此解决方案目前不起作用.

在机器人框架支持块之前,我建议使用辅助附件

I'd like to send a message with buttons to the slack channel. I'm using the bot framework (c#). I want to use the "blocks" (attachments are deprecated according to the slack api docs). So I composed a sample message in the slack "Bot Kit Builder":

The json for it looks like this:

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

As I understand, I have to provide this content in the ChannelData property of the message I sent to the channel:


if (turnContext.Activity.ChannelId == Channels.Slack)
{
   message = turnContext.Activity.CreateReply();
   message.ChannelData = ChannelDataBuilder.Create("Which pill do you want to take?", "Red", "Blue");
}

The code of the ChannelDataBuilder looks like this:

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 });
}

The resulting json of this method looks like this:

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

So, basically I wonder how I should generate this array of json object using c#. Currently the array is still surrounded by the curly brackets (the list object), but I guess I have to provide an array of json objects.

I've already tried using the JsonConvert class and setting the ChannelData as string. But then nothing appears in the slack channel.

解决方案

The channelData property allows you to pass a complete Slack message, but your are missing the required top-level properties.

If you want to include blocks, than those have to be defined under the blocks property.

So your JSON need to look more like this (not include the channelData property):

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

See here for the relevant documentation for the botframework.

And here you can see how a message payload for Slack is defined.

Update

As mentioned by @mdrichardson the botframework currently does not support blocks. (See this issue on their github)

So while syntactically correct this solutions does currently not work.

Until botframework supports blocks I would suggest to use secondary attachments.

这篇关于在Bot框架中正确设置消息格式以便松弛的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-22 22:44
查看更多