BotSharp/src/Infrastructure/BotSharp.Core/Messaging/RichContentService.cs

38 lines
1.1 KiB
C#
Raw Normal View History

2023-12-18 22:38:12 +00:00
using BotSharp.Abstraction.Messaging;
using BotSharp.Abstraction.Messaging.Models.RichContent;
namespace BotSharp.Core.Messaging;
public class RichContentService : IRichContentService
{
public List<IRichMessage> ConvertToMessages(string content)
{
var messages = new List<IRichMessage>();
var options = new JsonSerializerOptions
{
PropertyNameCaseInsensitive = true
};
var tempMessages = JsonSerializer.Deserialize<JsonDocument[]>(content, options);
foreach (var m in tempMessages)
{
var richType = "text";
if (m.RootElement.TryGetProperty("rich_type", out var element))
{
richType = element.GetString();
}
if (richType == "text")
{
messages.Add(JsonSerializer.Deserialize<TextMessage>(m.RootElement.ToString(), options));
}
else if (richType == "quick_reply")
{
messages.Add(JsonSerializer.Deserialize<QuickReplyMessage>(m.RootElement.ToString(), options));
}
}
return messages.ToList();
}
}