//public Task<SlackBot> GetAsync(SlackBot sb); public async Task UploadFileAsync(string token, Stream stream, string fileName, string channels) { var multiForm = new MultipartFormDataContent(); multiForm.Add(new StringContent(token), "token"); multiForm.Add(new StringContent(channels), "channels"); multiForm.Add(new StreamContent(stream), "file", fileName); var url = "https://slack.com/api/files.upload"; var response = await client.PostAsync(url, multiForm); var responseJson = await response.Content.ReadAsStringAsync(); SlackFileResponse fileResponse = JsonConvert.DeserializeObject <SlackFileResponse>(responseJson); if (fileResponse.ok == false) { throw new Exception( "failed to upload message: " + fileResponse.error ); } else { Console.WriteLine( "Uploaded new file with id: " + fileResponse.file.id ); } }
// main method with logic public static void Main() { var parameters = new NameValueCollection(); // put your token here parameters["token"] = "xoxp-YOUR-TOKEN"; parameters["channels"] = "test"; var client = new WebClient(); client.QueryString = parameters; byte[] responseBytes = client.UploadFile( "https://slack.com/api/files.upload", "D:\\temp\\Stratios_down.jpg" ); String responseString = Encoding.UTF8.GetString(responseBytes); SlackFileResponse fileResponse = JsonConvert.DeserializeObject <SlackFileResponse>(responseString); }
// sends a slack message asynchronous // throws exception if message can not be sent public static async Task UploadFileAsync(string token, string path, string channels) { // we need to send a request with multipart/form-data var multiForm = new MultipartFormDataContent(); // add API method parameters multiForm.Add(new StringContent(token), "token"); multiForm.Add(new StringContent(channels), "channels"); // add file and directly upload it FileStream fs = File.OpenRead(path); multiForm.Add(new StreamContent(fs), "file", Path.GetFileName(path)); // send request to API var url = "https://slack.com/api/files.upload"; var response = await client.PostAsync(url, multiForm); // fetch response from API var responseJson = await response.Content.ReadAsStringAsync(); // convert JSON response to object SlackFileResponse fileResponse = JsonConvert.DeserializeObject <SlackFileResponse>(responseJson); // throw exception if sending failed if (fileResponse.ok == false) { throw new Exception( "failed to upload message: " + fileResponse.error ); } else { Console.WriteLine( "Uploaded new file with id: " + fileResponse.file.id ); } }
// main method with logic public static void Main() { String token = "xoxp-YOUR-TOKEN"; ///////////////////// // Step 1: Upload file to Slack var parameters = new NameValueCollection(); // put your token here parameters["token"] = token; var client1 = new WebClient(); client1.QueryString = parameters; byte[] responseBytes1 = client1.UploadFile( "https://slack.com/api/files.upload", "C:\\Temp\\Stratios_down.jpg" ); String responseString1 = Encoding.UTF8.GetString(responseBytes1); SlackFileResponse fileResponse1 = JsonConvert.DeserializeObject <SlackFileResponse>(responseString1); String fileId = fileResponse1.file.id; ///////////////////// // Step 2: Make file public and get the URL 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; ///////////////////// // Step 3: Send message including freshly uploaded image as attachment var parameters3 = new NameValueCollection(); parameters3["token"] = token; parameters3["channel"] = "test_new"; parameters3["text"] = "test message 2"; // 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); }
public async Task SendAttachmentAsync(string channel, string Message, byte[] bytea, string AttachmentName) { try { //FileStream str = File.OpenRead(@"\430831-most-popular-relaxing-desktop-background-1920x1080.jpg"); //byte[] fBytes = new byte[str.Length]; //str.Read(fBytes, 0, fBytes.Length); //str.Close(); //Data["channels"] = channel; //Data["text"] = Message; //Client.QueryString = Data; //string boundary = "------------------------" + DateTime.Now.Ticks.ToString("x"); //Client.Headers.Add("Content-Type", "multipart/form-data; boundary=" + boundary); //string fileData = Client.Encoding.GetString(bytea); //string package = string.Format("--{0}\r\nContent-Disposition: form-data; name=\"file\"; filename=\"{1}\"\r\nContent-Type: {1}\r\n\r\n{3}\r\n--{0}--\r\n", boundary, AttachmentName, ".pdf", fileData); //var nfile = Client.Encoding.GetBytes(package); //string encodedfile = Encoding.ASCII.GetString(nfile); //string url = "https://slack.com/api/files.upload?token=" + Config.OAuthAccessToken + "&content=" + nfile + "&channels=" + channel + "&pretty=1"; //byte[] responseBytes = Client.UploadData(url, nfile); //String responseString = Encoding.UTF8.GetString(responseBytes); //Console.WriteLine(responseString); var ms = new System.IO.MemoryStream(); ms.Write(bytea, 0, bytea.Length); ms.Position = 0; multiForm.Add(new StringContent(channel), "channels"); multiForm.Add(new StringContent(Message), "initial_comment"); multiForm.Add(new StreamContent(ms), "file", AttachmentName); var url = "https://slack.com/api/files.upload"; var response = await client.PostAsync(url, multiForm); var responseJson = await response.Content.ReadAsStringAsync(); SlackFileResponse fileResponse = JsonConvert.DeserializeObject <SlackFileResponse>(responseJson); if (fileResponse.ok == false) { throw new Exception( "failed to upload message: " + fileResponse.error ); } else { Console.WriteLine( "Uploaded new file with id: " + fileResponse.file.id ); } } catch (Exception e) { Console.WriteLine("SendAttachement Slack Exception" + e.Message + e.StackTrace); } }