Skip to content

Commit

Permalink
Adding support to Teams workflow webhook (#803)
Browse files Browse the repository at this point in the history
  • Loading branch information
shibayan authored Jan 6, 2025
1 parent 97f7a10 commit 914d35b
Show file tree
Hide file tree
Showing 3 changed files with 122 additions and 19 deletions.
39 changes: 39 additions & 0 deletions KeyVault.Acmebot/Internal/LegacyTeamsPayloadBuilder.cs
Original file line number Diff line number Diff line change
@@ -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<string> 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"
};
}
}
95 changes: 77 additions & 18 deletions KeyVault.Acmebot/Internal/TeamsPayloadBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,31 +9,90 @@ 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)
}
}
}
}
}
}
}
};
}

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
}
}
}
}
}
};
}
}
7 changes: 6 additions & 1 deletion KeyVault.Acmebot/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});

Expand Down

0 comments on commit 914d35b

Please sign in to comment.