'How to attach a BitMap (or an image) to Slack message as attachment [C#]
I am using IHttpClientFactory and doing a POST request with parameters to receive an image. Then I need to attach this image to the slack message. In a normal way, we have to publish our image to a public cloud then attach the image URL to the slack attachment BUT I cannot do so because the image is private. This is my current code.
using (HttpClient httpClient = _clientFactory.CreateClient())
{
httpClient.DefaultRequestHeaders.Add("Accept", "image/*");
var parameters = new
{
names = "name x, name y",
title = "some title!",
msg = @"msg body",
time = "",
images = "",
};
var serializedParams = JsonConvert.SerializeObject(parameters);
HttpContent paramContent = new StringContent(serializedParams, Encoding.UTF8, "application/json");
using (HttpResponseMessage response = await httpClient.PostAsync($"{"the nedpoint", paramContent, new CancellationToken()))
{
if(response != null && response.IsSuccessStatusCode)
{
using(var stream = await response.Content.ReadAsStreamAsync())
{
using (var mStream = new MemoryStream())
{
await stream.CopyToAsync(mStream);
generatedImage = new Bitmap(System.Drawing.Image.FromStream(mStream));
}
}
}
}
}
Now I want to attach the generatedImage to the following attachment
var model = new
{
id = Guid.NewGuid().ToString(),
type = "message",
text = "",
attachments = new[]
{
new {
color = "#36a64f",
title = messageTitle,
text = messageBody
}
}
};
var client = _clientFactory.CreateClient();
client.Timeout = new TimeSpan(0, 0, 30);
var serializedContent = JsonConvert.SerializeObject(model);
client.DefaultRequestHeaders.Add("Accept", "application/json");
HttpContent content = new StringContent(serializedContent, Encoding.UTF8, "application/json");
await client.PostAsync(channel, content, new CancellationToken());
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
