我正在尝试使用C#编写的程序通过Slack应用发布到Slack频道,但使用此处建议的格式:https://api.slack.com/tools/block-kit-builder
我下面有这段代码,该代码发布到Slack频道,因此我知道这是可行的。
{
static void Main(string[] args)
{
PostWebHookAsync();
Console.ReadLine();
}
static async void PostWebHookAsync()
{
using (var httpClient = new HttpClient())
{
using (var request = new HttpRequestMessage(new HttpMethod("POST"), "my-webhook-link"))
{
string jsonValue = JsonConvert.SerializeObject(new
{
type = "section",
text = "Some text \n new line \t tab",
}
);
Console.WriteLine(jsonValue);
Type valueType = jsonValue.GetType();
if (valueType.IsArray)
{
jsonValue = jsonValue.ToString();
Console.WriteLine("Array Found");
}
request.Content = new StringContent(jsonValue, Encoding.UTF8, "application/json");
var response = await httpClient.SendAsync(request);
Console.WriteLine(response.StatusCode);
Console.WriteLine(response.Content);
}
}
}
}
哪个返回:
{"type":"section","text":"Some text \n new line \t tab"}
现在我要发布这个
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "This is a mrkdwn section block :ghost: *this is bold*, and ~this is crossed out~, and <https://google.com|this is a link>"
}
}
但是我正在努力了解如何将此代码块更改为
string jsonValue = JsonConvert.SerializeObject(new
{
type = "section",
text = "Some text \n new line \t tab",
}
答案 0 :(得分:2)
您需要执行以下操作,text
属性是一个对象,因此只需创建另一个匿名对象。
string jsonValue = JsonConvert.SerializeObject(new
{
type = "section",
text = new
{
type = "mrkdwn",
text = "This is a mrkdwn section block :ghost: *this is bold*, and ~this is crossed out~, and <https://google.com|this is a link>"
}
}