Skip to content

Commit 54513a2

Browse files
committed
[OneBot] Implemented upload_private_file
1 parent b21654f commit 54513a2

3 files changed

Lines changed: 45 additions & 0 deletions

File tree

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
using System.Text.Json.Serialization;
2+
3+
namespace Lagrange.OneBot.Entity.Action;
4+
5+
[Serializable]
6+
public class OneBotUploadPrivateFile
7+
{
8+
[JsonPropertyName("user_id")] public long UserId { get; set; }
9+
10+
[JsonPropertyName("file")] public string File { get; set; } = string.Empty;
11+
12+
[JsonPropertyName("name")] public string? Name { get; set; }
13+
}

Lagrange.OneBot/Entity/OneBotSerializerContext.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ namespace Lagrange.OneBot.Entity;
4747
[JsonSerializable(typeof(OneBotMessage))]
4848
[JsonSerializable(typeof(OneBotMessageResponse))]
4949
[JsonSerializable(typeof(OneBotQrCodeRequest))]
50+
[JsonSerializable(typeof(OneBotUploadPrivateFile))]
5051
public partial class OneBotSerializerContext : JsonSerializerContext;
5152

5253
public class BooleanConverter : JsonConverter<bool>
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
using System.Text.Json.Nodes;
2+
using Lagrange.Core;
3+
using Lagrange.Core.Common.Interface;
4+
using Lagrange.OneBot.Entity.Action;
5+
using Lagrange.OneBot.Utility;
6+
7+
namespace Lagrange.OneBot.Operation.Message;
8+
9+
[Operation("upload_private_file")]
10+
public class SendPrivateFileOperation : IOperation
11+
{
12+
public async Task<OneBotResult> HandleOperation(BotContext context, JsonNode? payload)
13+
{
14+
if (payload.Deserialize<OneBotUploadPrivateFile>() is not { } file) throw new Exception();
15+
16+
var stream = new FileStream(file.File, FileMode.Open);
17+
try
18+
{
19+
await context.SendFriendFile(file.UserId, stream, file.Name);
20+
return new OneBotResult(null, 200, "ok");
21+
}
22+
catch (Exception e)
23+
{
24+
return new OneBotResult(e.Message, 521, "error");
25+
}
26+
finally
27+
{
28+
await stream.DisposeAsync();
29+
}
30+
}
31+
}

0 commit comments

Comments
 (0)