0

I need help with uploading an image to Slack without storing the image in my system and then using the file.Upload method.

I want to convert an htmlContent into an image and send it to slack. I am able to convert it to an image Byte Stream and store it in my local then upload the file to slack and then fetch the link and send an message to slack along with the image_url.

What I want to do is to upload the image directly into slack without storing it in my local. I saw that we can either upload a file or the content to slack using the files.upload but I am not able to upload using the content.


Code to store image.

public void StoreLeadAnalyticImage(byte[] image)
{
   string imagePath = string.Concat( _iGlobalSettingsRespository.GetImagesRootLocation(), ImageLocationConstants.LeadAnalyticsImageLocation(), Defaults.LeadAnalytics, Defaults.FullStop, Defaults.Extension);
   var bw = new BinaryWriter(File.Open(imagePath, FileMode.OpenOrCreate));
   bw.Write(image);
   bw.Close();
}

Code to upload image and send message to slack with image url

var parameters = new NameValueCollection();
parameters["token"] = token;
var client1 = new WebClient();
client1.QueryString = parameters;
byte[] responseBytes1 = client1.UploadFile("https://slack.com/api/files.upload", imagePath);
tring responseString1 = Encoding.UTF8.GetString(responseBytes1);

SlackFileResponse fileResponse1 =
JsonConvert.DeserializeObject<SlackFileResponse>(responseString1);

String fileId = fileResponse1.file.id;

var parameters2 = new NameValueCollection();
parameters2["token"] = token;
parameters2["file"] = fileId;

var client2 = new WebClient();
byte[] responseBytes2 = client2.UploadValues("https://slack.com/api/files.sharedPublicURL", "POST", parameters2);

String responseString2 = Encoding.UTF8.GetString(responseBytes2);

SlackFileResponse fileResponse2 = JsonConvert.DeserializeObject<SlackFileResponse>(responseString2);

String imageUrl = fileResponse2.file.permalink_public;

var parameters3 = new NameValueCollection();
parameters3["token"] = token;
parameters3["channel"] = "testing";
parameters3["text"] = string.Format(Resources.LeadAnalyticsReportMailSubject, DateTime.Now.Date.AddDays(-1).ToString(Defaults.DataFormat)) + Defaults.SlackChannelTag;

// create attachment
SlackAttachment attachment = new SlackAttachment();
attachment.fallback = "this did not work";
attachment.text = "this is anattachment";
attachment.image_url = imageUrl;
SlackAttachment[] attachments = { attachment };
parameters3["attachments"] = JsonConvert.SerializeObject(attachments);

var client3 = new WebClient();
byte[] responseBytes3 = client3.UploadValues("https://slack.com/api/chat.postMessage", "POST", parameters3);

String responseString3 = Encoding.UTF8.GetString(responseBytes3);

SlackMessageResponse messageResponse = JsonConvert.DeserializeObject<SlackMessageResponse>(responseString3);

What i want is to directly upload the image contents from c# code to slack avoiding the code to store image in my local and then uploading the same image to slack.

Can someone help me understand how to do this, possibly with an example.

I guess I'm not able to get the proper way of using file.upload with content instead of file.

Vignesh
  • 3
  • 1
  • 1
  • 4
  • OK, I found a solution with `file` instead of `content`. https://stackoverflow.com/questions/53283312/no-file-data-error-when-trying-to-upload-file-to-slack – Erik Kalkoken Sep 08 '19 at 11:00
  • If you look at the code in my solution I am first loading the file into a byte array and then passing that byte array into `slackClient.UploadFile()`. That is exactly what you want. – Erik Kalkoken Sep 08 '19 at 11:03
  • `var parameters = new NameValueCollection(); parameters["token"] = token; parameters["content"] = Convert.ToBase64String(imageByteStream); var client1 = new WebClient(); byte[] responseBytes1 = client1.UploadValues( "https://slack.com/api/files.upload", parameters);` ok so this is what I did and the url that i got is [link](https://slack-files.com/T03FWTDL0-FN4L9RPU5-3b4545dbcd) its storing the string as a text instead of an image. – Vignesh Sep 08 '19 at 11:03
  • yeah, I got the same result when trying with content. Anyways, I posted you a working solution with file. It also uses HttpClient which is the recommend approach these days for making POST requests and should be used instead of WebClient. – Erik Kalkoken Sep 08 '19 at 11:06
  • @ErikKalkoken Is there a way to Add a title and tag the channel along with the image that is being sent ? Like i want to add a text and tag channel above the image – Vignesh Sep 08 '19 at 11:31
  • yes, you can specify an initial message with the parameter `initial_comment`. And a title with `title` – Erik Kalkoken Sep 08 '19 at 11:36
  • In the example I linked you have to add them to the URL, which is a bit ugly but should work just fine. If you want a cleaner approach you can look at this example which adds them in the POST body: https://stackoverflow.com/questions/53264117/how-to-upload-any-file-on-slack-via-slack-app-in-c-sharp/53284161#53284161 – Erik Kalkoken Sep 08 '19 at 11:39
  • @ErikKalkoken Since i created the token from my account its sending messages as if it is I who's sending the message, is it possible to avoid this. Like not display my username and profile picture instead display a text that We provide? – Vignesh Sep 09 '19 at 05:16
  • it looks like this [meh](https://imgur.com/Uekmo5V) can we make it look like this instead [yay](https://imgur.com/U19bGfX) – Vignesh Sep 09 '19 at 05:23
  • sure. just create a bot user in your Slack app and use the bot token to upload the file. Then it will appear to come from the bot user (which name and pic you can choose freely) – Erik Kalkoken Sep 09 '19 at 09:13
  • Oh OK alright cool thanks. – Vignesh Sep 09 '19 at 10:50

0 Answers0