Skip to content

Commit

Permalink
Added Attachment, Fields and Colors
Browse files Browse the repository at this point in the history
  • Loading branch information
henriqueholtz committed Aug 17, 2021
1 parent bb6ba66 commit 4d32617
Show file tree
Hide file tree
Showing 4 changed files with 138 additions and 0 deletions.
110 changes: 110 additions & 0 deletions SendSlackMessage/Entities/Attachment.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
using SendSlackMessage.Enums;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.Json.Serialization;
using System.Threading.Tasks;

namespace SendSlackMessage.Entities
{
public class Attachment
{
[JsonPropertyName("fallback")]
public string Fallback { get; set; }

[JsonPropertyName("text")]
public string Text { get; set; }

[JsonPropertyName("pretext")]
public string Pretext { get; set; }

[JsonPropertyName("fields")]
public List<Field> Fields { get; set; } = new List<Field>();
/// <summary>
/// Can either be one of 'good', 'warning', 'danger', or any hex color code
/// </summary>
[JsonPropertyName("color")]
public string Color { get; set; }

[JsonPropertyName("author_name")]
public string AuthorName { get; set; }

[JsonPropertyName("author_link")]
public string AuthorLink { get; set; }

[JsonPropertyName("author_icon")]
public string AuthorIcon { get; set; }

[JsonPropertyName("title_link")]
public string TitleLink { get; set; }

[JsonPropertyName("image_url")]
public string ImageUrl { get; set; }

[JsonPropertyName("thumb_url")]
public string ThumbUrl { get; set; }

[JsonPropertyName("footer")]
public string Footer { get; set; }
[JsonPropertyName("footer_icon")]
public string FooterIcon { get; set; }

[JsonPropertyName("ts")]
public string FooterTimeStamp { get; set; }

public Attachment() { }
public Attachment SetFooter(string footerText, string footerIcon/*, DateTime footerTimeStamp*/)
{
Footer = footerText;
FooterIcon = footerIcon;
//lack set FooterTimeStamp
return this;
}

public Attachment AddField(string title, string value, bool _short = false)
{
Fields.Add(new Field
{
Title = title,
Value = value,
Short = _short
});

return this;
}

public Attachment SetAuthor(string authorName, string authorLink = null, string authorIcon = null)
{
AuthorName = authorName;
AuthorLink = authorLink;
AuthorIcon = authorIcon;
return this;
}

public Attachment SetColor(EnumColor color)
{
switch(color)
{
case EnumColor.Green:
Color = "good";
break;
case EnumColor.Orange:
Color = "warning";
break;
case EnumColor.Red:
Color = "danger";
break;
default:
Color = null;
break;
}
return this;
}
public Attachment SetColor(string hexCodeColor)
{
Color = hexCodeColor;
return this;
}
}
}
15 changes: 15 additions & 0 deletions SendSlackMessage/Entities/Field.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System.Text.Json.Serialization;

namespace SendSlackMessage.Entities
{
public class Field
{

[JsonPropertyName("title")]
public string Title { get; set; }
[JsonPropertyName("value")]
public string Value { get; set; }
[JsonPropertyName("short")]
public bool Short { get; set; }
}
}
4 changes: 4 additions & 0 deletions SendSlackMessage/Entities/Message.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ public class Message
[JsonPropertyName("response_type")]
private string ResponseType { get => string.IsNullOrWhiteSpace(_responseType) ? "ephemeral" : _responseType; set => _responseType = value; }


[JsonPropertyName("attachments")]
public List<Attachment> Attachments { get; set; }

public Message(string channel, string username, string iconEmoji, string iconUrl, string text, bool markdown = true)
{
Channel = channel;
Expand Down
9 changes: 9 additions & 0 deletions SendSlackMessage/Enums/EnumColor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace SendSlackMessage.Enums
{
public enum EnumColor
{
Red,
Orange,
Green
}
}

0 comments on commit 4d32617

Please sign in to comment.