-
Notifications
You must be signed in to change notification settings - Fork 266
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update Refactoring of Lagrange.OneBot
- Loading branch information
1 parent
5b187c3
commit 2d71b5c
Showing
16 changed files
with
250 additions
and
45 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
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,40 @@ | ||
using System.Text.Json.Serialization; | ||
using Lagrange.Core.Common.Entity; | ||
using Lagrange.OneBot.Core.Message; | ||
|
||
namespace Lagrange.OneBot.Core.Entity.Message; | ||
|
||
[Serializable] | ||
public class OneBotGroupMsg : OneBotEntityBase | ||
{ | ||
[JsonPropertyName("message_type")] public string MessageType { get; set; } | ||
|
||
[JsonPropertyName("sub_type")] public string SubType { get; set; } | ||
|
||
[JsonPropertyName("message_id")] public int MessageId { get; set; } | ||
|
||
[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<ISegment> Message { get; set; } | ||
|
||
[JsonPropertyName("raw_message")] public string RawMessage { get; set; } | ||
|
||
[JsonPropertyName("font")] public int Font { get; set; } | ||
|
||
[JsonPropertyName("sender")] public OneBotGroupSender GroupSender { get; set; } | ||
|
||
public OneBotGroupMsg(uint selfId, List<ISegment> 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); | ||
} | ||
} |
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,45 @@ | ||
using System.Text.Json.Serialization; | ||
using Lagrange.Core.Common.Entity; | ||
|
||
namespace Lagrange.OneBot.Core.Entity.Message; | ||
|
||
[Serializable] | ||
public class OneBotGroupSender | ||
{ | ||
[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) | ||
{ | ||
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; | ||
} | ||
} |
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,34 @@ | ||
using System.Text.Json.Serialization; | ||
using Lagrange.OneBot.Core.Message; | ||
|
||
namespace Lagrange.OneBot.Core.Entity.Message; | ||
|
||
[Serializable] | ||
public class OneBotPrivateMsg : OneBotEntityBase | ||
{ | ||
[JsonPropertyName("message_type")] public string MessageType { get; set; } | ||
|
||
[JsonPropertyName("sub_type")] public string SubType { get; set; } | ||
|
||
[JsonPropertyName("message_id")] public int MessageId { get; set; } | ||
|
||
[JsonPropertyName("user_id")] public uint UserId { get; set; } | ||
|
||
[JsonPropertyName("message")] public List<ISegment> 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<ISegment>(); | ||
RawMessage = string.Empty; | ||
Font = 0; | ||
GroupSender = new OneBotSender(); | ||
} | ||
} |
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,7 @@ | ||
namespace Lagrange.OneBot.Core.Entity.Message; | ||
|
||
[Serializable] | ||
public class OneBotSender | ||
{ | ||
|
||
} |
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
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
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
This file was deleted.
Oops, something went wrong.
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,6 +1,12 @@ | ||
using Lagrange.Core.Message; | ||
|
||
namespace Lagrange.OneBot.Core.Message; | ||
|
||
public interface ISegment | ||
{ | ||
internal string Type { get; } | ||
|
||
public IMessageEntity ToEntity(); | ||
|
||
public ISegment FromMessageEntity(IMessageEntity entity); | ||
} |
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,9 +1,45 @@ | ||
using System.Reflection; | ||
using Lagrange.Core; | ||
using Lagrange.Core.Core.Event.EventArg; | ||
|
||
namespace Lagrange.OneBot.Core.Message; | ||
|
||
/// <summary> | ||
/// The class that converts the OneBot message to/from MessageEntity of Lagrange.Core | ||
/// </summary> | ||
public class MessageConverter | ||
public sealed class MessageConverter | ||
{ | ||
public Dictionary<Type, Type> 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<Type, Type>(); | ||
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) | ||
{ | ||
|
||
} | ||
} |
This file was deleted.
Oops, something went wrong.
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,49 @@ | ||
using System.Text; | ||
using System.Text.Json; | ||
using Lagrange.OneBot.Core.Entity.Meta; | ||
using Microsoft.Extensions.Configuration; | ||
using Microsoft.Extensions.Logging; | ||
|
||
namespace Lagrange.OneBot.Core.Service; | ||
|
||
public sealed class HttpPostService : ILagrangeWebService | ||
{ | ||
private readonly HttpClient _client; | ||
|
||
private readonly IConfiguration _config; | ||
|
||
private readonly ILogger _logger; | ||
|
||
private readonly string _url; | ||
|
||
public HttpPostService(IConfiguration config, ILogger<LagrangeApp> logger) | ||
{ | ||
_config = config; | ||
_logger = logger; | ||
_client = new HttpClient(); | ||
|
||
_client.DefaultRequestHeaders.Add("User-Agent", "Lagrange"); | ||
_client.DefaultRequestHeaders.Add("X-Self-ID", _config.GetValue<uint>("Account:Uin").ToString()); | ||
|
||
var conf = _config.GetSection("Implementation").GetSection("HttpPost"); | ||
_url = $"http://{conf["Host"]}:{conf["Port"]}{conf["Suffix"]}"; | ||
} | ||
|
||
public async Task StartAsync(CancellationToken cancellationToken) | ||
{ | ||
var lifecycle = new OneBotLifecycle(_config.GetValue<uint>("Account:Uin"), "enable"); | ||
await SendJsonAsync(lifecycle, cancellationToken); | ||
} | ||
|
||
public async Task StopAsync(CancellationToken cancellationToken) | ||
{ | ||
var lifecycle = new OneBotLifecycle(_config.GetValue<uint>("Account:Uin"), "disable"); | ||
await SendJsonAsync(lifecycle, cancellationToken); | ||
} | ||
|
||
public async ValueTask SendJsonAsync<T>(T json, CancellationToken cancellationToken = default) | ||
{ | ||
string payload = JsonSerializer.Serialize(json); | ||
await _client.PostAsync(_url, new StringContent(payload, Encoding.UTF8), cancellationToken); | ||
} | ||
} |
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
Oops, something went wrong.