Skip to content

Commit

Permalink
[OneBot] Add StringPost Option
Browse files Browse the repository at this point in the history
  • Loading branch information
Linwenxuan04 committed Mar 9, 2024
1 parent 17682b9 commit 13c8e2c
Show file tree
Hide file tree
Showing 8 changed files with 79 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace Lagrange.OneBot.Core.Entity.Message;

[Serializable]
public class OneBotFriendMsgHistoryResponse(List<OneBotPrivateMsg> messages)
public class OneBotFriendMsgHistoryResponse(List<object> messages)
{
[JsonPropertyName("messages")] public List<OneBotPrivateMsg> Messages { get; set; } = messages;
[JsonPropertyName("messages")] public List<object> Messages { get; set; } = messages;
}
22 changes: 22 additions & 0 deletions Lagrange.OneBot/Core/Entity/Message/OneBotGroupMsg.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,25 @@ public class OneBotGroupMsg(uint selfId, uint groupUin, List<OneBotSegment> mess

[JsonPropertyName("sender")] public OneBotGroupSender GroupSender { get; set; } = new(member.Uin, member.MemberName, member.MemberCard ?? string.Empty, (int)member.GroupLevel, member.Permission);
}

[Serializable]
public class OneBotGroupStringMsg(uint selfId, uint groupUin, string message, BotGroupMember member, int messageId) : OneBotEntityBase(selfId, "message")
{
[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; } = messageId;

[JsonPropertyName("group_id")] public uint GroupId { get; set; } = groupUin;

[JsonPropertyName("user_id")] public uint UserId { get; set; } = member.Uin;

[JsonPropertyName("anonymous")] public object? Anonymous { get; set; } = null;

[JsonPropertyName("message")] public string Message { get; set; } = message;

[JsonPropertyName("font")] public int Font { get; set; } = 0;

[JsonPropertyName("sender")] public OneBotGroupSender GroupSender { get; set; } = new(member.Uin, member.MemberName, member.MemberCard ?? string.Empty, (int)member.GroupLevel, member.Permission);
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace Lagrange.OneBot.Core.Entity.Message;

[Serializable]
public class OneBotGroupMsgHistoryResponse(List<OneBotGroupMsg> messages)
public class OneBotGroupMsgHistoryResponse(List<object> messages)
{
[JsonPropertyName("messages")] public List<OneBotGroupMsg> Messages { get; set; } = messages;
[JsonPropertyName("messages")] public List<object> Messages { get; set; } = messages;
}
18 changes: 18 additions & 0 deletions Lagrange.OneBot/Core/Entity/Message/OneBotPrivateMsg.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,23 @@ public class OneBotPrivateMsg(uint selfId, OneBotSender groupSender, string subT

[JsonPropertyName("font")] public int Font { get; set; } = 0;

[JsonPropertyName("sender")] public OneBotSender GroupSender { get; set; } = groupSender;
}

[Serializable]
public class OneBotPrivateStringMsg(uint selfId, OneBotSender groupSender, string subType) : OneBotEntityBase(selfId, "message")
{
[JsonPropertyName("message_type")] public string MessageType { get; set; } = "private";

[JsonPropertyName("sub_type")] public string SubType { get; set; } = subType;

[JsonPropertyName("message_id")] public int MessageId { get; set; }

[JsonPropertyName("user_id")] public uint UserId { get; set; }

[JsonPropertyName("message")] public string Message { get; set; } = string.Empty;

[JsonPropertyName("font")] public int Font { get; set; } = 0;

[JsonPropertyName("sender")] public OneBotSender GroupSender { get; set; } = groupSender;
}
30 changes: 19 additions & 11 deletions Lagrange.OneBot/Message/MessageService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public sealed class MessageService
private readonly LiteDatabase _context;
private readonly IConfiguration _config;
private readonly Dictionary<Type, List<(string Type, SegmentBase Factory)>> _entityToFactory;
private readonly bool _stringPost;

private static readonly JsonSerializerOptions Options;

Expand All @@ -38,6 +39,7 @@ public MessageService(BotContext bot, LagrangeWebSvcCollection service, LiteData
_service = service;
_context = context;
_config = config;
_stringPost = config.GetValue<bool>("Message:StringPost");

var invoker = bot.Invoker;

Expand Down Expand Up @@ -70,16 +72,21 @@ private void OnFriendMessageReceived(BotContext bot, FriendMessageEvent e)
_ = _service.SendJsonAsync(request);
}

public OneBotPrivateMsg ConvertToPrivateMsg(uint uin, MessageChain chain, int hash)
public object ConvertToPrivateMsg(uint uin, MessageChain chain, int hash)
{
var segments = Convert(chain);
var request = new OneBotPrivateMsg(uin, new OneBotSender(chain.FriendUin, chain.FriendInfo?.Nickname ?? string.Empty), "friend")
{
MessageId = hash,
UserId = chain.FriendUin,
Message = segments,
RawMessage = ToRawMessage(segments)
};
object request = _stringPost ? new OneBotPrivateStringMsg(uin, new OneBotSender(chain.FriendUin, chain.FriendInfo?.Nickname ?? string.Empty), "friend")
{
MessageId = hash,
UserId = chain.FriendUin,
Message = ToRawMessage(segments)
} : new OneBotPrivateMsg(uin, new OneBotSender(chain.FriendUin, chain.FriendInfo?.Nickname ?? string.Empty), "friend")
{
MessageId = hash,
UserId = chain.FriendUin,
Message = segments,
RawMessage = ToRawMessage(segments)
};
return request;
}

Expand All @@ -95,11 +102,12 @@ private void OnGroupMessageReceived(BotContext bot, GroupMessageEvent e)
_ = _service.SendJsonAsync(request);
}

public OneBotGroupMsg ConvertToGroupMsg(uint uin, MessageChain chain, int hash)
public object ConvertToGroupMsg(uint uin, MessageChain chain, int hash)
{
var segments = Convert(chain);
var request = new OneBotGroupMsg(uin, chain.GroupUin ?? 0, segments, ToRawMessage(segments),
chain.GroupMemberInfo ?? throw new Exception("Group member not found"), hash);
object request = _stringPost
? new OneBotGroupStringMsg(uin, chain.GroupUin ?? 0, ToRawMessage(segments), chain.GroupMemberInfo ?? throw new Exception("Group member not found"), hash)
: new OneBotGroupMsg(uin, chain.GroupUin ?? 0, segments, ToRawMessage(segments), chain.GroupMemberInfo ?? throw new Exception("Group member not found"), hash);
return request;
}

Expand Down
11 changes: 6 additions & 5 deletions Lagrange.OneBot/Resources/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@
"GetOptimumServer": true
},
"Message": {
"IgnoreSelf": true
"IgnoreSelf": true,
"StringPost": false
},
"QrCode": {
"ConsoleCompatibilityMode": false
},
"Implementations": [
{
Expand All @@ -27,8 +31,5 @@
"HeartBeatInterval": 5000,
"AccessToken": ""
}
],
"QrCode": {
"ConsoleCompatibilityMode": false
}
]
}
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,11 @@ Please use Lagrange.Core responsibly and in accordance with the law.
"GetOptimumServer": true
},
"Message": {
"IgnoreSelf": true
"IgnoreSelf": true,
"StringPost": false
},
"QrCode": {
"ConsoleCompatibilityMode": false
},
"Implementations": [
{
Expand Down
6 changes: 5 additions & 1 deletion README_zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,11 @@ Lagrange.Core 的开发者和贡献者对用户违反法律或从事任何形式
"GetOptimumServer": true
},
"Message": {
"IgnoreSelf": true
"IgnoreSelf": true,
"StringPost": false
},
"QrCode": {
"ConsoleCompatibilityMode": false
},
"Implementations": [
{
Expand Down

0 comments on commit 13c8e2c

Please sign in to comment.