From 97c3c39a2fd8937bba3831f8163e2df2bded47b0 Mon Sep 17 00:00:00 2001 From: Tatsuro Shibamura Date: Mon, 6 Jan 2025 15:26:08 +0900 Subject: [PATCH] Adding support to Teams workflow webhook --- .../Internal/LegacyTeamsPayloadBuilder.cs | 39 ++++++++ .../Internal/TeamsPayloadBuilder.cs | 95 +++++++++++++++---- KeyVault.Acmebot/Startup.cs | 7 +- 3 files changed, 122 insertions(+), 19 deletions(-) create mode 100644 KeyVault.Acmebot/Internal/LegacyTeamsPayloadBuilder.cs diff --git a/KeyVault.Acmebot/Internal/LegacyTeamsPayloadBuilder.cs b/KeyVault.Acmebot/Internal/LegacyTeamsPayloadBuilder.cs new file mode 100644 index 00000000..894445c1 --- /dev/null +++ b/KeyVault.Acmebot/Internal/LegacyTeamsPayloadBuilder.cs @@ -0,0 +1,39 @@ +using System; +using System.Collections.Generic; + +namespace KeyVault.Acmebot.Internal; + +internal class LegacyTeamsPayloadBuilder : IWebhookPayloadBuilder +{ + public object BuildCompleted(string certificateName, DateTimeOffset? expirationDate, IEnumerable dnsNames, string acmeEndpoint) + { + return new + { + title = "Acmebot", + text = @$"A new certificate has been issued. + +**Certificate Name**: {certificateName} + +**Expiration Date**: {expirationDate} + +**ACME Endpoint**: {acmeEndpoint} + +**DNS Names**: {string.Join(", ", dnsNames)}", + themeColor = "2EB886" + }; + } + + public object BuildFailed(string functionName, string reason) + { + return new + { + title = "Acmebot", + text = @$"**{functionName}** + +**Reason** + +{reason}", + themeColor = "A30200" + }; + } +} diff --git a/KeyVault.Acmebot/Internal/TeamsPayloadBuilder.cs b/KeyVault.Acmebot/Internal/TeamsPayloadBuilder.cs index 31a8801c..5bc0e2ab 100644 --- a/KeyVault.Acmebot/Internal/TeamsPayloadBuilder.cs +++ b/KeyVault.Acmebot/Internal/TeamsPayloadBuilder.cs @@ -9,17 +9,53 @@ public object BuildCompleted(string certificateName, DateTimeOffset? expirationD { return new { - title = "Acmebot", - text = @$"A new certificate has been issued. - -**Certificate Name**: {certificateName} - -**Expiration Date**: {expirationDate} - -**ACME Endpoint**: {acmeEndpoint} - -**DNS Names**: {string.Join(", ", dnsNames)}", - themeColor = "2EB886" + type = "message", + attachments = new[] + { + new + { + contentType = "application/vnd.microsoft.card.adaptive", + content = new + { + type = "AdaptiveCard", + body = new object[] + { + new + { + type = "TextBlock", + text = "A new certificate has been issued.", + wrap = true + }, + new{ + type = "FactSet", + facts = new object[] + { + new + { + title = "Certificate Name", + value = certificateName + }, + new + { + title = "Expiration Date", + value = expirationDate + }, + new + { + title = "ACME Endpoint", + value = acmeEndpoint + }, + new + { + title = "DNS Names", + value = string.Join("\n", dnsNames) + } + } + } + } + } + } + } }; } @@ -27,13 +63,36 @@ public object BuildFailed(string functionName, string reason) { return new { - title = "Acmebot", - text = @$"**{functionName}** - -**Reason** - -{reason}", - themeColor = "A30200" + type = "message", + attachments = new[] + { + new + { + contentType = "application/vnd.microsoft.card.adaptive", + content = new + { + type = "AdaptiveCard", + body = new object[] + { + new + { + type = "TextBlock", + size = "Medium", + weight = "Bolder", + text = functionName, + style = "heading", + wrap = true + }, + new + { + type = "TextBlock", + text = reason, + wrap = true + } + } + } + } + } }; } } diff --git a/KeyVault.Acmebot/Startup.cs b/KeyVault.Acmebot/Startup.cs index b96f9e8b..7f16a0b1 100644 --- a/KeyVault.Acmebot/Startup.cs +++ b/KeyVault.Acmebot/Startup.cs @@ -97,11 +97,16 @@ public override void Configure(IFunctionsHostBuilder builder) return new SlackPayloadBuilder(); } - if (options.Webhook.Host.EndsWith(".office.com", StringComparison.OrdinalIgnoreCase)) + if (options.Webhook.Host.EndsWith(".logic.azure.com", StringComparison.OrdinalIgnoreCase)) { return new TeamsPayloadBuilder(); } + if (options.Webhook.Host.EndsWith(".office.com", StringComparison.OrdinalIgnoreCase)) + { + return new LegacyTeamsPayloadBuilder(); + } + return new GenericPayloadBuilder(options); });