3

Basically I want to post local image files into slack. The method only accepts URL parameters but I want to pass the path of the file. Is there a way to achieve this using formatted message ? I had the example from this site by the way :

 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);
        }
Erik Kalkoken
  • 30,467
  • 8
  • 79
  • 114
Coder
  • 121
  • 9

1 Answers1

3

In order to attach a local image file to a message you need to first upload it so you get a public URL. Then you can use that URL in your message attachment.

This works with any public image service (e.g. imgur.com) and you can also use any Slack workspace as host for public image files.

Check out this answer for details including a complete example in C#.

Erik Kalkoken
  • 30,467
  • 8
  • 79
  • 114
  • Thank you . I am familiar more with the first example (chat post message) However I get this error at run time: System.NullReferenceException: 'Object reference not set to an instance of an object.' UploaderMessenger.SlackExample.SlackFileResponse.file.get returned null This is the line that generates the error: String imageUrl = fileResponse2.file.permalink_public; – Coder Jan 03 '19 at 08:11
  • Okay is there at least a way to use the second method to only upload the image and obtain url without posting? – Coder Jan 05 '19 at 20:51
  • Sure. If you don’t provide a channel with file.upload it will not post – Erik Kalkoken Jan 05 '19 at 21:43
  • Thanks . While I try that do you happen to know why I get this error? see img: https://i.imgur.com/RalifhT.png – Coder Jan 06 '19 at 07:18
  • Looks to me like the response from the API in `responseString2` does not contain the property `file.permalink_public`, because the call apparently failed. It says `ok` = `false` and `error` = `not allowed`. So your token does not appear to have the required scopes to upload a file. You need either `bot`or `files.write.user`. – Erik Kalkoken Jan 06 '19 at 11:52
  • btw: my example code is missing some proper error handling. Normally you would check for `ok` being true before de-serializing the object. – Erik Kalkoken Jan 06 '19 at 11:57
  • You are right !! I didn't have access permission. I went to the following link :https://Myworkspacename.slack.com/admin/settings#public_file_urls and then enabled the option in the photo : https://i.imgur.com/om9GmsS.png – Coder Jan 06 '19 at 16:43
  • 1
    As for the error handler of course I will add them . I was just testing . Thank you so much . You are a slack hero indeed !! – Coder Jan 06 '19 at 16:44