From fac253bbf285725b295f01d640f0259a53ee18e5 Mon Sep 17 00:00:00 2001 From: Linwenxuan <116782992+Linwenxuan05@users.noreply.github.com> Date: Fri, 13 Oct 2023 15:30:56 +0800 Subject: [PATCH] Download image --- .../Core/Message/Entity/ImageSegment.cs | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/Lagrange.OneBot/Core/Message/Entity/ImageSegment.cs b/Lagrange.OneBot/Core/Message/Entity/ImageSegment.cs index d60ed5559..3e16bbadc 100644 --- a/Lagrange.OneBot/Core/Message/Entity/ImageSegment.cs +++ b/Lagrange.OneBot/Core/Message/Entity/ImageSegment.cs @@ -7,8 +7,10 @@ namespace Lagrange.OneBot.Core.Message.Entity; [Serializable] public partial class ImageSegment(string url) { - public ImageSegment() : this("") { } - + private readonly HttpClient _client = new(); + + public ImageSegment() : this("") => _client = new HttpClient(); + [JsonPropertyName("file")] public string Url { get; set; } = url; } @@ -17,8 +19,14 @@ public partial class ImageSegment : ISegment { public IMessageEntity ToEntity() => new ImageEntity(url); - public void Build(MessageBuilder builder, ISegment segment) => throw new NotImplementedException(); - + public void Build(MessageBuilder builder, ISegment segment) + { + if (segment is ImageSegment imageSegment) + { + if (imageSegment.Url != "") builder.Image(_client.GetAsync(imageSegment.Url).Result.Content.ReadAsByteArrayAsync().Result); + } + } + public ISegment FromEntity(IMessageEntity entity) { if (entity is not ImageEntity imageEntity) throw new ArgumentException("Invalid entity type.");