Merge pull request #317 from iceljc/features/refine-rich-type
refine rich type and template type
This commit is contained in:
commit
32d5fc05bc
|
|
@ -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 BotSharp.Abstraction.Messaging.Models.RichContent.Template;
|
||||||
using System.Text.Json;
|
using System.Text.Json;
|
||||||
|
|
||||||
|
|
@ -13,13 +15,29 @@ public class RichContentJsonConverter : JsonConverter<IRichMessage>
|
||||||
JsonElement element;
|
JsonElement element;
|
||||||
object? res = null;
|
object? res = null;
|
||||||
|
|
||||||
if (root.TryGetProperty("buttons", out element))
|
if (root.TryGetProperty("rich_type", out element))
|
||||||
{
|
{
|
||||||
res = JsonSerializer.Deserialize<ButtonTemplateMessage>(jsonText, options);
|
var richType = element.GetString();
|
||||||
}
|
if (richType == RichTypeEnum.ButtonTemplate)
|
||||||
else if (root.TryGetProperty("options", out element))
|
{
|
||||||
{
|
res = JsonSerializer.Deserialize<ButtonTemplateMessage>(jsonText, options);
|
||||||
res = JsonSerializer.Deserialize<MultiSelectTemplateMessage>(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;
|
return res as IRichMessage;
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,4 @@
|
||||||
|
using BotSharp.Abstraction.Messaging.Enums;
|
||||||
using BotSharp.Abstraction.Messaging.Models.RichContent.Template;
|
using BotSharp.Abstraction.Messaging.Models.RichContent.Template;
|
||||||
using System.Text.Json;
|
using System.Text.Json;
|
||||||
|
|
||||||
|
|
@ -13,13 +14,25 @@ public class TemplateMessageJsonConverter : JsonConverter<ITemplateMessage>
|
||||||
JsonElement element;
|
JsonElement element;
|
||||||
object? res = null;
|
object? res = null;
|
||||||
|
|
||||||
if (root.TryGetProperty("buttons", out element))
|
if (root.TryGetProperty("template_type", out element))
|
||||||
{
|
{
|
||||||
res = JsonSerializer.Deserialize<ButtonTemplateMessage>(jsonText, options);
|
var templateType = element.GetString();
|
||||||
}
|
if (templateType == TemplateTypeEnum.Button)
|
||||||
else if (root.TryGetProperty("options", out element))
|
{
|
||||||
{
|
res = JsonSerializer.Deserialize<ButtonTemplateMessage>(jsonText, options);
|
||||||
res = JsonSerializer.Deserialize<MultiSelectTemplateMessage>(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;
|
return res as ITemplateMessage;
|
||||||
|
|
|
||||||
|
|
@ -1,9 +1,11 @@
|
||||||
|
using BotSharp.Abstraction.Messaging.Enums;
|
||||||
|
|
||||||
namespace BotSharp.Abstraction.Messaging.Models.RichContent;
|
namespace BotSharp.Abstraction.Messaging.Models.RichContent;
|
||||||
|
|
||||||
public class QuickReplyMessage : IRichMessage
|
public class QuickReplyMessage : IRichMessage
|
||||||
{
|
{
|
||||||
[JsonPropertyName("rich_type")]
|
[JsonPropertyName("rich_type")]
|
||||||
public string RichType => "quick_reply";
|
public string RichType => RichTypeEnum.QuickReply;
|
||||||
public string Text { get; set; } = string.Empty;
|
public string Text { get; set; } = string.Empty;
|
||||||
|
|
||||||
[JsonPropertyName("quick_replies")]
|
[JsonPropertyName("quick_replies")]
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,5 @@
|
||||||
|
using BotSharp.Abstraction.Messaging.Enums;
|
||||||
|
|
||||||
namespace BotSharp.Abstraction.Messaging.Models.RichContent.Template;
|
namespace BotSharp.Abstraction.Messaging.Models.RichContent.Template;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|
@ -6,13 +8,13 @@ namespace BotSharp.Abstraction.Messaging.Models.RichContent.Template;
|
||||||
public class ButtonTemplateMessage : IRichMessage, ITemplateMessage
|
public class ButtonTemplateMessage : IRichMessage, ITemplateMessage
|
||||||
{
|
{
|
||||||
[JsonPropertyName("rich_type")]
|
[JsonPropertyName("rich_type")]
|
||||||
public string RichType => "button_template";
|
public string RichType => RichTypeEnum.ButtonTemplate;
|
||||||
|
|
||||||
[JsonPropertyName("text")]
|
[JsonPropertyName("text")]
|
||||||
public string Text { get; set; } = string.Empty;
|
public string Text { get; set; } = string.Empty;
|
||||||
|
|
||||||
[JsonPropertyName("template_type")]
|
[JsonPropertyName("template_type")]
|
||||||
public string TemplateType => "button";
|
public string TemplateType => TemplateTypeEnum.Button;
|
||||||
|
|
||||||
[JsonPropertyName("buttons")]
|
[JsonPropertyName("buttons")]
|
||||||
public ButtonElement[] Buttons { get; set; } = new ButtonElement[0];
|
public ButtonElement[] Buttons { get; set; } = new ButtonElement[0];
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,5 @@
|
||||||
|
using BotSharp.Abstraction.Messaging.Enums;
|
||||||
|
|
||||||
namespace BotSharp.Abstraction.Messaging.Models.RichContent.Template;
|
namespace BotSharp.Abstraction.Messaging.Models.RichContent.Template;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|
@ -7,14 +9,14 @@ namespace BotSharp.Abstraction.Messaging.Models.RichContent.Template;
|
||||||
public class CouponTemplateMessage : IRichMessage, ITemplateMessage
|
public class CouponTemplateMessage : IRichMessage, ITemplateMessage
|
||||||
{
|
{
|
||||||
[JsonPropertyName("rich_type")]
|
[JsonPropertyName("rich_type")]
|
||||||
public string RichType => "coupon_template";
|
public string RichType => RichTypeEnum.CouponTemplate;
|
||||||
[JsonIgnore]
|
[JsonIgnore]
|
||||||
public string Text { get; set; }
|
public string Text { get; set; }
|
||||||
public string Title { get; set; }
|
public string Title { get; set; }
|
||||||
public string Subtitle { get; set; }
|
public string Subtitle { get; set; }
|
||||||
|
|
||||||
[JsonPropertyName("template_type")]
|
[JsonPropertyName("template_type")]
|
||||||
public string TemplateType => "coupon";
|
public string TemplateType => TemplateTypeEnum.Coupon;
|
||||||
|
|
||||||
[JsonPropertyName("coupon_code")]
|
[JsonPropertyName("coupon_code")]
|
||||||
public string CouponCode { get; set; }
|
public string CouponCode { get; set; }
|
||||||
|
|
|
||||||
|
|
@ -1,15 +1,17 @@
|
||||||
|
using BotSharp.Abstraction.Messaging.Enums;
|
||||||
|
|
||||||
namespace BotSharp.Abstraction.Messaging.Models.RichContent.Template;
|
namespace BotSharp.Abstraction.Messaging.Models.RichContent.Template;
|
||||||
|
|
||||||
public class GenericTemplateMessage<T> : IRichMessage, ITemplateMessage
|
public class GenericTemplateMessage<T> : IRichMessage, ITemplateMessage
|
||||||
{
|
{
|
||||||
[JsonPropertyName("rich_type")]
|
[JsonPropertyName("rich_type")]
|
||||||
public string RichType => "generic_template";
|
public string RichType => RichTypeEnum.GenericTemplate;
|
||||||
|
|
||||||
[JsonIgnore]
|
[JsonIgnore]
|
||||||
public string Text { get; set; } = string.Empty;
|
public string Text { get; set; } = string.Empty;
|
||||||
|
|
||||||
[JsonPropertyName("template_type")]
|
[JsonPropertyName("template_type")]
|
||||||
public string TemplateType => "generic";
|
public string TemplateType => TemplateTypeEnum.Generic;
|
||||||
|
|
||||||
[JsonPropertyName("elements")]
|
[JsonPropertyName("elements")]
|
||||||
public List<T> Elements { get; set; } = new List<T>();
|
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;
|
namespace BotSharp.Abstraction.Messaging.Models.RichContent.Template;
|
||||||
|
|
||||||
public class MultiSelectTemplateMessage : IRichMessage, ITemplateMessage
|
public class MultiSelectTemplateMessage : IRichMessage, ITemplateMessage
|
||||||
{
|
{
|
||||||
[JsonPropertyName("rich_type")]
|
[JsonPropertyName("rich_type")]
|
||||||
public string RichType => "multi-select_template";
|
public string RichType => RichTypeEnum.MultiSelectTemplate;
|
||||||
|
|
||||||
public string Text { get; set; } = string.Empty;
|
public string Text { get; set; } = string.Empty;
|
||||||
|
|
||||||
[JsonPropertyName("template_type")]
|
[JsonPropertyName("template_type")]
|
||||||
public string TemplateType => "multi-select";
|
public string TemplateType => TemplateTypeEnum.MultiSelect;
|
||||||
public List<OptionElement> Options { get; set; } = new List<OptionElement>();
|
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;
|
namespace BotSharp.Abstraction.Messaging.Models.RichContent.Template;
|
||||||
|
|
||||||
public class ProductTemplateMessage : IRichMessage, ITemplateMessage
|
public class ProductTemplateMessage : IRichMessage, ITemplateMessage
|
||||||
{
|
{
|
||||||
[JsonPropertyName("rich_type")]
|
[JsonPropertyName("rich_type")]
|
||||||
public string RichType => "generic_template";
|
public string RichType => RichTypeEnum.GenericTemplate;
|
||||||
|
|
||||||
[JsonIgnore]
|
[JsonIgnore]
|
||||||
public string Text { get; set; } = string.Empty;
|
public string Text { get; set; } = string.Empty;
|
||||||
|
|
||||||
[JsonPropertyName("template_type")]
|
[JsonPropertyName("template_type")]
|
||||||
public string TemplateType => "product";
|
public string TemplateType => TemplateTypeEnum.Product;
|
||||||
}
|
}
|
||||||
|
|
||||||
public class ProductElement
|
public class ProductElement
|
||||||
|
|
|
||||||
|
|
@ -1,9 +1,11 @@
|
||||||
|
using BotSharp.Abstraction.Messaging.Enums;
|
||||||
|
|
||||||
namespace BotSharp.Abstraction.Messaging.Models.RichContent;
|
namespace BotSharp.Abstraction.Messaging.Models.RichContent;
|
||||||
|
|
||||||
public class TextMessage : IRichMessage
|
public class TextMessage : IRichMessage
|
||||||
{
|
{
|
||||||
[JsonPropertyName("rich_type")]
|
[JsonPropertyName("rich_type")]
|
||||||
public string RichType => "text";
|
public string RichType => RichTypeEnum.Text;
|
||||||
|
|
||||||
public string Text { get; set; } = string.Empty;
|
public string Text { get; set; } = string.Empty;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,5 @@
|
||||||
using BotSharp.Abstraction.Messaging;
|
using BotSharp.Abstraction.Messaging;
|
||||||
|
using BotSharp.Abstraction.Messaging.Enums;
|
||||||
using BotSharp.Abstraction.Messaging.Models.RichContent;
|
using BotSharp.Abstraction.Messaging.Models.RichContent;
|
||||||
|
|
||||||
namespace BotSharp.Core.Messaging;
|
namespace BotSharp.Core.Messaging;
|
||||||
|
|
@ -16,17 +17,17 @@ public class RichContentService : IRichContentService
|
||||||
|
|
||||||
foreach (var m in tempMessages)
|
foreach (var m in tempMessages)
|
||||||
{
|
{
|
||||||
var richType = "text";
|
var richType = RichTypeEnum.Text;
|
||||||
if (m.RootElement.TryGetProperty("rich_type", out var element))
|
if (m.RootElement.TryGetProperty("rich_type", out var element))
|
||||||
{
|
{
|
||||||
richType = element.GetString();
|
richType = element.GetString();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (richType == "text")
|
if (richType == RichTypeEnum.Text)
|
||||||
{
|
{
|
||||||
messages.Add(JsonSerializer.Deserialize<TextMessage>(m.RootElement.ToString(), options));
|
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));
|
messages.Add(JsonSerializer.Deserialize<QuickReplyMessage>(m.RootElement.ToString(), options));
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,7 @@ namespace BotSharp.Plugin.MetaMessenger.MessagingModels;
|
||||||
public class AttachmentMessage : IRichMessage
|
public class AttachmentMessage : IRichMessage
|
||||||
{
|
{
|
||||||
[JsonPropertyName("rich_type")]
|
[JsonPropertyName("rich_type")]
|
||||||
public string RichType => "attachment";
|
public string RichType => RichTypeEnum.Attachment;
|
||||||
|
|
||||||
[JsonIgnore]
|
[JsonIgnore]
|
||||||
public string Text { get; set; }
|
public string Text { get; set; }
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue