Created
May 23, 2022 17:53
-
-
Save pderendinger-everse/4be551d5ebfdcd772b1d82500b2fb7fc to your computer and use it in GitHub Desktop.
slackBot Poster Uploader
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private async void UploadPDF(byte[] pdfString, string channel_id, string user_name) | |
{ | |
using (var httpClient = new HttpClient()) | |
{ | |
HttpResponseMessage? response = null; | |
var request = new HttpRequestMessage(new HttpMethod("POST"), "https://slack.com/api/files.upload"); | |
JsonElement JO = new JsonElement(); | |
request.Headers.TryAddWithoutValidation("Authorization", "Bearer XXXX"); | |
var multipartContent = new MultipartFormDataContent(); | |
multipartContent.Add(new ByteArrayContent(pdfString), "file", "XXXX.pdf"); | |
multipartContent.Add(new StringContent($"Hello, {user_name}. Here is your new tax information"), "initial_comment"); | |
multipartContent.Add(new StringContent(channel_id, System.Text.Encoding.UTF8), "channels"); | |
request.Content = multipartContent; | |
try | |
{ | |
var e = await httpClient.SendAsync(request); | |
var content = e.Content.ReadAsStringAsync().Result; | |
LambdaLogger.Log("PDF upload process result: " + content); | |
} | |
catch (Exception ex) | |
{ | |
LambdaLogger.Log("PDF upload process failed, retrying: " + ex.Message); | |
UploadPDF(pdfString, channel_id, user_name); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment