-
Notifications
You must be signed in to change notification settings - Fork 279
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[OneBot] Support Receive for RecordSegment.cs
- Loading branch information
Linwenxuan
authored and
Linwenxuan
committed
Feb 2, 2024
1 parent
8040381
commit d152345
Showing
4 changed files
with
66 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
namespace Lagrange.OneBot.Core.Message.Entity; | ||
|
||
public static class CommonResolver | ||
{ | ||
private static readonly HttpClient Client = new(); | ||
|
||
public static byte[]? Resolve(string url) | ||
{ | ||
if (url.StartsWith("http")) | ||
{ | ||
return Client.GetAsync(url).Result.Content.ReadAsByteArrayAsync().Result; | ||
} | ||
|
||
if (url.StartsWith("file")) | ||
{ | ||
string path = new Uri(url).LocalPath; | ||
return File.ReadAllBytes(path); | ||
} | ||
|
||
if (url.StartsWith("base64")) | ||
{ | ||
string base64 = url.Replace("base64://", ""); | ||
return Convert.FromBase64String(base64); | ||
} | ||
|
||
return null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
using System.Text.Json.Serialization; | ||
using Lagrange.Core.Message; | ||
using Lagrange.Core.Message.Entity; | ||
|
||
namespace Lagrange.OneBot.Core.Message.Entity; | ||
|
||
[Serializable] | ||
public partial class RecordSegment(string url) | ||
{ | ||
public RecordSegment() : this("") { } | ||
|
||
[JsonPropertyName("file")] public string Url { get; set; } = url; | ||
} | ||
|
||
[SegmentSubscriber(typeof(RecordEntity), "record")] | ||
public partial class RecordSegment : ISegment | ||
{ | ||
public IMessageEntity ToEntity() => new ImageEntity(url); | ||
|
||
public void Build(MessageBuilder builder, ISegment segment) | ||
{ | ||
if (segment is RecordSegment recordSegment and not { Url: "" } && CommonResolver.Resolve(recordSegment.Url) is { } image) | ||
{ | ||
builder.Record(image); | ||
Check failure on line 24 in Lagrange.OneBot/Core/Message/Entity/RecordSegment.cs GitHub Actions / Build (linux-arm64)
|
||
} | ||
} | ||
|
||
public ISegment FromEntity(IMessageEntity entity) | ||
{ | ||
if (entity is not RecordEntity recordEntity) throw new ArgumentException("Invalid entity type."); | ||
|
||
return new RecordSegment(recordEntity.AudioUrl); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters