Skip to content

Commit

Permalink
[OneBot] /send_private_msg and /send_group_msg implemented
Browse files Browse the repository at this point in the history
  • Loading branch information
Linwenxuan authored and Linwenxuan committed Oct 25, 2023
1 parent dcc26c2 commit 881bbfd
Show file tree
Hide file tree
Showing 7 changed files with 137 additions and 40 deletions.
12 changes: 12 additions & 0 deletions Lagrange.OneBot/Core/Entity/Action/OneBotGroupMessage.cs
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 Lagrange.OneBot/Core/Entity/Action/OneBotPrivateMessage.cs
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();
}
63 changes: 63 additions & 0 deletions Lagrange.OneBot/Core/Operation/Message/MessageCommon.cs
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);
}
}
}
}
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 Lagrange.OneBot/Core/Operation/Message/SendMessageOperation.cs
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;
}
}
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();
}
}
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,8 @@ Please use Lagrange.Core responsibly and in accordance with the law.

| API | Support |
| -------------------------- | :-----: |
| [/send_private_msg] | 🔴 |
| [/send_group_msg] | 🔴 |
| [/send_private_msg] | 🟢 |
| [/send_group_msg] | 🟢 |
| [/send_msg] | 🟢 |
| [/delete_msg] | 🔴 |
| [/get_msg] | 🔴 |
Expand Down

0 comments on commit 881bbfd

Please sign in to comment.