Skip to content

Instantly share code, notes, and snippets.

@pderendinger-everse
Created May 23, 2022 17:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pderendinger-everse/4be551d5ebfdcd772b1d82500b2fb7fc to your computer and use it in GitHub Desktop.
Save pderendinger-everse/4be551d5ebfdcd772b1d82500b2fb7fc to your computer and use it in GitHub Desktop.
slackBot Poster Uploader
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