Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refine rich type and template type #317

Merged
merged 1 commit into from
Feb 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
namespace BotSharp.Abstraction.Messaging.Enums;

public static class RichTypeEnum
{
public const string ButtonTemplate = "button_template";
public const string MultiSelectTemplate = "multi-select_template";
public const string CouponTemplate = "coupon_template";
public const string GenericTemplate = "generic_template";
public const string QuickReply = "quick_reply";
public const string Text = "text";
public const string Attachment = "attachment";
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace BotSharp.Abstraction.Messaging.Enums;

public static class TemplateTypeEnum
{
public const string Button = "button";
public const string Coupon = "coupon";
public const string Generic = "generic";
public const string MultiSelect = "multi-select";
public const string Product = "product";
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using BotSharp.Abstraction.Messaging.Enums;
using BotSharp.Abstraction.Messaging.Models.RichContent;
using BotSharp.Abstraction.Messaging.Models.RichContent.Template;
using System.Text.Json;

Expand All @@ -13,13 +15,29 @@ public class RichContentJsonConverter : JsonConverter<IRichMessage>
JsonElement element;
object? res = null;

if (root.TryGetProperty("buttons", out element))
if (root.TryGetProperty("rich_type", out element))
{
res = JsonSerializer.Deserialize<ButtonTemplateMessage>(jsonText, options);
}
else if (root.TryGetProperty("options", out element))
{
res = JsonSerializer.Deserialize<MultiSelectTemplateMessage>(jsonText, options);
var richType = element.GetString();
if (richType == RichTypeEnum.ButtonTemplate)
{
res = JsonSerializer.Deserialize<ButtonTemplateMessage>(jsonText, options);
}
else if (richType == RichTypeEnum.MultiSelectTemplate)
{
res = JsonSerializer.Deserialize<MultiSelectTemplateMessage>(jsonText, options);
}
else if (richType == RichTypeEnum.QuickReply)
{
res = JsonSerializer.Deserialize<QuickReplyMessage>(jsonText, options);
}
else if (richType == RichTypeEnum.CouponTemplate)
{
res = JsonSerializer.Deserialize<CouponTemplateMessage>(jsonText, options);
}
else if (richType == RichTypeEnum.Text)
{
res = JsonSerializer.Deserialize<TextMessage>(jsonText, options);
}
}

return res as IRichMessage;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using BotSharp.Abstraction.Messaging.Enums;
using BotSharp.Abstraction.Messaging.Models.RichContent.Template;
using System.Text.Json;

Expand All @@ -13,13 +14,25 @@ public class TemplateMessageJsonConverter : JsonConverter<ITemplateMessage>
JsonElement element;
object? res = null;

if (root.TryGetProperty("buttons", out element))
if (root.TryGetProperty("template_type", out element))
{
res = JsonSerializer.Deserialize<ButtonTemplateMessage>(jsonText, options);
}
else if (root.TryGetProperty("options", out element))
{
res = JsonSerializer.Deserialize<MultiSelectTemplateMessage>(jsonText, options);
var templateType = element.GetString();
if (templateType == TemplateTypeEnum.Button)
{
res = JsonSerializer.Deserialize<ButtonTemplateMessage>(jsonText, options);
}
else if (templateType == TemplateTypeEnum.MultiSelect)
{
res = JsonSerializer.Deserialize<MultiSelectTemplateMessage>(jsonText, options);
}
else if (templateType == TemplateTypeEnum.Coupon)
{
res = JsonSerializer.Deserialize<CouponTemplateMessage>(jsonText, options);
}
else if (templateType == TemplateTypeEnum.Product)
{
res = JsonSerializer.Deserialize<ProductTemplateMessage>(jsonText, options);
}
}

return res as ITemplateMessage;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
using BotSharp.Abstraction.Messaging.Enums;

namespace BotSharp.Abstraction.Messaging.Models.RichContent;

public class QuickReplyMessage : IRichMessage
{
[JsonPropertyName("rich_type")]
public string RichType => "quick_reply";
public string RichType => RichTypeEnum.QuickReply;
public string Text { get; set; } = string.Empty;

[JsonPropertyName("quick_replies")]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using BotSharp.Abstraction.Messaging.Enums;

namespace BotSharp.Abstraction.Messaging.Models.RichContent.Template;

/// <summary>
Expand All @@ -6,13 +8,13 @@ namespace BotSharp.Abstraction.Messaging.Models.RichContent.Template;
public class ButtonTemplateMessage : IRichMessage, ITemplateMessage
{
[JsonPropertyName("rich_type")]
public string RichType => "button_template";
public string RichType => RichTypeEnum.ButtonTemplate;

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

[JsonPropertyName("template_type")]
public string TemplateType => "button";
public string TemplateType => TemplateTypeEnum.Button;

[JsonPropertyName("buttons")]
public ButtonElement[] Buttons { get; set; } = new ButtonElement[0];
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using BotSharp.Abstraction.Messaging.Enums;

namespace BotSharp.Abstraction.Messaging.Models.RichContent.Template;

/// <summary>
Expand All @@ -7,14 +9,14 @@ namespace BotSharp.Abstraction.Messaging.Models.RichContent.Template;
public class CouponTemplateMessage : IRichMessage, ITemplateMessage
{
[JsonPropertyName("rich_type")]
public string RichType => "coupon_template";
public string RichType => RichTypeEnum.CouponTemplate;
[JsonIgnore]
public string Text { get; set; }
public string Title { get; set; }
public string Subtitle { get; set; }

[JsonPropertyName("template_type")]
public string TemplateType => "coupon";
public string TemplateType => TemplateTypeEnum.Coupon;

[JsonPropertyName("coupon_code")]
public string CouponCode { get; set; }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
using BotSharp.Abstraction.Messaging.Enums;

namespace BotSharp.Abstraction.Messaging.Models.RichContent.Template;

public class GenericTemplateMessage<T> : IRichMessage, ITemplateMessage
{
[JsonPropertyName("rich_type")]
public string RichType => "generic_template";
public string RichType => RichTypeEnum.GenericTemplate;

[JsonIgnore]
public string Text { get; set; } = string.Empty;

[JsonPropertyName("template_type")]
public string TemplateType => "generic";
public string TemplateType => TemplateTypeEnum.Generic;

[JsonPropertyName("elements")]
public List<T> Elements { get; set; } = new List<T>();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
using BotSharp.Abstraction.Messaging.Enums;

namespace BotSharp.Abstraction.Messaging.Models.RichContent.Template;

public class MultiSelectTemplateMessage : IRichMessage, ITemplateMessage
{
[JsonPropertyName("rich_type")]
public string RichType => "multi-select_template";
public string RichType => RichTypeEnum.MultiSelectTemplate;

public string Text { get; set; } = string.Empty;

[JsonPropertyName("template_type")]
public string TemplateType => "multi-select";
public string TemplateType => TemplateTypeEnum.MultiSelect;
public List<OptionElement> Options { get; set; } = new List<OptionElement>();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
using BotSharp.Abstraction.Messaging.Enums;

namespace BotSharp.Abstraction.Messaging.Models.RichContent.Template;

public class ProductTemplateMessage : IRichMessage, ITemplateMessage
{
[JsonPropertyName("rich_type")]
public string RichType => "generic_template";
public string RichType => RichTypeEnum.GenericTemplate;

[JsonIgnore]
public string Text { get; set; } = string.Empty;

[JsonPropertyName("template_type")]
public string TemplateType => "product";
public string TemplateType => TemplateTypeEnum.Product;
}

public class ProductElement
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
using BotSharp.Abstraction.Messaging.Enums;

namespace BotSharp.Abstraction.Messaging.Models.RichContent;

public class TextMessage : IRichMessage
{
[JsonPropertyName("rich_type")]
public string RichType => "text";
public string RichType => RichTypeEnum.Text;

public string Text { get; set; } = string.Empty;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using BotSharp.Abstraction.Messaging;
using BotSharp.Abstraction.Messaging.Enums;
using BotSharp.Abstraction.Messaging.Models.RichContent;

namespace BotSharp.Core.Messaging;
Expand All @@ -16,17 +17,17 @@ public List<IRichMessage> ConvertToMessages(string content)

foreach (var m in tempMessages)
{
var richType = "text";
var richType = RichTypeEnum.Text;
if (m.RootElement.TryGetProperty("rich_type", out var element))
{
richType = element.GetString();
}

if (richType == "text")
if (richType == RichTypeEnum.Text)
{
messages.Add(JsonSerializer.Deserialize<TextMessage>(m.RootElement.ToString(), options));
}
else if (richType == "quick_reply")
else if (richType == RichTypeEnum.QuickReply)
{
messages.Add(JsonSerializer.Deserialize<QuickReplyMessage>(m.RootElement.ToString(), options));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace BotSharp.Plugin.MetaMessenger.MessagingModels;
public class AttachmentMessage : IRichMessage
{
[JsonPropertyName("rich_type")]
public string RichType => "attachment";
public string RichType => RichTypeEnum.Attachment;

[JsonIgnore]
public string Text { get; set; }
Expand Down