diff --git a/src/Infrastructure/BotSharp.Abstraction/Messaging/Enums/RichTypeEnum.cs b/src/Infrastructure/BotSharp.Abstraction/Messaging/Enums/RichTypeEnum.cs new file mode 100644 index 00000000..7603f790 --- /dev/null +++ b/src/Infrastructure/BotSharp.Abstraction/Messaging/Enums/RichTypeEnum.cs @@ -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"; +} diff --git a/src/Infrastructure/BotSharp.Abstraction/Messaging/Enums/TemplateTypeEnum.cs b/src/Infrastructure/BotSharp.Abstraction/Messaging/Enums/TemplateTypeEnum.cs new file mode 100644 index 00000000..74f88735 --- /dev/null +++ b/src/Infrastructure/BotSharp.Abstraction/Messaging/Enums/TemplateTypeEnum.cs @@ -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"; +} diff --git a/src/Infrastructure/BotSharp.Abstraction/Messaging/JsonConverters/RichContentJsonConverter .cs b/src/Infrastructure/BotSharp.Abstraction/Messaging/JsonConverters/RichContentJsonConverter .cs index 33bb30fa..6545214e 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Messaging/JsonConverters/RichContentJsonConverter .cs +++ b/src/Infrastructure/BotSharp.Abstraction/Messaging/JsonConverters/RichContentJsonConverter .cs @@ -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; @@ -13,13 +15,29 @@ public class RichContentJsonConverter : JsonConverter JsonElement element; object? res = null; - if (root.TryGetProperty("buttons", out element)) + if (root.TryGetProperty("rich_type", out element)) { - res = JsonSerializer.Deserialize(jsonText, options); - } - else if (root.TryGetProperty("options", out element)) - { - res = JsonSerializer.Deserialize(jsonText, options); + var richType = element.GetString(); + if (richType == RichTypeEnum.ButtonTemplate) + { + res = JsonSerializer.Deserialize(jsonText, options); + } + else if (richType == RichTypeEnum.MultiSelectTemplate) + { + res = JsonSerializer.Deserialize(jsonText, options); + } + else if (richType == RichTypeEnum.QuickReply) + { + res = JsonSerializer.Deserialize(jsonText, options); + } + else if (richType == RichTypeEnum.CouponTemplate) + { + res = JsonSerializer.Deserialize(jsonText, options); + } + else if (richType == RichTypeEnum.Text) + { + res = JsonSerializer.Deserialize(jsonText, options); + } } return res as IRichMessage; diff --git a/src/Infrastructure/BotSharp.Abstraction/Messaging/JsonConverters/TemplateMessageJsonConverter.cs b/src/Infrastructure/BotSharp.Abstraction/Messaging/JsonConverters/TemplateMessageJsonConverter.cs index db09b7f4..22462f38 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Messaging/JsonConverters/TemplateMessageJsonConverter.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Messaging/JsonConverters/TemplateMessageJsonConverter.cs @@ -1,3 +1,4 @@ +using BotSharp.Abstraction.Messaging.Enums; using BotSharp.Abstraction.Messaging.Models.RichContent.Template; using System.Text.Json; @@ -13,13 +14,25 @@ public class TemplateMessageJsonConverter : JsonConverter JsonElement element; object? res = null; - if (root.TryGetProperty("buttons", out element)) + if (root.TryGetProperty("template_type", out element)) { - res = JsonSerializer.Deserialize(jsonText, options); - } - else if (root.TryGetProperty("options", out element)) - { - res = JsonSerializer.Deserialize(jsonText, options); + var templateType = element.GetString(); + if (templateType == TemplateTypeEnum.Button) + { + res = JsonSerializer.Deserialize(jsonText, options); + } + else if (templateType == TemplateTypeEnum.MultiSelect) + { + res = JsonSerializer.Deserialize(jsonText, options); + } + else if (templateType == TemplateTypeEnum.Coupon) + { + res = JsonSerializer.Deserialize(jsonText, options); + } + else if (templateType == TemplateTypeEnum.Product) + { + res = JsonSerializer.Deserialize(jsonText, options); + } } return res as ITemplateMessage; diff --git a/src/Infrastructure/BotSharp.Abstraction/Messaging/Models/RichContent/QuickReplyMessage.cs b/src/Infrastructure/BotSharp.Abstraction/Messaging/Models/RichContent/QuickReplyMessage.cs index 0ffd4d44..efb177f0 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Messaging/Models/RichContent/QuickReplyMessage.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Messaging/Models/RichContent/QuickReplyMessage.cs @@ -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")] diff --git a/src/Infrastructure/BotSharp.Abstraction/Messaging/Models/RichContent/Template/ButtonTemplateMessage.cs b/src/Infrastructure/BotSharp.Abstraction/Messaging/Models/RichContent/Template/ButtonTemplateMessage.cs index 8531f185..fbc2f9d6 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Messaging/Models/RichContent/Template/ButtonTemplateMessage.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Messaging/Models/RichContent/Template/ButtonTemplateMessage.cs @@ -1,3 +1,5 @@ +using BotSharp.Abstraction.Messaging.Enums; + namespace BotSharp.Abstraction.Messaging.Models.RichContent.Template; /// @@ -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]; diff --git a/src/Infrastructure/BotSharp.Abstraction/Messaging/Models/RichContent/Template/CouponTemplateMessage.cs b/src/Infrastructure/BotSharp.Abstraction/Messaging/Models/RichContent/Template/CouponTemplateMessage.cs index dd5428fb..1d0e033b 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Messaging/Models/RichContent/Template/CouponTemplateMessage.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Messaging/Models/RichContent/Template/CouponTemplateMessage.cs @@ -1,3 +1,5 @@ +using BotSharp.Abstraction.Messaging.Enums; + namespace BotSharp.Abstraction.Messaging.Models.RichContent.Template; /// @@ -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; } diff --git a/src/Infrastructure/BotSharp.Abstraction/Messaging/Models/RichContent/Template/GenericTemplateMessage.cs b/src/Infrastructure/BotSharp.Abstraction/Messaging/Models/RichContent/Template/GenericTemplateMessage.cs index c3e9a32a..7c0ef0b5 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Messaging/Models/RichContent/Template/GenericTemplateMessage.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Messaging/Models/RichContent/Template/GenericTemplateMessage.cs @@ -1,15 +1,17 @@ +using BotSharp.Abstraction.Messaging.Enums; + namespace BotSharp.Abstraction.Messaging.Models.RichContent.Template; public class GenericTemplateMessage : 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 Elements { get; set; } = new List(); diff --git a/src/Infrastructure/BotSharp.Abstraction/Messaging/Models/RichContent/Template/MultiSelectTemplateMessage.cs b/src/Infrastructure/BotSharp.Abstraction/Messaging/Models/RichContent/Template/MultiSelectTemplateMessage.cs index 47ce576a..b838ba03 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Messaging/Models/RichContent/Template/MultiSelectTemplateMessage.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Messaging/Models/RichContent/Template/MultiSelectTemplateMessage.cs @@ -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 Options { get; set; } = new List(); } diff --git a/src/Infrastructure/BotSharp.Abstraction/Messaging/Models/RichContent/Template/ProductTemplateMessage.cs b/src/Infrastructure/BotSharp.Abstraction/Messaging/Models/RichContent/Template/ProductTemplateMessage.cs index 879da7c6..e796afae 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Messaging/Models/RichContent/Template/ProductTemplateMessage.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Messaging/Models/RichContent/Template/ProductTemplateMessage.cs @@ -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 diff --git a/src/Infrastructure/BotSharp.Abstraction/Messaging/Models/RichContent/TextMessage.cs b/src/Infrastructure/BotSharp.Abstraction/Messaging/Models/RichContent/TextMessage.cs index cf14ca9b..899a9265 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Messaging/Models/RichContent/TextMessage.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Messaging/Models/RichContent/TextMessage.cs @@ -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; diff --git a/src/Infrastructure/BotSharp.Core/Messaging/RichContentService.cs b/src/Infrastructure/BotSharp.Core/Messaging/RichContentService.cs index 6bfb60ad..62b016b5 100644 --- a/src/Infrastructure/BotSharp.Core/Messaging/RichContentService.cs +++ b/src/Infrastructure/BotSharp.Core/Messaging/RichContentService.cs @@ -1,4 +1,5 @@ using BotSharp.Abstraction.Messaging; +using BotSharp.Abstraction.Messaging.Enums; using BotSharp.Abstraction.Messaging.Models.RichContent; namespace BotSharp.Core.Messaging; @@ -16,17 +17,17 @@ public class RichContentService : IRichContentService 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(m.RootElement.ToString(), options)); } - else if (richType == "quick_reply") + else if (richType == RichTypeEnum.QuickReply) { messages.Add(JsonSerializer.Deserialize(m.RootElement.ToString(), options)); } diff --git a/src/Plugins/BotSharp.Plugin.MetaMessenger/MessagingModels/AttachmentMessage.cs b/src/Plugins/BotSharp.Plugin.MetaMessenger/MessagingModels/AttachmentMessage.cs index e70e172a..36d44292 100644 --- a/src/Plugins/BotSharp.Plugin.MetaMessenger/MessagingModels/AttachmentMessage.cs +++ b/src/Plugins/BotSharp.Plugin.MetaMessenger/MessagingModels/AttachmentMessage.cs @@ -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; }