diff --git a/Lagrange.OneBot/Core/Entity/Message/OneBotGroupMsg.cs b/Lagrange.OneBot/Core/Entity/Message/OneBotGroupMsg.cs index 5ef9d5b9a..e8e6ebfce 100644 --- a/Lagrange.OneBot/Core/Entity/Message/OneBotGroupMsg.cs +++ b/Lagrange.OneBot/Core/Entity/Message/OneBotGroupMsg.cs @@ -5,36 +5,25 @@ namespace Lagrange.OneBot.Core.Entity.Message; [Serializable] -public class OneBotGroupMsg : OneBotEntityBase +public class OneBotGroupMsg(uint selfId, uint groupUin, List message, BotGroupMember member) : OneBotEntityBase(selfId, "message") { - [JsonPropertyName("message_type")] public string MessageType { get; set; } - - [JsonPropertyName("sub_type")] public string SubType { get; set; } - + [JsonPropertyName("message_type")] public string MessageType { get; set; } = "group"; + + [JsonPropertyName("sub_type")] public string SubType { get; set; } = "normal"; + [JsonPropertyName("message_id")] public int MessageId { get; set; } + + [JsonPropertyName("group_id")] public uint GroupId { get; set; } = groupUin; - [JsonPropertyName("group_id")] public uint GroupId { get; set; } - - [JsonPropertyName("user_id")] public uint UserId { get; set; } - - [JsonPropertyName("anonymous")] public object? Anonymous { get; set; } - - [JsonPropertyName("message")] public List Message { get; set; } - - [JsonPropertyName("raw_message")] public string RawMessage { get; set; } - - [JsonPropertyName("font")] public int Font { get; set; } + [JsonPropertyName("user_id")] public uint UserId { get; set; } = member.Uin; - [JsonPropertyName("sender")] public OneBotGroupSender GroupSender { get; set; } - - public OneBotGroupMsg(uint selfId, List message, BotGroupMember member) : base(selfId, "message") - { - MessageType = "group"; - SubType = "normal"; - Anonymous = null; - Message = message; - RawMessage = string.Empty; - Font = 0; - GroupSender = new OneBotGroupSender(member.Uin, member.MemberName, member.MemberCard, (int)member.GroupLevel, member.Permission); - } + [JsonPropertyName("anonymous")] public object? Anonymous { get; set; } = null; + + [JsonPropertyName("message")] public List Message { get; set; } = message; + + [JsonPropertyName("raw_message")] public string RawMessage { get; set; } = string.Empty; + + [JsonPropertyName("font")] public int Font { get; set; } = 0; + + [JsonPropertyName("sender")] public OneBotGroupSender GroupSender { get; set; } = new(member.Uin, member.MemberName, member.MemberCard, (int)member.GroupLevel, member.Permission); } \ No newline at end of file diff --git a/Lagrange.OneBot/Core/Entity/Message/OneBotGroupSender.cs b/Lagrange.OneBot/Core/Entity/Message/OneBotGroupSender.cs index 1754177a5..dde2e5bde 100644 --- a/Lagrange.OneBot/Core/Entity/Message/OneBotGroupSender.cs +++ b/Lagrange.OneBot/Core/Entity/Message/OneBotGroupSender.cs @@ -4,42 +4,29 @@ namespace Lagrange.OneBot.Core.Entity.Message; [Serializable] -public class OneBotGroupSender +public class OneBotGroupSender(uint userId, string nickname, string card, int level, GroupMemberPermission permission) { - [JsonPropertyName("user_id")] public uint UserId { get; set; } - - [JsonPropertyName("nickname")] public string Nickname { get; set; } - - [JsonPropertyName("card")] public string Card { get; set; } - - [JsonPropertyName("sex")] public string Sex { get; set; } - - [JsonPropertyName("age")] public uint Age { get; set; } - - [JsonPropertyName("area")] public string Area { get; set; } - - [JsonPropertyName("level")] public string Level { get; set; } - - [JsonPropertyName("role")] public string Role { get; set; } - - [JsonPropertyName("title")] public string Title { get; set; } - - public OneBotGroupSender(uint userId, string nickname, string card, int level, GroupMemberPermission permission) + [JsonPropertyName("user_id")] public uint UserId { get; set; } = userId; + + [JsonPropertyName("nickname")] public string Nickname { get; set; } = nickname; + + [JsonPropertyName("card")] public string Card { get; set; } = card; + + [JsonPropertyName("sex")] public string Sex { get; set; } = "unknown"; + + [JsonPropertyName("age")] public uint Age { get; set; } = 0; + + [JsonPropertyName("area")] public string Area { get; set; } = string.Empty; + + [JsonPropertyName("level")] public string Level { get; set; } = level.ToString(); + + [JsonPropertyName("role")] public string Role { get; set; } = permission switch { - UserId = userId; - Nickname = nickname; - Card = card; - Sex = "unknown"; - Age = 0; - Area = string.Empty; - Level = level.ToString(); - Role = permission switch - { - GroupMemberPermission.Owner => "owner", - GroupMemberPermission.Admin => "admin", - GroupMemberPermission.Member => "member", - _ => "unknown" - }; - Title = string.Empty; - } + GroupMemberPermission.Owner => "owner", + GroupMemberPermission.Admin => "admin", + GroupMemberPermission.Member => "member", + _ => "unknown" + }; + + [JsonPropertyName("title")] public string Title { get; set; } = string.Empty; } \ No newline at end of file diff --git a/Lagrange.OneBot/Core/Entity/Message/OneBotPrivateMsg.cs b/Lagrange.OneBot/Core/Entity/Message/OneBotPrivateMsg.cs index 5dbe38ce2..c03b04cbe 100644 --- a/Lagrange.OneBot/Core/Entity/Message/OneBotPrivateMsg.cs +++ b/Lagrange.OneBot/Core/Entity/Message/OneBotPrivateMsg.cs @@ -4,31 +4,21 @@ namespace Lagrange.OneBot.Core.Entity.Message; [Serializable] -public class OneBotPrivateMsg : OneBotEntityBase +public class OneBotPrivateMsg(uint selfId) : OneBotEntityBase(selfId, "message") { - [JsonPropertyName("message_type")] public string MessageType { get; set; } - - [JsonPropertyName("sub_type")] public string SubType { get; set; } - + [JsonPropertyName("message_type")] public string MessageType { get; } = "private"; + + [JsonPropertyName("sub_type")] public string SubType { get; } = "friend"; + [JsonPropertyName("message_id")] public int MessageId { get; set; } [JsonPropertyName("user_id")] public uint UserId { get; set; } - [JsonPropertyName("message")] public List Message { get; set; } - - [JsonPropertyName("raw_message")] public string RawMessage { get; set; } - - [JsonPropertyName("font")] public int Font { get; set; } - - [JsonPropertyName("sender")] public OneBotSender GroupSender { get; set; } - - public OneBotPrivateMsg(uint selfId) : base(selfId, "message") - { - MessageType = "private"; - SubType = "friend"; - Message = new List(); - RawMessage = string.Empty; - Font = 0; - GroupSender = new OneBotSender(); - } + [JsonPropertyName("message")] public List Message { get; set; } = new(); + + [JsonPropertyName("raw_message")] public string RawMessage { get; set; } = string.Empty; + + [JsonPropertyName("font")] public int Font { get; set; } = 0; + + [JsonPropertyName("sender")] public OneBotSender GroupSender { get; set; } = new(); } \ No newline at end of file diff --git a/Lagrange.OneBot/Core/Entity/Message/OneBotSegment.cs b/Lagrange.OneBot/Core/Entity/Message/OneBotSegment.cs new file mode 100644 index 000000000..990f69663 --- /dev/null +++ b/Lagrange.OneBot/Core/Entity/Message/OneBotSegment.cs @@ -0,0 +1,11 @@ +using System.Text.Json.Serialization; +using Lagrange.OneBot.Core.Message; + +namespace Lagrange.OneBot.Core.Entity.Message; + +public class OneBotSegment(string type, ISegment data) +{ + [JsonPropertyName("type")] public string Type { get; set; } = type; + + [JsonPropertyName("data")] public object Data { get; set; } = data; +} \ No newline at end of file diff --git a/Lagrange.OneBot/Core/Entity/Meta/OneBotHeartBeat.cs b/Lagrange.OneBot/Core/Entity/Meta/OneBotHeartBeat.cs index 1b04472cc..5bcaaae96 100644 --- a/Lagrange.OneBot/Core/Entity/Meta/OneBotHeartBeat.cs +++ b/Lagrange.OneBot/Core/Entity/Meta/OneBotHeartBeat.cs @@ -3,15 +3,9 @@ namespace Lagrange.OneBot.Core.Entity.Meta; [Serializable] -public class OneBotHeartBeat : OneBotMeta +public class OneBotHeartBeat(uint selfId, int interval, object status) : OneBotMeta(selfId, "heartbeat") { - [JsonPropertyName("interval")] public int Interval { get; set; } - - [JsonPropertyName("status")] public object Status { get; set; } - - public OneBotHeartBeat(uint selfId, int interval, object status) : base(selfId, "heartbeat") - { - Interval = interval; - Status = status; - } + [JsonPropertyName("interval")] public int Interval { get; set; } = interval; + + [JsonPropertyName("status")] public object Status { get; set; } = status; } \ No newline at end of file diff --git a/Lagrange.OneBot/Core/Entity/Meta/OneBotLifecycle.cs b/Lagrange.OneBot/Core/Entity/Meta/OneBotLifecycle.cs index f8ab22246..a5f9dc388 100644 --- a/Lagrange.OneBot/Core/Entity/Meta/OneBotLifecycle.cs +++ b/Lagrange.OneBot/Core/Entity/Meta/OneBotLifecycle.cs @@ -3,12 +3,7 @@ namespace Lagrange.OneBot.Core.Entity.Meta; [Serializable] -public class OneBotLifecycle : OneBotMeta +public class OneBotLifecycle(uint selfId, string subType) : OneBotMeta(selfId, "lifecycle") { - [JsonPropertyName("sub_type")] public string SubType { get; set; } - - public OneBotLifecycle(uint selfId, string subType) : base(selfId, "lifecycle") - { - SubType = subType; - } + [JsonPropertyName("sub_type")] public string SubType { get; set; } = subType; } \ No newline at end of file diff --git a/Lagrange.OneBot/Core/Entity/Meta/OneBotMeta.cs b/Lagrange.OneBot/Core/Entity/Meta/OneBotMeta.cs index afaa8d71d..fd1d91437 100644 --- a/Lagrange.OneBot/Core/Entity/Meta/OneBotMeta.cs +++ b/Lagrange.OneBot/Core/Entity/Meta/OneBotMeta.cs @@ -3,12 +3,7 @@ namespace Lagrange.OneBot.Core.Entity.Meta; [Serializable] -public abstract class OneBotMeta : OneBotEntityBase +public abstract class OneBotMeta(uint selfId, string metaEventType) : OneBotEntityBase(selfId, "meta_event") { - [JsonPropertyName("meta_event_type")] public string MetaEventType { get; set; } - - protected OneBotMeta(uint selfId, string metaEventType) : base(selfId, "meta_event") - { - MetaEventType = metaEventType; - } + [JsonPropertyName("meta_event_type")] public string MetaEventType { get; set; } = metaEventType; } \ No newline at end of file diff --git a/Lagrange.OneBot/Core/Entity/Meta/OneBotStatus.cs b/Lagrange.OneBot/Core/Entity/Meta/OneBotStatus.cs index 5b1e14c1f..ef080ec12 100644 --- a/Lagrange.OneBot/Core/Entity/Meta/OneBotStatus.cs +++ b/Lagrange.OneBot/Core/Entity/Meta/OneBotStatus.cs @@ -3,24 +3,15 @@ namespace Lagrange.OneBot.Core.Entity.Meta; [Serializable] -public class OneBotStatus +public class OneBotStatus(bool online, bool good) { - [JsonPropertyName("app_initialized")] public bool AppInitialized { get; set; } - - [JsonPropertyName("app_enabled")] public bool AppEnabled { get; set; } - - [JsonPropertyName("app_good")] public bool AppGood { get; set; } - - [JsonPropertyName("online")] public bool Online { get; set; } - - [JsonPropertyName("good")] public bool Good { get; set; } - - public OneBotStatus(bool online, bool good) - { - AppInitialized = true; - AppEnabled = true; - AppGood = true; - Online = online; - Good = good; - } + [JsonPropertyName("app_initialized")] public bool AppInitialized { get; set; } = true; + + [JsonPropertyName("app_enabled")] public bool AppEnabled { get; set; } = true; + + [JsonPropertyName("app_good")] public bool AppGood { get; set; } = true; + + [JsonPropertyName("online")] public bool Online { get; set; } = online; + + [JsonPropertyName("good")] public bool Good { get; set; } = good; } \ No newline at end of file diff --git a/Lagrange.OneBot/Core/Entity/OneBotEntityBase.cs b/Lagrange.OneBot/Core/Entity/OneBotEntityBase.cs index f61b75f5d..7f559b882 100644 --- a/Lagrange.OneBot/Core/Entity/OneBotEntityBase.cs +++ b/Lagrange.OneBot/Core/Entity/OneBotEntityBase.cs @@ -3,18 +3,11 @@ namespace Lagrange.OneBot.Core.Entity; [Serializable] -public abstract class OneBotEntityBase +public abstract class OneBotEntityBase(uint selfId, string postType) { - [JsonPropertyName("time")] public long Time { get; set; } - - [JsonPropertyName("self_id")] public uint SelfId { get; set; } - - [JsonPropertyName("post_type")] public string PostType { get; set; } - - protected OneBotEntityBase(uint selfId, string postType) - { - Time = DateTimeOffset.Now.ToUnixTimeSeconds(); - SelfId = selfId; - PostType = postType; - } + [JsonPropertyName("time")] public long Time { get; set; } = DateTimeOffset.Now.ToUnixTimeSeconds(); + + [JsonPropertyName("self_id")] public uint SelfId { get; set; } = selfId; + + [JsonPropertyName("post_type")] public string PostType { get; set; } = postType; } \ No newline at end of file diff --git a/Lagrange.OneBot/Core/Message/Entity/AtSegment.cs b/Lagrange.OneBot/Core/Message/Entity/AtSegment.cs index ca304b0e0..59320e847 100644 --- a/Lagrange.OneBot/Core/Message/Entity/AtSegment.cs +++ b/Lagrange.OneBot/Core/Message/Entity/AtSegment.cs @@ -5,20 +5,19 @@ namespace Lagrange.OneBot.Core.Message.Entity; [Serializable] -public partial class AtSegment +public partial class AtSegment(uint at) { - public AtSegment(uint at) => At = at.ToString(); - - [JsonPropertyName("qq")] public string At { get; set; } + public AtSegment() : this(0) { } + + [JsonPropertyName("qq")] public string At { get; set; } = at.ToString(); } +[SegmentSubscriber(typeof(MentionEntity), "at")] public partial class AtSegment : ISegment { - string ISegment.Type => "at"; - public IMessageEntity ToEntity() => new MentionEntity("", uint.Parse(At)); - public ISegment FromMessageEntity(IMessageEntity entity) + public ISegment FromEntity(IMessageEntity entity) { if (entity is not MentionEntity mentionEntity) throw new ArgumentException("Invalid entity type."); diff --git a/Lagrange.OneBot/Core/Message/Entity/FaceSegment.cs b/Lagrange.OneBot/Core/Message/Entity/FaceSegment.cs index b6453afa2..6e843b247 100644 --- a/Lagrange.OneBot/Core/Message/Entity/FaceSegment.cs +++ b/Lagrange.OneBot/Core/Message/Entity/FaceSegment.cs @@ -5,20 +5,19 @@ namespace Lagrange.OneBot.Core.Message.Entity; [Serializable] -public partial class FaceSegment +public partial class FaceSegment(int id) { - public FaceSegment(int id) => Id = id.ToString(); - - [JsonPropertyName("id")] public string Id { get; set; } + public FaceSegment() : this(0) { } + + [JsonPropertyName("id")] public string Id { get; set; } = id.ToString(); } +[SegmentSubscriber(typeof(FaceSegment), "face")] public partial class FaceSegment : ISegment { - string ISegment.Type => "face"; - public IMessageEntity ToEntity() => new FaceEntity(ushort.Parse(Id), false); - public ISegment FromMessageEntity(IMessageEntity entity) + public ISegment FromEntity(IMessageEntity entity) { if (entity is not FaceEntity faceEntity) throw new ArgumentException("Invalid entity type."); diff --git a/Lagrange.OneBot/Core/Message/Entity/ImageSegment.cs b/Lagrange.OneBot/Core/Message/Entity/ImageSegment.cs new file mode 100644 index 000000000..192753775 --- /dev/null +++ b/Lagrange.OneBot/Core/Message/Entity/ImageSegment.cs @@ -0,0 +1,26 @@ +using System.Text.Json.Serialization; +using Lagrange.Core.Message; +using Lagrange.Core.Message.Entity; + +namespace Lagrange.OneBot.Core.Message.Entity; + +[Serializable] +public partial class ImageSegment(string url) +{ + public ImageSegment() : this("") { } + + [JsonPropertyName("file")] public string Url { get; set; } = url; +} + +[SegmentSubscriber(typeof(ImageEntity), "image")] +public partial class ImageSegment : ISegment +{ + public IMessageEntity ToEntity() => new ImageEntity(url); + + public ISegment FromEntity(IMessageEntity entity) + { + if (entity is not ImageEntity imageEntity) throw new ArgumentException("Invalid entity type."); + + return new ImageSegment(imageEntity.ImageUrl); + } +} \ No newline at end of file diff --git a/Lagrange.OneBot/Core/Message/Entity/TextSegment.cs b/Lagrange.OneBot/Core/Message/Entity/TextSegment.cs index cbfc19cd6..1d91fcc38 100644 --- a/Lagrange.OneBot/Core/Message/Entity/TextSegment.cs +++ b/Lagrange.OneBot/Core/Message/Entity/TextSegment.cs @@ -5,20 +5,19 @@ namespace Lagrange.OneBot.Core.Message.Entity; [Serializable] -public partial class TextSegment +public partial class TextSegment(string text) { - public TextSegment(string text) => Text = text; + public TextSegment() : this("") { } - [JsonPropertyName("text")] public string Text { get; set; } + [JsonPropertyName("text")] public string Text { get; set; } = text; } +[SegmentSubscriber(typeof(TextEntity), "text")] public partial class TextSegment : ISegment { - string ISegment.Type => "text"; - public IMessageEntity ToEntity() => new TextEntity(Text); - public ISegment FromMessageEntity(IMessageEntity entity) + public ISegment FromEntity(IMessageEntity entity) { if (entity is not TextEntity textEntity) throw new ArgumentException("Invalid entity type."); diff --git a/Lagrange.OneBot/Core/Message/ISegment.cs b/Lagrange.OneBot/Core/Message/ISegment.cs index 49255f227..23b3cd80e 100644 --- a/Lagrange.OneBot/Core/Message/ISegment.cs +++ b/Lagrange.OneBot/Core/Message/ISegment.cs @@ -1,12 +1,11 @@ +using System.Text.Json.Serialization; using Lagrange.Core.Message; namespace Lagrange.OneBot.Core.Message; public interface ISegment { - internal string Type { get; } - public IMessageEntity ToEntity(); - public ISegment FromMessageEntity(IMessageEntity entity); + public ISegment FromEntity(IMessageEntity entity); } \ No newline at end of file diff --git a/Lagrange.OneBot/Core/Message/MessageConverter.cs b/Lagrange.OneBot/Core/Message/MessageConverter.cs deleted file mode 100644 index f72a265b1..000000000 --- a/Lagrange.OneBot/Core/Message/MessageConverter.cs +++ /dev/null @@ -1,45 +0,0 @@ -using System.Reflection; -using Lagrange.Core; -using Lagrange.Core.Internal.Event.EventArg; - -namespace Lagrange.OneBot.Core.Message; - -/// -/// The class that converts the OneBot message to/from MessageEntity of Lagrange.Core -/// -public sealed class MessageConverter -{ - public Dictionary EntityToSegment { get; set; } - - public MessageConverter(BotContext bot, ILagrangeWebService service) - { - var invoker = bot.Invoker; - - invoker.OnFriendMessageReceived += OnFriendMessageReceived; - invoker.OnGroupMessageReceived += OnGroupMessageReceived; - invoker.OnTempMessageReceived += OnTempMessageReceived; - - EntityToSegment = new Dictionary(); - foreach (var type in Assembly.GetExecutingAssembly().GetTypes()) - { - if (type.IsSubclassOf(typeof(ISegment))) - { - } - } - } - - private void OnFriendMessageReceived(BotContext bot, FriendMessageEvent e) - { - - } - - private void OnGroupMessageReceived(BotContext bot, GroupMessageEvent e) - { - - } - - private void OnTempMessageReceived(BotContext bot, TempMessageEvent e) - { - - } -} \ No newline at end of file diff --git a/Lagrange.OneBot/Core/Message/MessageService.cs b/Lagrange.OneBot/Core/Message/MessageService.cs new file mode 100644 index 000000000..b03dc905b --- /dev/null +++ b/Lagrange.OneBot/Core/Message/MessageService.cs @@ -0,0 +1,82 @@ +using System.Reflection; +using Lagrange.Core; +using Lagrange.Core.Common.Interface.Api; +using Lagrange.Core.Internal.Event.EventArg; +using Lagrange.Core.Message; +using Lagrange.Core.Utility.Extension; +using Lagrange.OneBot.Core.Entity.Message; + +namespace Lagrange.OneBot.Core.Message; + +/// +/// The class that converts the OneBot message to/from MessageEntity of Lagrange.Core +/// +public sealed class MessageService +{ + private readonly ILagrangeWebService _service; + public Dictionary EntityToSegment { get; set; } + + public MessageService(BotContext bot, ILagrangeWebService service) + { + _service = service; + var invoker = bot.Invoker; + + invoker.OnFriendMessageReceived += OnFriendMessageReceived; + invoker.OnGroupMessageReceived += OnGroupMessageReceived; + invoker.OnTempMessageReceived += OnTempMessageReceived; + + EntityToSegment = new Dictionary(); + foreach (var type in Assembly.GetExecutingAssembly().GetTypes()) + { + var attribute = type.GetCustomAttribute(); + if (attribute != null) + { + EntityToSegment[attribute.Entity] = (attribute.Type, (ISegment)type.CreateInstance(false)); + } + } + } + + private void OnFriendMessageReceived(BotContext bot, FriendMessageEvent e) + { + var request = new OneBotPrivateMsg(bot.UpdateKeystore().Uin) + { + MessageId = 0, + UserId = e.Chain.FriendUin, + GroupSender = new OneBotSender + { + + }, + Message = Convert(e.Chain) + }; + + _service.SendJsonAsync(request); + } + + private void OnGroupMessageReceived(BotContext bot, GroupMessageEvent e) + { + var request = new OneBotGroupMsg(bot.UpdateKeystore().Uin, e.Chain.GroupUin ?? 0,Convert(e.Chain), + e.Chain.GroupMemberInfo ?? throw new Exception("Group member not found")); + + _service.SendJsonAsync(request); + } + + private void OnTempMessageReceived(BotContext bot, TempMessageEvent e) + { + // TODO: Implement temp msg + } + + private List Convert(IEnumerable entities) + { + var result = new List(); + + foreach (var entity in entities) + { + if (EntityToSegment.TryGetValue(entity.GetType(), out var instance)) + { + result.Add(new OneBotSegment(instance.Item1, instance.Item2.FromEntity(entity))); + } + } + + return result; + } +} \ No newline at end of file diff --git a/Lagrange.OneBot/Core/Message/SegmentSubscriberAttribute.cs b/Lagrange.OneBot/Core/Message/SegmentSubscriberAttribute.cs new file mode 100644 index 000000000..9931ae32c --- /dev/null +++ b/Lagrange.OneBot/Core/Message/SegmentSubscriberAttribute.cs @@ -0,0 +1,9 @@ +namespace Lagrange.OneBot.Core.Message; + +[AttributeUsage(AttributeTargets.Class)] +public class SegmentSubscriberAttribute(Type entity, string type) : Attribute +{ + public Type Entity { get; } = entity; + + public string Type { get; } = type; +} \ No newline at end of file diff --git a/Lagrange.OneBot/Core/Service/ReverseWSService.cs b/Lagrange.OneBot/Core/Service/ReverseWSService.cs index cd1d61c49..b68cd02ed 100644 --- a/Lagrange.OneBot/Core/Service/ReverseWSService.cs +++ b/Lagrange.OneBot/Core/Service/ReverseWSService.cs @@ -1,4 +1,5 @@ using System.Net.WebSockets; +using System.Text; using System.Text.Json; using Lagrange.OneBot.Core.Entity.Meta; using Microsoft.Extensions.Configuration; @@ -30,10 +31,13 @@ public ReverseWSService(IConfiguration config, ILogger logger) { var socket = new ClientWebSocket(); + SetRequestHeader(socket, new Dictionary + { + { "X-Client-Role", "Universal" }, + { "X-Self-ID", _config.GetValue("Account:Uin").ToString() }, + { "User-Agent", Constant.OneBotImpl } + }); socket.Options.KeepAliveInterval = TimeSpan.FromSeconds(5); - socket.Options.SetRequestHeader("X-Client-Role", "Universal"); - socket.Options.SetRequestHeader("X-Self-ID", _config.GetValue("Account:Uin").ToString()); - socket.Options.SetRequestHeader("User-Agent", Constant.OneBotImpl); if (_config["AccessToken"] != null) socket.Options.SetRequestHeader("Authorization", $"Bearer {_config["AccessToken"]}"); return socket; @@ -61,14 +65,23 @@ public Task StopAsync(CancellationToken cancellationToken) public Task SendJsonAsync(T json, CancellationToken cancellationToken = default) { var payload = JsonSerializer.SerializeToUtf8Bytes(json); + Console.WriteLine(Encoding.UTF8.GetString(payload)); return _socket.SendInstant(payload); } private void OnHeartbeat(object? sender) { var status = new OneBotStatus(true, true); - var heartBeat = new OneBotHeartBeat(_config.GetValue("Account:Uin"), _config.GetValue("Implementation:ReverseWebSocket:HeartBeatInterval"), status); + var heartBeat = new OneBotHeartBeat( + _config.GetValue("Account:Uin"), + _config.GetValue("Implementation:ReverseWebSocket:HeartBeatInterval"), + status); SendJsonAsync(heartBeat); } + + private static void SetRequestHeader(ClientWebSocket webSocket, Dictionary headers) + { + foreach (var (key, value) in headers) webSocket.Options.SetRequestHeader(key, value); + } } \ No newline at end of file diff --git a/Lagrange.OneBot/Lagrange.OneBot.csproj b/Lagrange.OneBot/Lagrange.OneBot.csproj index 268942800..cb5118343 100644 --- a/Lagrange.OneBot/Lagrange.OneBot.csproj +++ b/Lagrange.OneBot/Lagrange.OneBot.csproj @@ -2,7 +2,8 @@ Exe - net6.0 + net7.0 + preview enable enable diff --git a/Lagrange.OneBot/LagrangeApp.cs b/Lagrange.OneBot/LagrangeApp.cs index e59f4e178..d08f30615 100644 --- a/Lagrange.OneBot/LagrangeApp.cs +++ b/Lagrange.OneBot/LagrangeApp.cs @@ -26,14 +26,14 @@ public class LagrangeApp : IHost public ILagrangeWebService WebService => Services.GetRequiredService(); - public MessageConverter Converter { get; set; } + public MessageService Service { get; set; } internal LagrangeApp(IHost host) { _hostApp = host; Logger = Services.GetRequiredService>(); - Converter = new MessageConverter(Instance, WebService); + Service = new MessageService(Instance, WebService); } public async Task StartAsync(CancellationToken cancellationToken = new())