2023-12-18 22:38:12 +00:00
|
|
|
using BotSharp.Abstraction.Messaging;
|
2024-02-24 21:17:37 +00:00
|
|
|
using BotSharp.Abstraction.Messaging.Enums;
|
2023-12-18 22:38:12 +00:00
|
|
|
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)
|
|
|
|
|
{
|
2024-02-24 21:17:37 +00:00
|
|
|
var richType = RichTypeEnum.Text;
|
2023-12-18 22:38:12 +00:00
|
|
|
if (m.RootElement.TryGetProperty("rich_type", out var element))
|
|
|
|
|
{
|
|
|
|
|
richType = element.GetString();
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-24 21:17:37 +00:00
|
|
|
if (richType == RichTypeEnum.Text)
|
2023-12-18 22:38:12 +00:00
|
|
|
{
|
|
|
|
|
messages.Add(JsonSerializer.Deserialize<TextMessage>(m.RootElement.ToString(), options));
|
|
|
|
|
}
|
2024-02-24 21:17:37 +00:00
|
|
|
else if (richType == RichTypeEnum.QuickReply)
|
2023-12-18 22:38:12 +00:00
|
|
|
{
|
|
|
|
|
messages.Add(JsonSerializer.Deserialize<QuickReplyMessage>(m.RootElement.ToString(), options));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return messages.ToList();
|
|
|
|
|
}
|
|
|
|
|
}
|