如何 post 带有本地附件的 Slack 格式邮件?

How to post Slack formated message with local attachment?

基本上我想post把本地图片文件放到slack中。该方法只接受 URL 参数,但我想传递文件的路径。有没有办法使用格式化消息来实现这一点? 顺便说一句,我从这个网站上得到了这个例子:

 public class SlackAttachment
    {
        public string fallback { get; set; }
...
        public string author_name { get; set; }
        public string image_url { get; set; }

    } 
public static void SendMessageToSlack()
        {

            string token= "myTokenHere";


            var sampleAttachment = new SlackAttachment[]

            {
                new SlackAttachment {

                    fallback = "this did not work",
                    text = "text here",
                    color = "0b7c1e",
                    pretext = "",
                    author_name = "myName",
                    author_icon =  @"https://i.imgur.com/02sddfQMt9p.png",
                    author_link = "",
                    title = "no title",
                    title_link = "Title link here",
                  //image_url = @"https://i.imgur.com/U9S0CDG.png",
                  // cannot replace this with 
                  //   C:\Users\Public\image.png

                    thumb_url = @"",
                    footer = "footer here",
                    footer_icon =  migrationIcon

                             },
        };

            var attachmentsJson = JsonConvert.SerializeObject(sampleAttachment);
            var data = new NameValueCollection();
            data["token"] = token;
            data["channel"] = channelName;
            data["as_user"] = "true";          
            data["text"] = "";
            data["attachments"] = attachmentsJson;

            var client = new WebClient();
            var response = client.UploadValues("https://slack.com/api/chat.postMessage", "POST", data);
            string responseInString = Encoding.UTF8.GetString(response);
            Console.WriteLine(responseInString);
        }

要将本地图像文件附加到邮件中,您需要先上传它,这样您才能获得 public URL。然后您可以在邮件附件中使用 URL。

这适用于任何 public 图像服务(例如 imgur.com),您还可以使用任何 Slack 工作区作为 public 图像文件的宿主。

查看 了解详细信息,包括 C# 中的完整示例。