Skip to content

Commit

Permalink
Rework Teams publishing, decrease max message size and use HttpClient…
Browse files Browse the repository at this point in the history
…Factory
  • Loading branch information
GeorgDangl committed May 2, 2024
1 parent f7ab533 commit c058f0a
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
15 changes: 11 additions & 4 deletions src/IPA.Bcfier.App/Services/TeamsMessagesService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,21 @@
using Newtonsoft.Json;
using SixLabors.ImageSharp.Formats.Png;
using SixLabors.ImageSharp.Processing;
using System.Drawing.Text;
using System.Text;

namespace IPA.Bcfier.App.Services
{
public class TeamsMessagesService
{
private readonly BcfierDbContext _context;
private readonly HttpClient _httpClient;

public TeamsMessagesService(BcfierDbContext context)
public TeamsMessagesService(BcfierDbContext context,
HttpClient httpClient)
{
_context = context;
_httpClient = httpClient;
}

public async Task AnnounceNewCommentInProjectTopicAsync(Guid projectId,
Expand Down Expand Up @@ -59,6 +63,8 @@ public async Task AnnounceNewCommentInProjectTopicAsync(Guid projectId,
};
teamsMessage.Sections = new List<TeamsSection> { mainSection };

teamsMessage.Summary = "Update via BCFier";

if (!string.IsNullOrWhiteSpace(message.ViewpointBase64))
{
teamsMessage.Sections.Add(new TeamsSection
Expand All @@ -81,19 +87,20 @@ private async Task SendTeamsMessageAsync(TeamsMessage message,
{
// Apparently, the max message size for Teams webhooks is 28.000 characters
// so we're ensuring we resize images to fit in there
var messageJson = GetTeamsMessageWithMaxSizeInBytes(message, 28_000);
// Also, we seemed to have problems with a max size of 28.000, so we've lowered it
// to 25.000
var messageJson = GetTeamsMessageWithMaxSizeInBytes(message, 25_000);
if (string.IsNullOrWhiteSpace(messageJson))
{
// Looks like the message was too large for the webhook
return;
}

using var httpClient = new HttpClient();
var body = new StringContent(messageJson, Encoding.Default, "application/json");
var request = new HttpRequestMessage(HttpMethod.Post, teamsWebhookUrl);
request.Content = body;

await httpClient.SendAsync(request);
await _httpClient.SendAsync(request);
}

private static string? GetTeamsMessageWithMaxSizeInBytes(TeamsMessage message,
Expand Down
1 change: 1 addition & 0 deletions src/IPA.Bcfier.App/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public void ConfigureServices(IServiceCollection services)
services.AddTransient<SettingsService>();
services.AddHttpContextAccessor();
services.AddSingleton(new RevitParameters());
services.AddHttpClient<TeamsMessagesService>();

services.Configure<ApiBehaviorOptions>(options =>
{
Expand Down

0 comments on commit c058f0a

Please sign in to comment.