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

115 lines
3.9 KiB
C#
Raw Normal View History

2024-03-19 00:10:15 +00:00
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;
2024-03-19 00:10:15 +00:00
2024-04-02 19:09:42 +00:00
namespace BotSharp.Abstraction.Messaging;
2024-03-19 00:10:15 +00:00
2024-04-02 16:38:18 +00:00
public static class BotSharpMessageParser
2024-03-19 00:10:15 +00:00
{
2024-05-08 16:47:21 +00:00
public static IRichMessage? ParseRichMessage(JsonElement root, JsonSerializerOptions options)
2024-03-19 00:10:15 +00:00
{
IRichMessage? res = null;
2024-04-02 19:09:42 +00:00
Type? targetType = 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 19:09:42 +00:00
targetType = typeof(ButtonTemplateMessage);
2024-04-02 03:33:42 +00:00
}
else if (richType == RichTypeEnum.MultiSelectTemplate)
{
2024-04-02 19:09:42 +00:00
targetType = typeof(MultiSelectTemplateMessage);
2024-04-02 03:33:42 +00:00
}
else if (richType == RichTypeEnum.QuickReply)
{
2024-04-02 19:09:42 +00:00
targetType = typeof(QuickReplyMessage);
2024-04-02 03:33:42 +00:00
}
else if (richType == RichTypeEnum.CouponTemplate)
{
2024-04-02 19:09:42 +00:00
targetType = typeof(CouponTemplateMessage);
2024-04-02 03:33:42 +00:00
}
else if (richType == RichTypeEnum.Text)
{
2024-04-02 19:09:42 +00:00
targetType = typeof(TextMessage);
2024-04-02 03:33:42 +00:00
}
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 19:09:42 +00:00
targetType = wrapperType.MakeGenericType(genericType);
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
2024-04-02 19:09:42 +00:00
if (targetType != null)
{
2024-05-08 16:47:21 +00:00
res = JsonSerializer.Deserialize(jsonText, targetType, options) as IRichMessage;
2024-04-02 19:09:42 +00:00
}
2024-03-19 00:10:15 +00:00
return res;
}
2024-05-08 16:47:21 +00:00
public static ITemplateMessage? ParseTemplateMessage(JsonElement root, JsonSerializerOptions options)
2024-03-19 00:10:15 +00:00
{
ITemplateMessage? res = null;
2024-04-02 19:09:42 +00:00
Type? targetType = 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 19:09:42 +00:00
targetType = typeof(ButtonTemplateMessage);
2024-04-02 03:33:42 +00:00
}
else if (templateType == TemplateTypeEnum.MultiSelect)
{
2024-04-02 19:09:42 +00:00
targetType = typeof(MultiSelectTemplateMessage);
2024-04-02 03:33:42 +00:00
}
else if (templateType == TemplateTypeEnum.Coupon)
{
2024-04-02 19:09:42 +00:00
targetType = typeof(CouponTemplateMessage);
2024-04-02 03:33:42 +00:00
}
else if (templateType == TemplateTypeEnum.Product)
{
2024-04-02 19:09:42 +00:00
targetType = typeof(ProductTemplateMessage);
2024-04-02 03:33:42 +00:00
}
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 19:09:42 +00:00
targetType = wrapperType.MakeGenericType(genericType);
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
2024-04-02 19:09:42 +00:00
if (targetType != null)
{
2024-05-08 16:47:21 +00:00
res = JsonSerializer.Deserialize(jsonText, targetType, options) as ITemplateMessage;
2024-04-02 19:09:42 +00:00
}
2024-03-19 00:10:15 +00:00
return res;
}
}