-
Notifications
You must be signed in to change notification settings - Fork 272
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[OneBot] /send_private_msg and /send_group_msg implemented
- Loading branch information
Linwenxuan
authored and
Linwenxuan
committed
Oct 25, 2023
1 parent
dcc26c2
commit 881bbfd
Showing
7 changed files
with
137 additions
and
40 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,12 @@ | ||
using System.Text.Json.Serialization; | ||
using Lagrange.OneBot.Core.Entity.Message; | ||
|
||
namespace Lagrange.OneBot.Core.Entity.Action; | ||
|
||
[Serializable] | ||
public class OneBotGroupMessage | ||
{ | ||
[JsonPropertyName("group_id")] public uint GroupId { get; set; } | ||
|
||
[JsonPropertyName("message")] public List<OneBotSegment> Messages { get; set; } = new(); | ||
} |
12 changes: 12 additions & 0 deletions
12
Lagrange.OneBot/Core/Entity/Action/OneBotPrivateMessage.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,12 @@ | ||
using System.Text.Json.Serialization; | ||
using Lagrange.OneBot.Core.Entity.Message; | ||
|
||
namespace Lagrange.OneBot.Core.Entity.Action; | ||
|
||
[Serializable] | ||
public class OneBotPrivateMessage | ||
{ | ||
[JsonPropertyName("user_id")] public uint UserId { get; set; } | ||
|
||
[JsonPropertyName("message")] public List<OneBotSegment> Messages { get; set; } = new(); | ||
} |
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,63 @@ | ||
using System.Reflection; | ||
using System.Text.Json; | ||
using Lagrange.Core.Message; | ||
using Lagrange.Core.Utility.Extension; | ||
using Lagrange.OneBot.Core.Entity.Action; | ||
using Lagrange.OneBot.Core.Entity.Message; | ||
using Lagrange.OneBot.Core.Message; | ||
|
||
namespace Lagrange.OneBot.Core.Operation.Message; | ||
|
||
public static class MessageCommon | ||
{ | ||
private static readonly Dictionary<string, ISegment> TypeToSegment; | ||
|
||
static MessageCommon() | ||
{ | ||
TypeToSegment = new Dictionary<string, ISegment>(); | ||
|
||
foreach (var type in Assembly.GetExecutingAssembly().GetTypes()) | ||
{ | ||
var attribute = type.GetCustomAttribute<SegmentSubscriberAttribute>(); | ||
if (attribute != null) TypeToSegment[attribute.Type] = (ISegment)type.CreateInstance(false); | ||
} | ||
} | ||
|
||
public static MessageBuilder ParseChain(OneBotMessage message) | ||
{ | ||
var builder = message.MessageType == "private" | ||
? MessageBuilder.Friend(message.UserId) | ||
: MessageBuilder.Group(message.GroupId); | ||
BuildMessages(builder, message.Messages); | ||
|
||
return builder; | ||
} | ||
|
||
public static MessageBuilder ParseChain(OneBotPrivateMessage message) | ||
{ | ||
var builder = MessageBuilder.Friend(message.UserId); | ||
BuildMessages(builder, message.Messages); | ||
|
||
return builder; | ||
} | ||
|
||
public static MessageBuilder ParseChain(OneBotGroupMessage message) | ||
{ | ||
var builder = MessageBuilder.Group(message.GroupId); | ||
BuildMessages(builder, message.Messages); | ||
|
||
return builder; | ||
} | ||
|
||
private static void BuildMessages(MessageBuilder builder, List<OneBotSegment> segments) | ||
{ | ||
foreach (var segment in segments) | ||
{ | ||
if (TypeToSegment.TryGetValue(segment.Type, out var instance)) | ||
{ | ||
var cast = (ISegment)((JsonElement)segment.Data).Deserialize(instance.GetType())!; | ||
instance.Build(builder, cast); | ||
} | ||
} | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
Lagrange.OneBot/Core/Operation/Message/SendGroupMessageOperation.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,23 @@ | ||
using System.Text.Json; | ||
using System.Text.Json.Nodes; | ||
using Lagrange.Core; | ||
using Lagrange.Core.Common.Interface.Api; | ||
using Lagrange.OneBot.Core.Entity.Action; | ||
using Lagrange.OneBot.Core.Entity.Action.Response; | ||
|
||
namespace Lagrange.OneBot.Core.Operation.Message; | ||
|
||
[Operation("send_group_msg")] | ||
public sealed class SendGroupMessageOperation : IOperation | ||
{ | ||
public async Task<OneBotResult> HandleOperation(string echo, BotContext context, JsonObject? payload) | ||
{ | ||
if (payload.Deserialize<OneBotGroupMessage>() is { } message) | ||
{ | ||
await context.SendMessage(MessageCommon.ParseChain(message).Build()); | ||
return new OneBotResult(new OneBotMessageResponse(0), 0, "ok", echo); | ||
} | ||
|
||
throw new Exception(); | ||
} | ||
} |
40 changes: 2 additions & 38 deletions
40
Lagrange.OneBot/Core/Operation/Message/SendMessageOperation.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 |
---|---|---|
@@ -1,59 +1,23 @@ | ||
using System.Reflection; | ||
using System.Text.Json; | ||
using System.Text.Json.Nodes; | ||
using Lagrange.Core; | ||
using Lagrange.Core.Common.Interface.Api; | ||
using Lagrange.Core.Message; | ||
using Lagrange.Core.Utility.Extension; | ||
using Lagrange.OneBot.Core.Entity.Action; | ||
using Lagrange.OneBot.Core.Entity.Action.Response; | ||
using Lagrange.OneBot.Core.Message; | ||
|
||
namespace Lagrange.OneBot.Core.Operation.Message; | ||
|
||
[Operation("send_msg")] | ||
public sealed class SendMessageOperation : IOperation | ||
{ | ||
private readonly Dictionary<string, ISegment> _typeToSegment; | ||
|
||
public SendMessageOperation() | ||
{ | ||
_typeToSegment = new Dictionary<string, ISegment>(); | ||
|
||
foreach (var type in Assembly.GetExecutingAssembly().GetTypes()) | ||
{ | ||
var attribute = type.GetCustomAttribute<SegmentSubscriberAttribute>(); | ||
if (attribute != null) _typeToSegment[attribute.Type] = (ISegment)type.CreateInstance(false); | ||
} | ||
} | ||
|
||
public async Task<OneBotResult> HandleOperation(string echo, BotContext context, JsonObject? payload) | ||
{ | ||
var message = payload.Deserialize<OneBotMessage>(); | ||
if (message != null) | ||
if (payload.Deserialize<OneBotMessage>() is { } message) | ||
{ | ||
await context.SendMessage(ParseChain(message).Build()); | ||
await context.SendMessage(MessageCommon.ParseChain(message).Build()); | ||
return new OneBotResult(new OneBotMessageResponse(0), 0, "ok", echo); | ||
} | ||
|
||
throw new Exception(); | ||
} | ||
|
||
private MessageBuilder ParseChain(OneBotMessage message) | ||
{ | ||
var builder = message.MessageType == "private" | ||
? MessageBuilder.Friend(message.UserId) | ||
: MessageBuilder.Group(message.GroupId); | ||
|
||
foreach (var segment in message.Messages) | ||
{ | ||
if (_typeToSegment.TryGetValue(segment.Type, out var instance)) | ||
{ | ||
var cast = (ISegment)((JsonElement)segment.Data).Deserialize(instance.GetType())!; | ||
instance.Build(builder, cast); | ||
} | ||
} | ||
|
||
return builder; | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
Lagrange.OneBot/Core/Operation/Message/SendPrivateMessageOperation.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,23 @@ | ||
using System.Text.Json; | ||
using System.Text.Json.Nodes; | ||
using Lagrange.Core; | ||
using Lagrange.Core.Common.Interface.Api; | ||
using Lagrange.OneBot.Core.Entity.Action; | ||
using Lagrange.OneBot.Core.Entity.Action.Response; | ||
|
||
namespace Lagrange.OneBot.Core.Operation.Message; | ||
|
||
[Operation("send_private_msg")] | ||
public sealed class SendPrivateMessageOperation : IOperation | ||
{ | ||
public async Task<OneBotResult> HandleOperation(string echo, BotContext context, JsonObject? payload) | ||
{ | ||
if (payload.Deserialize<OneBotPrivateMessage>() is { } message) | ||
{ | ||
await context.SendMessage(MessageCommon.ParseChain(message).Build()); | ||
return new OneBotResult(new OneBotMessageResponse(0), 0, "ok", echo); | ||
} | ||
|
||
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