-
Notifications
You must be signed in to change notification settings - Fork 278
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[OneBot] Implemented Segment and get_forward_message
- Loading branch information
Linwenxuan
authored and
Linwenxuan
committed
Nov 8, 2023
1 parent
2cd6194
commit 3da6963
Showing
7 changed files
with
131 additions
and
17 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,9 @@ | ||
using System.Text.Json.Serialization; | ||
|
||
namespace Lagrange.OneBot.Core.Entity.Action; | ||
|
||
[Serializable] | ||
public class OneBotGetForwardMsg | ||
{ | ||
[JsonPropertyName("id")] public string Id { get; set; } = ""; // resId | ||
} |
10 changes: 10 additions & 0 deletions
10
Lagrange.OneBot/Core/Entity/Action/Response/OneBotGetForwardMsgResponse.cs
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,10 @@ | ||
using System.Text.Json.Serialization; | ||
using Lagrange.OneBot.Core.Entity.Message; | ||
|
||
namespace Lagrange.OneBot.Core.Entity.Action.Response; | ||
|
||
[Serializable] | ||
public class OneBotGetForwardMsgResponse(List<OneBotSegment> message) | ||
{ | ||
[JsonPropertyName("message")] public List<OneBotSegment> Message { get; set; } = message; | ||
} |
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,30 @@ | ||
using System.Text.Json.Serialization; | ||
using Lagrange.Core.Message; | ||
using Lagrange.OneBot.Core.Message; | ||
|
||
namespace Lagrange.OneBot.Core.Entity.Message; | ||
|
||
[Serializable] | ||
public class OneBotNode(uint userId, string nickName, List<OneBotSegment> content) : ISegment | ||
{ | ||
[JsonPropertyName("user_id")] public uint UserId { get; set; } = userId; | ||
|
||
[JsonPropertyName("nickname")] public string NickName { get; set; } = nickName; | ||
|
||
[JsonPropertyName("content")] public List<OneBotSegment> Content { get; set; } = content; | ||
|
||
public IMessageEntity ToEntity() | ||
{ | ||
throw new NotImplementedException(); | ||
} | ||
|
||
public void Build(MessageBuilder builder, ISegment segment) | ||
{ | ||
throw new NotImplementedException(); | ||
} | ||
|
||
public ISegment FromEntity(IMessageEntity entity) | ||
{ | ||
throw new NotImplementedException(); | ||
} | ||
} |
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,27 @@ | ||
using System.Text.Json.Serialization; | ||
using Lagrange.Core.Message; | ||
using Lagrange.Core.Message.Entity; | ||
|
||
namespace Lagrange.OneBot.Core.Message.Entity; | ||
|
||
public partial class ForwardSegment(string name) | ||
{ | ||
public ForwardSegment() : this("") { } | ||
|
||
[JsonPropertyName("id")] public string Name { get; set; } = name; | ||
} | ||
|
||
[SegmentSubscriber(typeof(ForwardSegment), "forward")] | ||
public partial class ForwardSegment : ISegment | ||
{ | ||
public IMessageEntity ToEntity() => throw new InvalidOperationException("Only Receive but not send"); | ||
|
||
public void Build(MessageBuilder builder, ISegment segment) => throw new InvalidOperationException(); | ||
|
||
public ISegment FromEntity(IMessageEntity entity) | ||
{ | ||
if (entity is not MultiMsgEntity multiMsg) throw new ArgumentException("Invalid entity type."); | ||
|
||
return new ForwardSegment(multiMsg.ResId ?? ""); | ||
} | ||
} |
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
35 changes: 35 additions & 0 deletions
35
Lagrange.OneBot/Core/Operation/Message/GetForwardMsgOperation.cs
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,35 @@ | ||
using System.Text.Json; | ||
using System.Text.Json.Nodes; | ||
using Lagrange.Core; | ||
using Lagrange.Core.Internal.Event.Protocol.Message; | ||
using Lagrange.OneBot.Core.Entity.Action; | ||
using Lagrange.OneBot.Core.Entity.Action.Response; | ||
using Lagrange.OneBot.Core.Entity.Message; | ||
using Lagrange.OneBot.Core.Message; | ||
|
||
namespace Lagrange.OneBot.Core.Operation.Message; | ||
|
||
[Operation("get_forward_msg")] | ||
public class GetForwardMsgOperation : IOperation | ||
{ | ||
public async Task<OneBotResult> HandleOperation(BotContext context, JsonObject? payload) | ||
{ | ||
if (payload.Deserialize<OneBotGetForwardMsg>() is { } forwardMsg) | ||
{ | ||
var nodes = new List<OneBotSegment>(); | ||
|
||
var @event = MultiMsgDownloadEvent.Create(context.ContextCollection.Keystore.Uid ?? "", forwardMsg.Id); | ||
var results = await context.ContextCollection.Business.SendEvent(@event); | ||
foreach (var chain in ((MultiMsgDownloadEvent)results[0]).Chains ?? throw new Exception()) | ||
{ | ||
var parsed = MessageService.Convert(chain); | ||
var node = new OneBotNode(chain.FriendUin, "", parsed); | ||
nodes.Add(new OneBotSegment("node", node)); | ||
} | ||
|
||
return new OneBotResult(new OneBotGetForwardMsgResponse(nodes), 0, "ok"); | ||
} | ||
|
||
throw new Exception(); | ||
} | ||
} |
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