我需要将图像上传到Slack而不将其存储在系统中,然后使用file.Upload方法的帮助。
我想将htmlContent转换为图像并将其发送到松弛状态。 我能够将其转换为图像字节流,并将其存储在本地,然后将文件上传到Slack,然后获取链接,并与image_url一起发送消息到Slack。
我想要做的是将图像直接上传到Slack,而不将其存储在本地。 我看到我们可以使用files.upload上传文件或将内容松弛,但是我无法使用内容上传。
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();
}
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);
我想要的是直接将图像内容从C#代码上传到Slack,避免将代码存储在本地图像中,然后再将同一张图片上传到Slack。
有人可以帮助我了解如何执行此操作吗?
我想我无法正确使用file.upload和内容而不是文件。