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

106 lines
4 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;
namespace BotSharp.Core.Messaging;
2024-04-02 03:33:42 +00:00
public static class MessageParser
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();
if (elementType == typeof(GenericElement).Name)
{
res = JsonSerializer.Deserialize<GenericTemplateMessage<GenericElement>>(jsonText, options);
}
else if (elementType == typeof(ButtonElement).Name)
{
res = JsonSerializer.Deserialize<GenericTemplateMessage<ButtonElement>>(jsonText, options);
}
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();
if (elementType == typeof(GenericElement).Name)
{
res = JsonSerializer.Deserialize<GenericTemplateMessage<GenericElement>>(jsonText, options);
}
else if (elementType == typeof(ButtonElement).Name)
{
res = JsonSerializer.Deserialize<GenericTemplateMessage<ButtonElement>>(jsonText, options);
}
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;
}
}