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

时间:2019-01-02 10:41:49

标签: c# image post local 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);
        }

1 个答案:

答案 0 :(得分:1)

为了将本地图像文件附加到邮件,您需要先上传它,以便获得公共URL。然后,您可以在邮件附件中使用该URL。

这可用于任何公共图像服务(例如imgur.com),并且您也可以将任何Slack工作区用作公共图像文件的主机。

查看this answer以获得包括C#中完整示例在内的详细信息。