Created
January 16, 2020 09:01
-
-
Save galiana/37238a70c68f25670e64b0930142bcc7 to your computer and use it in GitHub Desktop.
Transforming MongoDB WebHooks notifications payload into Microsoft teams message cards
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#r "Newtonsoft.Json" | |
using System.Net; | |
using Microsoft.AspNetCore.Mvc; | |
using Microsoft.Extensions.Primitives; | |
using Newtonsoft.Json; | |
using System.Net.Http.Headers; | |
public static async Task<IActionResult> Run(HttpRequest req, ILogger log) | |
{ | |
log.LogInformation("C# HTTP trigger function processed a request."); | |
string requestBody = await new StreamReader(req.Body).ReadToEndAsync(); | |
ActionResult result; | |
if(!string.IsNullOrEmpty(requestBody)) | |
{ | |
dynamic data = JsonConvert.DeserializeObject(requestBody); | |
log.LogInformation(requestBody); | |
//Push message through Teams | |
using (var client = new HttpClient()) | |
{ | |
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); | |
client.DefaultRequestHeaders.Add("User-Agent", "AzureFunctions"); | |
//Copy paste uri from Teams | |
var uri = "https://outlook.office.com/webhook/XXXXX/IncomingWebhook/YYYY/ZZZZ"; | |
String content = @"{ | |
""@type"": ""MessageCard"", | |
""@context"": ""https://schema.org/extensions"", | |
""summary"": ""Card \""Test card\"""", | |
""themeColor"": ""0078D7"", | |
""title"": ""--title--"", | |
""sections"": [ | |
{ | |
""activityTitle"": ""--stitle--"", | |
""activitySubtitle"": ""--date--"", | |
""activityImage"": ""--icon--"", | |
""facts"": [ | |
{ | |
""name"": ""Custer:"", | |
""value"": ""--clustername--"" | |
}, | |
{ | |
""name"": ""Replica set:"", | |
""value"": ""--repset--"" | |
}, | |
{ | |
""name"": ""Server:"", | |
""value"": ""--handp--"" | |
}, | |
{ | |
""name"": ""Metric:"", | |
""value"": ""--stitle--"" | |
}, | |
{ | |
""name"": ""Metric value:"", | |
""value"": ""--value--"" | |
}, | |
{ | |
""name"": ""Type:"", | |
""value"": ""--type--"" | |
}, | |
{ | |
""name"": ""Event:"", | |
""value"": ""--event--"" | |
} | |
], | |
""text"": ""--MESSAGE--"" | |
} | |
], | |
""potentialAction"": [ | |
{ | |
""@type"": ""OpenUri"", | |
""name"": ""Check the stats"", | |
""targets"": [ | |
{ | |
""os"": ""default"", | |
""uri"": ""--url--"", | |
""url"": ""--url--"" | |
} | |
] | |
} | |
] | |
}"; | |
if(data!= null) | |
{ | |
string value = data.metricName; | |
if(!string.IsNullOrEmpty(value)) | |
content = content.Replace(@"--stitle--",value); | |
value = data.updated; | |
if(!string.IsNullOrEmpty(value)) | |
content = content.Replace(@"--date--",value); | |
value = data.humanReadable; | |
if(!string.IsNullOrEmpty(value)) | |
content = content.Replace(@"--MESSAGE--",value); | |
value = data.replicaSetName; | |
if(!string.IsNullOrEmpty(value)) | |
content = content.Replace(@"--repset--",value); | |
value = data.hostnameAndPort; | |
if(!string.IsNullOrEmpty(value)) | |
content = content.Replace(@"--handp--",value); | |
value = data.clusterName; | |
if(!string.IsNullOrEmpty(value)) | |
content = content.Replace(@"--clustername--",value); | |
value = data.status; | |
if(!string.IsNullOrEmpty(value)) | |
{ | |
content = content.Replace(@"--status--",value); | |
if(value =="OPEN") | |
{ | |
content = content.Replace(@"--icon--","https://www.clearpeople.com/-/media/Images/TeamsIntegrations/notification%20png.png"); | |
content = content.Replace(@"--title--","New alert triggered"); | |
} | |
else if(value =="CLOSED") | |
{ | |
content = content.Replace(@"--icon--","https://www.clearpeople.com/-/media/Images/TeamsIntegrations/correct%20jpg.jpg"); | |
content = content.Replace(@"--title--","Alert closed"); | |
} | |
} | |
value = data.typeName; | |
if(!string.IsNullOrEmpty(value)) | |
content = content.Replace(@"--type--",value); | |
value = data.eventTypeName; | |
if(!string.IsNullOrEmpty(value)) | |
content = content.Replace(@"--event--",value); | |
if(data.currentValue!=null) | |
{ | |
value = data.currentValue.number; | |
content = content.Replace(@"--value--",value); | |
} | |
if(data.links!= null && data.links[0]!= null) | |
{ | |
value = data.links[0].href; | |
content = content.Replace(@"--url--",value ); | |
} | |
} | |
StringContent TeamMsg = new StringContent(content); | |
log.LogInformation(content); | |
HttpResponseMessage response = client.PostAsync(uri,TeamMsg).Result; | |
var responseString = response.Content.ReadAsStringAsync().Result; | |
log.LogInformation(responseString); | |
} | |
result = new OkObjectResult($"Hello"); | |
} | |
else | |
{ | |
result = new BadRequestObjectResult("Please pass some JSON in the request body"); | |
} | |
return result; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment