diff --git a/src/Infrastructure/BotSharp.Abstraction/Messaging/JsonConverters/RichContentJsonConverter .cs b/src/Infrastructure/BotSharp.Abstraction/Messaging/JsonConverters/RichContentJsonConverter .cs index a490415dd..95c00037d 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Messaging/JsonConverters/RichContentJsonConverter .cs +++ b/src/Infrastructure/BotSharp.Abstraction/Messaging/JsonConverters/RichContentJsonConverter .cs @@ -1,6 +1,4 @@ -using BotSharp.Abstraction.Messaging.Enums; -using BotSharp.Abstraction.Messaging.Models.RichContent; -using BotSharp.Abstraction.Messaging.Models.RichContent.Template; +using BotSharp.Core.Messaging; using System.Text.Json; namespace BotSharp.Abstraction.Messaging.JsonConverters; @@ -12,35 +10,16 @@ public class RichContentJsonConverter : JsonConverter using var jsonDoc = JsonDocument.ParseValue(ref reader); var root = jsonDoc.RootElement; var jsonText = root.GetRawText(); - JsonElement element; - object? res = null; + IRichMessage? res = null; - if (root.TryGetProperty("rich_type", out element)) + var parser = new MessageParser(); + if (root.TryGetProperty("rich_type", out JsonElement element)) { 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); - } + res = parser.ParseRichMessage(richType, jsonText, options); } - return res as IRichMessage; + return res; } public override void Write(Utf8JsonWriter writer, IRichMessage value, JsonSerializerOptions options) diff --git a/src/Infrastructure/BotSharp.Abstraction/Messaging/JsonConverters/TemplateMessageJsonConverter.cs b/src/Infrastructure/BotSharp.Abstraction/Messaging/JsonConverters/TemplateMessageJsonConverter.cs index 22462f382..fbd07e3f9 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Messaging/JsonConverters/TemplateMessageJsonConverter.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Messaging/JsonConverters/TemplateMessageJsonConverter.cs @@ -1,5 +1,4 @@ -using BotSharp.Abstraction.Messaging.Enums; -using BotSharp.Abstraction.Messaging.Models.RichContent.Template; +using BotSharp.Core.Messaging; using System.Text.Json; namespace BotSharp.Abstraction.Messaging.JsonConverters; @@ -11,31 +10,16 @@ public class TemplateMessageJsonConverter : JsonConverter using var jsonDoc = JsonDocument.ParseValue(ref reader); var root = jsonDoc.RootElement; var jsonText = root.GetRawText(); - JsonElement element; - object? res = null; + ITemplateMessage? res = null; - if (root.TryGetProperty("template_type", out element)) + var parser = new MessageParser(); + if (root.TryGetProperty("template_type", out JsonElement element)) { 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); - } + res = parser.ParseTemplateMessage(templateType, jsonText, options); } - return res as ITemplateMessage; + return res; } public override void Write(Utf8JsonWriter writer, ITemplateMessage value, JsonSerializerOptions options) diff --git a/src/Infrastructure/BotSharp.Abstraction/Messaging/MessageParser.cs b/src/Infrastructure/BotSharp.Abstraction/Messaging/MessageParser.cs new file mode 100644 index 000000000..abc2f621b --- /dev/null +++ b/src/Infrastructure/BotSharp.Abstraction/Messaging/MessageParser.cs @@ -0,0 +1,66 @@ +using BotSharp.Abstraction.Messaging; +using BotSharp.Abstraction.Messaging.Enums; +using BotSharp.Abstraction.Messaging.Models.RichContent.Template; +using BotSharp.Abstraction.Messaging.Models.RichContent; +using System.Text.Json; + +namespace BotSharp.Core.Messaging; + +public class MessageParser +{ + public MessageParser() + { + } + + public IRichMessage? ParseRichMessage(string richType, string jsonText, JsonSerializerOptions options) + { + IRichMessage? res = null; + + 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; + } + + public ITemplateMessage? ParseTemplateMessage(string templateType, string jsonText, JsonSerializerOptions options) + { + ITemplateMessage? res = null; + + 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; + } +}