BotSharp/src/Infrastructure/BotSharp.Abstraction/Messaging/BotSharpMessageParser.cs

109 lines
4.2 KiB
C#
Raw Normal View History

2024-03-19 00:10:15 +00:00
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;
2024-04-02 16:38:18 +00:00
using System.Reflection;
using Newtonsoft.Json;
using JsonSerializer = System.Text.Json.JsonSerializer;
2024-03-19 00:10:15 +00:00
namespace BotSharp.Core.Messaging;
2024-04-02 16:38:18 +00:00
public static class BotSharpMessageParser
2024-03-19 00:10:15 +00:00
{
2024-04-02 03:33:42 +00:00
public static IRichMessage? ParseRichMessage(JsonElement root, JsonSerializerOptions options)
2024-03-19 00:10:15 +00:00
{
IRichMessage? res = null;
2024-04-02 03:33:42 +00:00
JsonElement element;
var jsonText = root.GetRawText();
2024-03-19 00:10:15 +00:00
2024-04-02 03:33:42 +00:00
if (root.TryGetProperty("rich_type", out element))
2024-03-19 00:10:15 +00:00
{
2024-04-02 03:33:42 +00:00
var richType = element.GetString();
if (richType == RichTypeEnum.ButtonTemplate)
2024-03-25 19:12:08 +00:00
{
2024-04-02 03:33:42 +00:00
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);
}
else if (richType == RichTypeEnum.GenericTemplate)
{
if (root.TryGetProperty("element_type", out element))
2024-03-28 15:32:09 +00:00
{
2024-04-02 03:33:42 +00:00
var elementType = element.GetString();
2024-04-02 16:38:18 +00:00
var wrapperType = typeof(GenericTemplateMessage<>);
var genericType = Assembly.GetExecutingAssembly().GetTypes().FirstOrDefault(x => x.Name == elementType);
if (wrapperType != null && genericType != null)
2024-04-02 03:33:42 +00:00
{
2024-04-02 16:38:18 +00:00
var targetType = wrapperType.MakeGenericType(genericType);
res = JsonConvert.DeserializeObject(jsonText, targetType) as IRichMessage;
2024-04-02 03:33:42 +00:00
}
2024-03-28 15:32:09 +00:00
}
2024-03-25 19:12:08 +00:00
}
}
2024-03-19 00:10:15 +00:00
return res;
}
2024-04-02 03:33:42 +00:00
public static ITemplateMessage? ParseTemplateMessage(JsonElement root, JsonSerializerOptions options)
2024-03-19 00:10:15 +00:00
{
ITemplateMessage? res = null;
2024-04-02 03:33:42 +00:00
JsonElement element;
var jsonText = root.GetRawText();
2024-03-19 00:10:15 +00:00
2024-04-02 03:33:42 +00:00
if (root.TryGetProperty("template_type", out element))
2024-03-25 19:12:08 +00:00
{
2024-04-02 03:33:42 +00:00
var templateType = element.GetString();
if (templateType == TemplateTypeEnum.Button)
2024-03-25 19:12:08 +00:00
{
2024-04-02 03:33:42 +00:00
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);
}
else if (templateType == TemplateTypeEnum.Generic)
{
if (root.TryGetProperty("element_type", out element))
2024-03-28 15:32:09 +00:00
{
2024-04-02 03:33:42 +00:00
var elementType = element.GetString();
2024-04-02 16:38:18 +00:00
var wrapperType = typeof(GenericTemplateMessage<>);
var genericType = Assembly.GetExecutingAssembly().GetTypes().FirstOrDefault(x => x.Name == elementType);
if (wrapperType != null && genericType != null)
2024-04-02 03:33:42 +00:00
{
2024-04-02 16:38:18 +00:00
var targetType = wrapperType.MakeGenericType(genericType);
res = JsonConvert.DeserializeObject(jsonText, targetType) as ITemplateMessage;
2024-04-02 03:33:42 +00:00
}
2024-03-28 15:32:09 +00:00
}
2024-03-25 19:12:08 +00:00
}
}
2024-03-19 00:10:15 +00:00
return res;
}
}