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.