refine rich type and template type
This commit is contained in:
parent
f86f437f31
commit
9877f1c623
|
|
@ -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";
|
||||
}
|
||||
|
|
@ -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";
|
||||
}
|
||||
|
|
@ -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<IRichMessage>
|
|||
JsonElement element;
|
||||
object? res = null;
|
||||
|
||||
if (root.TryGetProperty("buttons", out element))
|
||||
if (root.TryGetProperty("rich_type", out element))
|
||||
{
|
||||
res = JsonSerializer.Deserialize<ButtonTemplateMessage>(jsonText, options);
|
||||
}
|
||||
else if (root.TryGetProperty("options", out element))
|
||||
{
|
||||
res = JsonSerializer.Deserialize<MultiSelectTemplateMessage>(jsonText, options);
|
||||
var richType = element.GetString();
|
||||
if (richType == RichTypeEnum.ButtonTemplate)
|
||||
{
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
return res as IRichMessage;
|
||||
|
|
|
|||
|
|
@ -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<ITemplateMessage>
|
|||
JsonElement element;
|
||||
object? res = null;
|
||||
|
||||
if (root.TryGetProperty("buttons", out element))
|
||||
if (root.TryGetProperty("template_type", out element))
|
||||
{
|
||||
res = JsonSerializer.Deserialize<ButtonTemplateMessage>(jsonText, options);
|
||||
}
|
||||
else if (root.TryGetProperty("options", out element))
|
||||
{
|
||||
res = JsonSerializer.Deserialize<MultiSelectTemplateMessage>(jsonText, options);
|
||||
var templateType = element.GetString();
|
||||
if (templateType == TemplateTypeEnum.Button)
|
||||
{
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
return res as ITemplateMessage;
|
||||
|
|
|
|||
|
|
@ -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")]
|
||||
|
|
|
|||
|
|
@ -1,3 +1,5 @@
|
|||
using BotSharp.Abstraction.Messaging.Enums;
|
||||
|
||||
namespace BotSharp.Abstraction.Messaging.Models.RichContent.Template;
|
||||
|
||||
/// <summary>
|
||||
|
|
@ -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];
|
||||
|
|
|
|||
|
|
@ -1,3 +1,5 @@
|
|||
using BotSharp.Abstraction.Messaging.Enums;
|
||||
|
||||
namespace BotSharp.Abstraction.Messaging.Models.RichContent.Template;
|
||||
|
||||
/// <summary>
|
||||
|
|
@ -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; }
|
||||
|
|
|
|||
|
|
@ -1,15 +1,17 @@
|
|||
using BotSharp.Abstraction.Messaging.Enums;
|
||||
|
||||
namespace BotSharp.Abstraction.Messaging.Models.RichContent.Template;
|
||||
|
||||
public class GenericTemplateMessage<T> : 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<T> Elements { get; set; } = new List<T>();
|
||||
|
|
|
|||
|
|
@ -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<OptionElement> Options { get; set; } = new List<OptionElement>();
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
|
||||
|
|
|
|||
|
|
@ -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<TextMessage>(m.RootElement.ToString(), options));
|
||||
}
|
||||
else if (richType == "quick_reply")
|
||||
else if (richType == RichTypeEnum.QuickReply)
|
||||
{
|
||||
messages.Add(JsonSerializer.Deserialize<QuickReplyMessage>(m.RootElement.ToString(), options));
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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; }
|
||||
|
|
|
|||
Loading…
Reference in a new issue