public void convertslackmessage_should_return_expected_result_when_the_message_contains_a_section_block() { var block = new SlackSectionBlock() { Text = new SlackMarkdownText("A message *with some bold text* and _some italicized text_.") }; var expectedSlackMessage = CreateSlackMessage(block); var json = @"{ ""as_user"": false, ""channel"": ""#test"", ""icon_emoji"": "":smiley:"", ""mrkdwn"": false, ""text"": ""My Super Text"", ""username"": ""MyAppBot"", ""unfurl_media"": false, ""blocks"": [ { ""type"": ""section"", ""text"": { ""type"": ""mrkdwn"", ""text"": ""A message *with some bold text* and _some italicized text_."" } } ] }"; var expectedSlackJsonMessage = JObject.Parse(json); var slackMessage = SlackExtensionConfigProvider.ConvertSlackMessage(expectedSlackJsonMessage); AssertMessage(slackMessage, expectedSlackMessage); }
public void sectionblock_fields_collection_should_be_null_if_it_contains_no_elements() { var block = new SlackSectionBlock(); var blockCleaner = new SlackBlockCleaner(new[] { block }); blockCleaner.Clean(); block.Fields.Should().BeNull(); }
public void sectionblock_with_checkbox_block_element_the_selectedoptions_collection_should_be_null_if_it_contains_no_elements() { var block = new SlackSectionBlock(); var element = new SlackCheckboxBlockElement(); block.Accessory = element; var blockCleaner = new SlackBlockCleaner(new[] { block }); blockCleaner.Clean(); element.SelectedOptions.Should().BeNull(); }
public void sectionblock_with_multi_menu_external_source_block_element_the_initialoptions_collection_should_be_null_if_it_contains_no_elements() { var block = new SlackSectionBlock(); var element = new SlackMultiSelectMenuExternalSourceBlockElement(); block.Accessory = element; var blockCleaner = new SlackBlockCleaner(new[] { block }); blockCleaner.Clean(); element.InitialOptions.Should().BeNull(); }
public void sectionblock_with_multi_menu_static_block_element_the_options_collection_should_be_null_if_it_contains_no_elements() { var block = new SlackSectionBlock(); var element = new SlackMultiSelectMenuStaticBlockElement(); block.Accessory = element; var blockCleaner = new SlackBlockCleaner(new[] { block }); blockCleaner.Clean(); element.Options.Should().BeNull(); }
public SlackMessage GetSample2() { var slackMessage = new SlackMessage(); slackMessage.Text = "The assistant to the Regional Manager Dwight has an announcement!"; var sectionBlock1 = new SlackSectionBlock(); sectionBlock1.Text = new SlackMarkdownText { Text = "Take a look at this image." }; sectionBlock1.Accessory = new SlackImageBlockElement { Url = new Uri("https://api.slack.com/img/blocks/bkb_template_images/palmtree.png"), AlternateText = "palm tree" }; var sectionBlock2 = new SlackSectionBlock(); sectionBlock2.Text = new SlackMarkdownText { Text = "You can add a button alongside text in your message." }; sectionBlock2.Accessory = new SlackButtonBlockElement() { Text = new SlackPlainText { Emoji = false, Text = "Button" }, Value = "click_me_123" }; var sectionBlock3 = new SlackSectionBlock(); sectionBlock3.Text = new SlackMarkdownText { Text = "Pick one or more items from the list" }; sectionBlock3.Accessory = new SlackMultiSelectMenuStaticBlockElement { Placeholder = "Select items", Options = { new SlackOption { Text = new SlackPlainText{ Text = "Choice 1" }, Value = "value-0" }, new SlackOption { Text = new SlackPlainText{ Text = "Choice 2" }, Value = "value-1" }, new SlackOption { Text = new SlackPlainText{ Text = "Choice 3" }, Value = "value-2" } } }; var sectionBlock4 = new SlackSectionBlock(); sectionBlock4.Text = new SlackMarkdownText { Text = "This block has an overflow menu." }; sectionBlock4.Accessory = new SlackOverflowMenuBlockElement { Options = { new SlackOption { Text = new SlackPlainText{ Text = "Choice 1" }, Value = "value-0" }, new SlackOption { Text = new SlackPlainText{ Text = "Choice 2" }, Value = "value-1" }, new SlackOption { Text = new SlackPlainText{ Text = "Choice 3" }, Value = "value-2" }, new SlackOption { Text = new SlackPlainText{ Text = "Choice 4" }, Value = "value-3" } } }; var sectionBlock5 = new SlackSectionBlock(); sectionBlock5.Text = new SlackMarkdownText { Text = "Pick a date for the deadline." }; sectionBlock5.Accessory = new SlackDatePickerBlockElement { InitialDate = new DateTime(1990, 04, 28), Placeholder = new SlackPlainText { Text = "Select a date" } }; var sectionBlock6 = new SlackSectionBlock(); sectionBlock6.Fields.Add(new SlackPlainText { Text = "*this is plain_text text*" }); sectionBlock6.Fields.Add(new SlackPlainText { Text = "*this is plain_text text*" }); sectionBlock6.Fields.Add(new SlackPlainText { Text = "*this is plain_text text*" }); sectionBlock6.Fields.Add(new SlackPlainText { Text = "*this is plain_text text*" }); slackMessage.Blocks.AddRange(new[] { sectionBlock1, sectionBlock2, sectionBlock3, sectionBlock4, sectionBlock5, sectionBlock6 }); return(slackMessage); }
public SlackMessage GetSample1() { var slackMessage = new SlackMessage(); slackMessage.Text = "The assistant to the Regional Manager Dwight has an announcement!"; var sectionBlock1 = new SlackSectionBlock(); // can also use new SlackMarkdownText() {Text = "..."}; SlackMarkdownText text1 = "Hello, Assistant to the Regional Manager Dwight! *Michael Scott* wants to know where you'd like to take the Paper Company investors to dinner tonight.\n\n *Please select a restaurant:*"; sectionBlock1.Text = text1; slackMessage.Blocks.Add(sectionBlock1); var divider = new SlackDividerBlock(); divider.BlockId = null; // to enable reuse slackMessage.Blocks.Add(divider); var sectionBlock2 = new SlackSectionBlock(); SlackMarkdownText text2 = "*Farmhouse Thai Cuisine*\n:star::star::star::star: 1528 reviews\n They do have some vegan options, like the roti and curry, plus they have a ton of salad stuff and noodles can be ordered without meat!! They have something for everyone here"; sectionBlock2.Text = text2; sectionBlock2.Accessory = new SlackImageBlockElement { Url = new Uri("https://s3-media3.fl.yelpcdn.com/bphoto/c7ed05m9lC2EmA3Aruue7A/o.jpg"), AlternateText = "alt text for image" }; slackMessage.Blocks.Add(sectionBlock2); var sectionBlock3 = new SlackSectionBlock(); SlackMarkdownText text3 = "*Kin Khao*\n:star::star::star::star: 1638 reviews\n The sticky rice also goes wonderfully with the caramelized pork belly, which is absolutely melt-in-your-mouth and so soft."; sectionBlock3.Text = text3; sectionBlock3.Accessory = new SlackImageBlockElement { Url = new Uri("https://s3-media2.fl.yelpcdn.com/bphoto/korel-1YjNtFtJlMTaC26A/o.jpg"), AlternateText = "alt text for image" }; slackMessage.Blocks.Add(sectionBlock3); var sectionBlock4 = new SlackSectionBlock(); SlackMarkdownText text4 = "*Ler Ros*\n:star::star::star::star: 2082 reviews\n I would really recommend the Yum Koh Moo Yang - Spicy lime dressing and roasted quick marinated pork shoulder, basil leaves, chili & rice powder."; sectionBlock4.Text = text4; sectionBlock4.Accessory = new SlackImageBlockElement { Url = new Uri("https://s3-media2.fl.yelpcdn.com/bphoto/DawwNigKJ2ckPeDeDM7jAg/o.jpg"), AlternateText = "alt text for image" }; slackMessage.Blocks.Add(sectionBlock4); slackMessage.Blocks.Add(divider); var actionBlock = new SlackActionBlock(); var buttonElement1 = new SlackButtonBlockElement(); SlackPlainText text5 = "Farmhouse"; text5.Emoji = true; buttonElement1.Text = text5; buttonElement1.Value = "click_me_123"; actionBlock.Elements.Add(buttonElement1); var buttonElement2 = new SlackButtonBlockElement(); SlackPlainText text6 = "Kin Khao"; text5.Emoji = true; buttonElement2.Text = text6; buttonElement2.Value = "click_me_123"; actionBlock.Elements.Add(buttonElement2); var buttonElement3 = new SlackButtonBlockElement(); SlackPlainText text7 = "Ler Ros"; text5.Emoji = true; buttonElement3.Text = text7; buttonElement3.Value = "click_me_123"; actionBlock.Elements.Add(buttonElement3); slackMessage.Blocks.Add(actionBlock); return(slackMessage); }
public static async Task Run( [EventHubTrigger("monitoring", Connection = "EventHubConnectionAppSetting")] EventData[] events, [Slack(Username = "******")] IAsyncCollector <SlackMessage> slackMessages, ILogger log ) { var tenantName = "equisoft"; var exceptions = new List <Exception>(); var eventRecords = new List <KeyvaultEvent>(); var auditRecords = new List <KeyvaultAuditEvent>(); foreach (EventData eventData in events) { try { string messageBody = Encoding.UTF8.GetString(eventData.Body.Array, eventData.Body.Offset, eventData.Body.Count); log.LogInformation($"C# Event Hub trigger function processed a message: {messageBody}"); var obj = JToken.Parse(messageBody); if (obj.Type == JTokenType.Object) { if (((JObject)obj).TryGetValue("records", StringComparison.OrdinalIgnoreCase, out var auditEventRecords)) { log.LogInformation("Found {EventCount} AuditEvent(s)", auditEventRecords.Children().Count()); foreach (var auditEventRecord in auditEventRecords) { var keyvaultAuditEvent = auditEventRecord.ToObject <KeyvaultAuditEvent>(); auditRecords.Add(keyvaultAuditEvent); } } } else if (obj.Type == JTokenType.Array) { log.LogInformation("Found {EventCount} Event(s)", obj.Children().Count()); foreach (var eventRecord in obj) { var keyvaultEvent = eventRecord.ToObject <KeyvaultEvent>(); eventRecords.Add(keyvaultEvent); } } else { log.LogWarning("C# Event Hub trigger function processed a message: {Message} but the type {MessageType} is not handled", messageBody, obj.Type); // Todo add some more logic here } await Task.Yield(); } catch (Exception e) { // We need to keep processing the rest of the batch - capture this exception and continue. // Also, consider capturing details of the message that failed processing so it can be processed again later. exceptions.Add(e); } } // Once processing of the batch is complete, if any messages in the batch failed processing throw an exception so that there is a record of the failure. if (exceptions.Count > 1) { throw new AggregateException(exceptions); } if (exceptions.Count == 1) { throw exceptions.Single(); } if (auditRecords.Count > 0) { foreach (var auditRecord in auditRecords) { if (auditRecord.OperationName == "SecretGet" && auditRecord.ResultType == "Success") { var vaultName = auditRecord.Properties.Id.Host.Replace(".vault.azure.net", string.Empty); var slackMessage = new SlackMessage(); slackMessage.Text = "A new keyvault audit event occured"; var sectionBlockHeader = new SlackSectionBlock(); sectionBlockHeader.Text = new SlackMarkdownText($"You have a new keyvault audit event activity:\n*<https://portal.azure.com/#@{tenantName}.onmicrosoft.com/resource{auditRecord.ResourceId}|{vaultName}>*"); var sectionBlockBody = new SlackSectionBlock(); // you should be careful if the identity is a Service Principal. Using a user as demonstration var upnClaim = auditRecord.Identity.Claims.FirstOrDefault(x => x.Type == "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/upn"); var objectId = auditRecord.Identity.Claims.FirstOrDefault(x => x.Type == "http://schemas.microsoft.com/identity/claims/objectidentifier"); sectionBlockBody.Fields.AddRange(new[]