//public Task<HttpResponseMessage> HandleMethodAsync(MethodInfo method, // KeyValuePair<ParameterInfo, object>[] queryParameters, // IApplication httpApp, HttpRequestMessage request, // MethodHandlingDelegate continueExecution) //{ // if (!request.Headers.Contains("X-Teams-Notify")) // return continueExecution(method, queryParameters, httpApp, request); // var teamsNotifyParams = request.Headers.GetValues("X-Teams-Notify"); // if(!teamsNotifyParams.Any()) // return continueExecution(method, queryParameters, httpApp, request); // var teamsNotifyParam = teamsNotifyParams.First(); // return AppSettings.ApplicationInsights.TeamsHook.ConfigurationUri( // async teamsHookUrl => // { // var response = continueExecution(method, queryParameters, httpApp, request); // var message = await CreateMessageCardAsync(method, teamsNotifyParam, httpApp, request); // using (var client = new HttpClient()) // { // var teamsRequest = new HttpRequestMessage(HttpMethod.Post, teamsHookUrl); // var messageString = JsonConvert.SerializeObject(message); // teamsRequest.Content = new StringContent(messageString); // teamsRequest.Content.Headers.ContentType = // new System.Net.Http.Headers.MediaTypeHeaderValue("application/json"); // var response = await client.SendAsync(teamsRequest); // var responseMessage = await response.Content.ReadAsStringAsync(); // } // return await continueExecution(ex, method, queryParameters, // httpApp, request); // }, // (why) => continueExecution(ex, method, queryParameters, // httpApp, request)); //} private static async Task <MessageCard> CreateMessageCardAsync(Exception ex, MethodInfo method, IApplication httpApp, IHttpRequest request) { var messageCard = await CreateMessageCardAsync(method, ex.Message, summary : "Server Exception", httpApp, request); var stackTrackSection = new MessageCard.Section { title = "Stack Trace", text = $"<blockquote>{ex.StackTrace.Replace("\r\n", "<p />")}</blockquote>", }; messageCard.sections = messageCard.sections .Append(stackTrackSection) .ToArray(); var vsoBugAction = new MessageCard.ActionCard { type = "ActionCard", name = "Create Bug in Visual Studio Online", inputs = new MessageCard.ActionCard.Input[] { new MessageCard.ActionCard.Input { type = "TextInput", id = "bugtitle", title = "Title", isMultiline = false, }, new MessageCard.ActionCard.Input { type = "DateInput", id = "dueDate", title = "Select a date", }, new MessageCard.ActionCard.Input { type = "TextInput", id = "comment", title = "Enter your comment", isMultiline = true, }, } }; messageCard.potentialAction = messageCard.potentialAction .Append(vsoBugAction) .ToArray(); return(messageCard); }
private static async Task <MessageCard> CreateMessageCardAsync( string title, string summary, MonitoringRequest monitoringRequest, IApplication httpApp, IHttpRequest request, Func <MessageCard.Section> getRequestInformation) { var appName = AppSettings.ApplicationInsights.TeamsAppIdentification .ConfigurationString( x => x, (why) => httpApp.GetType().FullName); var appImage = AppSettings.ApplicationInsights.TeamsAppImage .ConfigurationString( x => new Uri(x), (why) => default); var content = await ReadContentAsync(); var utcOffset = "Central Standard Time" .FindSystemTimeZone() .GetUtcOffset(DateTime.UtcNow); var sections = new MessageCard.Section[] { new MessageCard.Section { activityTitle = appName, activitySubtitle = (DateTime.UtcNow + utcOffset).ToString("f"), activityImage = appImage, }, getRequestInformation(), new MessageCard.Section { title = "Headers", markdown = false, // so that underscores are not stripped facts = request.Headers .Select( header => new MessageCard.Section.Fact { name = $"{header.Key}:", value = header.Value.Join(","), }) .ToArray(), }, new MessageCard.Section { title = "Content", text = $"<blockquote>{content}</blockquote>", } }; if (request.Properties.ContainsKey(HttpApplication.DiagnosticsLogProperty)) { var diagnosticsLogs = (string[])request.Properties[HttpApplication.DiagnosticsLogProperty]; sections = sections .Append( new MessageCard.Section { title = "Log", text = $"<blockquote>{diagnosticsLogs.Join("<br />")}</blockquote>", }) .ToArray(); } if (request.Properties.TryGetValue(EnableProfilingAttribute.ResponseProperty, out object profileObj)) { var profile = (IProfile)profileObj; var log = profile.Events .NullToEmpty() .OrderBy(kvp => kvp.Key) .Select(diagEvent => $"<b>{diagEvent.Key}</b>:<i>{diagEvent.Value}</i>") .Join("<br />"); sections = sections .Append( new MessageCard.Section { title = "Diagnostics", text = $"<blockquote>{log}</blockquote>", }) .ToArray(); } var postmanLink = new QueryableServer <Api.Azure.Monitoring.MonitoringRequest>(request) .Where(mr => mr.monitoringRequestRef == monitoringRequest.monitoringRequestRef) .Where(mr => mr.when == monitoringRequest.when) .HttpAction(MonitoringRequest.PostmanAction) .Location() .SignWithAccessTokenAccount(Guid.NewGuid(), Guid.NewGuid(), DateTime.UtcNow.AddYears(1), url => url); var message = new MessageCard { summary = summary, themeColor = "F00807", title = title, sections = sections, potentialAction = new MessageCard.ActionCard[] { new MessageCard.ActionCard { name = "Postman", type = "OpenUri", targets = new MessageCard.ActionCard.Target[] { new MessageCard.ActionCard.Target { os = "default", uri = postmanLink, } } }, new MessageCard.ActionCard { name = "Application Insights", type = "OpenUri", targets = new MessageCard.ActionCard.Target[] { new MessageCard.ActionCard.Target { os = "default", uri = new Uri("https://www.example.com/ai/message/1234"), } } }, new MessageCard.ActionCard { type = "ActionCard", name = "Run in TestFramework", inputs = new MessageCard.ActionCard.Input[] { new MessageCard.ActionCard.Input { type = "TextInput", id = "comment", title = "Test ID", isMultiline = false, }, new MessageCard.ActionCard.Input { type = "MultichoiceInput", id = "move", title = "Pick a test function", choices = new MessageCard.ActionCard.Input.Choice [] { new MessageCard.ActionCard.Input.Choice { display = "Unauthenticated", value = "unauthenticated", }, new MessageCard.ActionCard.Input.Choice { display = "Physicial Redirect", value = "redirect", }, new MessageCard.ActionCard.Input.Choice { display = "Session Authenticated", value = "session", }, new MessageCard.ActionCard.Input.Choice { display = "Account Authenticated", value = "account", }, }, } } }, } }; return(message); async Task <string> ReadContentAsync() { if (!request.HasBody) { return(string.Empty); } try { if (request.RequestHeaders.ContentType.IsTextType()) { return(await request.ReadContentAsStringAsync()); } var byteContent = await request.ReadContentAsync(); return(byteContent.ToBase64String()); } catch (Exception) { return(string.Empty); } } }
public async Task <string> TeamsNotifyAsync(Type controllerType, IInvokeResource resourceInvoker, IApplication httpApp, IHttpRequest request, IHttpResponse response, string teamsNotifyParam, string collectionFolder) { var monitoringRequest = await Api.Azure.Monitoring.MonitoringRequest.CreateAsync( controllerType, resourceInvoker, httpApp, request, collectionFolder); var monitoringRequestId = monitoringRequest.id.ToString(); var responseParam = response.Headers .Where(hdr => hdr.Key == Middleware.HeaderStatusName) .Where(hdr => hdr.Value.AnyNullSafe()) .First( (hdr, next) => hdr.Value.First(), () => ""); var message = await CreateMessageCardAsync( teamsNotifyParam, $"{request} = {response.StatusCode} / {response.ReasonPhrase}", monitoringRequest, httpApp, request, () => { var cardSection = new MessageCard.Section { title = "Request/Response Information", markdown = false, // so that underscores are not stripped facts = new MessageCard.Section.Fact[] { new MessageCard.Section.Fact { name = "Response Param:", value = responseParam, }, new MessageCard.Section.Fact { name = "Http Method:", value = request.Method.Method, }, new MessageCard.Section.Fact { name = "URL:", value = request.GetAbsoluteUri().OriginalString, }, new MessageCard.Section.Fact { name = "Status Code:", value = $"{response.StatusCode.ToString()} / {(int)response.StatusCode}", }, new MessageCard.Section.Fact { name = "Reason:", value = response.ReasonPhrase, }, new MessageCard.Section.Fact { name = "RequestID:", value = monitoringRequestId, }, }, }; return(cardSection); }); return(await message.SendAsync(teamsHookUrl)); }