Skip to content

Commit

Permalink
Add function for sending generated report to Teams channel
Browse files Browse the repository at this point in the history
  • Loading branch information
MuhammadUsama-afk-equinor authored and UsamaEquinorAFK committed Dec 1, 2023
1 parent 81f2edd commit 178ee12
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions backend/api/EventHandlers/InspectionFindingEventHandler.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
using System.Globalization;
using System.Net.Http.Headers;
using System.Text;
using Api.Database.Models;
using Api.Services;
using Azure.Identity;
using Azure.Security.KeyVault.Secrets;
namespace Api.EventHandlers
{
public class InspectionFindingEventHandler(IConfiguration configuration,
Expand Down Expand Up @@ -33,6 +36,14 @@ protected override async Task ExecuteAsync(CancellationToken stoppingToken)
string messageString = GenerateReportFromFindingsReportsList(findingsList);

string adaptiveCardJson = GenerateAdaptiveCard(messageString);

string url = GetWebhookURL("TeamsInspectionFindingsWebhook");

var client = new HttpClient();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
var content = new StringContent(adaptiveCardJson, Encoding.UTF8, "application/json");
var response = await client.PostAsync(url, content, stoppingToken);
//if (response.StatusCode == StatusCodes.Status201Created) ;
}
}
catch (OperationCanceledException) { throw; }
Expand Down Expand Up @@ -122,6 +133,37 @@ public static string GenerateAdaptiveCard(string messageContent)
}}";
return adaptiveCardJson;
}
public static string GetWebhookURL(string secretName)
{
string environment = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT")!;

string projectPath = Path.Combine(
Directory.GetParent(Directory.GetCurrentDirectory())!.FullName,
"api"
);

var config = new ConfigurationBuilder()
.SetBasePath(projectPath)
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
.AddJsonFile($"appsettings.{environment}.json", optional: true)
.AddEnvironmentVariables()
.Build();

string? keyVaultUri = config.GetSection("KeyVault")["VaultUri"] ?? throw new KeyNotFoundException("No key vault in config");

var keyVault = new SecretClient(
new Uri(keyVaultUri),
new DefaultAzureCredential(
new DefaultAzureCredentialOptions { ExcludeSharedTokenCacheCredential = true }
)
);

string webhookURL = keyVault
.GetSecret(secretName)
.Value.Value;

return webhookURL;
}
}

public class Finding(string tagId, string plantName, string areaName, string findingDescription, DateTime timestamp, string robotName)
Expand Down

0 comments on commit 178ee12

Please sign in to comment.