public static SlackMessageBuilder CreateMessageBuilder(string channel, LogEvent logEvent, string message, string replayTimespan = null) => SlackMessageBuilder.CreateBuilder(channel) .Text(message) .LineBreak() .Reply(replayTimespan) .Blocks( new SectionBlock { Text = (MarkdownTextObject)SlackMessageTextHelper.Date(logEvent.Timestamp.ToUnixTimeSeconds(), "{date_num} {time_secs}", "LogDate"), });
SendMessageResponse MessageWithBlocksResponse)> SendMessagesWithBuilderAsync(SlackClient slackClient, string channelName, string userId) { const string SampleUrl = "https://api.slack.com/"; var now = DateTime.Now; var nowTimestamp = UnixTimeHelper.ToUnixTime(now); var channelId = await ConversationsClientMethods.GetChannelIdAsync(slackClient, channelName); var messageWithTextHelper = new SlackMessageBuilder(channelName) .Text($"Channel mention by {nameof(SlackMessageTextHelper)}: {SlackMessageTextHelper.ChannelMention(SlackMention.Channel)}") .LineBreak() .Text($"Users mention by {nameof(SlackMessageTextHelper)}: {SlackMessageTextHelper.UserMentions(new[] { userId, userId })}") .LineBreak() .Text($"Public channel mention by {nameof(SlackMessageTextHelper)}: {SlackMessageTextHelper.PublicChannelMention(channelId)}") .LineBreak() .Text($"{SlackMessageTextHelper.Bold("bold text")} by {nameof(SlackMessageTextHelper)}") .LineBreak() .Text($"{SlackMessageTextHelper.InlineCode("inline code")} by {nameof(SlackMessageTextHelper)}") .LineBreak() .Text($"{SlackMessageTextHelper.Strike("strike text")} by {nameof(SlackMessageTextHelper)}") .LineBreak() .Text($"{SlackMessageTextHelper.Italic("italic text")} by {nameof(SlackMessageTextHelper)}") .LineBreak() .Text($"Link by {nameof(SlackMessageTextHelper)}: {SlackMessageTextHelper.Link(SampleUrl)}") .LineBreak() .Text($"Link by {nameof(SlackMessageTextHelper)}: {SlackMessageTextHelper.Link(new Uri(SampleUrl))}") .LineBreak() .Text($"Link by {nameof(SlackMessageTextHelper)}: {SlackMessageTextHelper.EmailLink("*****@*****.**")}") .LineBreak() .LineBreak() .Text($"Code block by {nameof(SlackMessageTextHelper)}:{SlackMessageTextHelper.CodeBlock("code line #1\ncode line #2")}") .LineBreak() .LineBreak() .Text($"Unquoted text by {nameof(SlackMessageTextHelper)}:{SlackMessageTextHelper.Quoted("quoted text #1\nquoted text #2")}Unquoted text again") .LineBreak() .LineBreak() .Text($"List by {nameof(SlackMessageTextHelper)}:{SlackMessageTextHelper.List("list item #1", "list item #2")}") .LineBreak() .LineBreak() .Text($"User mention by {nameof(SlackMessageTextHelper)}: {SlackMessageTextHelper.UserMention(userId)}") .LineBreak() .Text(SlackMessageTextHelper.Date(nowTimestamp, $"Date by {nameof(SlackMessageTextHelper)}: {{date_num}} {{time}}", now.ToString("yyyy-MM-dd hh:mm"))); var messageWithTextBuilder = new SlackMessageBuilder(channelId) .ChannelMention(SlackMention.Here) .UserMentions(new[] { userId, userId }, "\n") .PublicChannelMention(channelId, "\n") .BoldText("bold text", "\n") .InlineCodeText("inline code", "\n") .StrikeText("strike text", SlackMessageTextHelper.LineBreak) .ItalicText("italic text", SlackMessageTextHelper.LineBreak) .CodeBlock("code block", SlackMessageTextHelper.LineBreak) .LinkText(SampleUrl, "Slack api url", SlackMessageTextHelper.LineBreak) .LinkText(new Uri(SampleUrl), "Slack api url", SlackMessageTextHelper.LineBreak) .EmailLinkText("*****@*****.**", "Test email", SlackMessageTextHelper.LineBreak) .QuotedText("quoted text #1") .QuotedText("quoted text #2") .ListText(new[] { "list item #1", "list item #2" }) .UserMention(userId) .Date(nowTimestamp, "{date_num} {time}", now.ToString("yyyy-MM-dd hh:mm"), new Uri(SampleUrl), SlackMessageTextHelper.LineBreak); var userGroupListResponse = await slackClient.UserGroups.ListAsync(); var userGroup = userGroupListResponse.UserGroups.FirstOrDefault(); if (userGroup != null) { var userGroupId = userGroup.Id; messageWithTextHelper = messageWithTextHelper .LineBreak() .Text($"User group mention by {nameof(SlackMessageTextHelper)}: {SlackMessageTextHelper.UserGroupMention(userGroupId)}"); messageWithTextBuilder = messageWithTextBuilder.UserGroupMention(userGroupId, SlackMessageTextHelper.LineBreak); } var messageWithTextHelperResponse = await slackClient.Chat.PostMessageAsync(messageWithTextHelper); var messageWithTextBuilderResponse = await slackClient.Chat.PostMessageAsync(messageWithTextBuilder); var messageWithBlocks = SlackMessageBuilder.CreateBuilder(channelName) .Reply(messageWithTextBuilderResponse.Timestamp, true) .Blocks(CreateHeaderBlock()) .Blocks(CreateActionBlock(), CreateContextBlock()) .Attachments(new Attachment("#000099", "Attachment1")) .Attachments(new Attachment("#009900", "Attachment2"), new Attachment("#990000", "Attachment3")) .CreateMessage(); var messageWithBlocksResponse = await slackClient.Chat.PostMessageAsync(messageWithBlocks); return(messageWithTextHelperResponse, messageWithTextBuilderResponse, messageWithBlocksResponse); }