【发布时间】:2018-11-15 21:48:20
【问题描述】:
我正在尝试将使用 Slack 机器人应用程序 API 的 slack 通知合并到我的 C# 应用程序中。下面的代码运行良好,但 attachments 字段使用的格式使得编辑和维护变得非常困难......必须有一种更简单的方法来填充该 json 数组?
我尝试了多种方法来编写它,但除了这种笨拙的语法外,我无法让它正常工作。
var data = new NameValueCollection
{
["token"] = "token", // Removed my actual token from here obviously
["channel"] = "channel", // Same with the channel
["as_user"] = "true",
["text"] = "test message 2",
["attachments"] = "[{\"fallback\":\"dummy\", \"text\":\"this is an attachment\", \"color\":\"#F35A00\", \"title\" : \"Title\", \"title_link\": \"http://www.google.com\"}]"
};
var client = new WebClient();
var response = client.UploadValues("https://slack.com/api/chat.postMessage", "POST", data);
【问题讨论】: