Skip to content

Commit

Permalink
大改
Browse files Browse the repository at this point in the history
  • Loading branch information
hatulaile committed Sep 18, 2024
1 parent de91e79 commit 9f18c88
Show file tree
Hide file tree
Showing 31 changed files with 342 additions and 176 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using HeyBoxChatBotCs.Api.Commands.ConsoleCommands;
using HeyBoxChatBotCs.Api.Commands.Interfaces;
using HeyBoxChatBotCs.Api.Enums;

namespace HeyBoxChatBotCs.Api.Commands.CommandSystem;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using HeyBoxChatBotCs.Api.Commands.Interfaces;
using HeyBoxChatBotCs.Api.Enums;
using HeyBoxChatBotCs.Api.Features;

namespace HeyBoxChatBotCs.Api.Commands.CommandSystem;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using HeyBoxChatBotCs.Api.EventArgs.Interfaces;
using HeyBoxChatBotCs.Api.Features;
using HeyBoxChatBotCs.Api.Features;

namespace HeyBoxChatBotCs.Api.Commands.CommandSystem;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using HeyBoxChatBotCs.Api.Commands.Interfaces;
using HeyBoxChatBotCs.Api.EventArgs.Interfaces;
using HeyBoxChatBotCs.Api.Features;
using HeyBoxChatBotCs.Api.Features.Bot;
using HeyBoxChatBotCs.Api.Features.Message;
Expand All @@ -11,7 +10,7 @@ public static class UserCommandProcessor
{
public const string REPLY_MESSAGE = """@{id:%USERID%} %MESSAGE%""";

public static readonly UserCommandHandler UserCommandHandler = CommandSystem.UserCommandHandler.Create();
public static readonly UserCommandHandler UserCommandHandler = UserCommandHandler.Create();

public static event UserSendCommandAction UserSendCommandAction = ProcessorInput;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System.Text;
using HeyBoxChatBotCs.Api.Commands.CommandSystem;
using HeyBoxChatBotCs.Api.Commands.Interfaces;
using HeyBoxChatBotCs.Api.Enums;

namespace HeyBoxChatBotCs.Api.Commands.ConsoleCommands;

Expand All @@ -19,7 +18,7 @@ public bool Execute(ArraySegment<string> args, out string response)
ConsoleCommandProcessor.ConsoleCommandHandler.AllCommand)
{
sb.AppendLine(
$" {++count}.{command.Command} - {command.Description} {(!Misc.Misc.IsArrayNullOrEmpty(command.Aliases) ? $"别名: {string.Join(',', command.Aliases)}" : string.Empty)}");
$" {++count}.{command.Command} - {command.Description} {(!Misc.Misc.IsArrayNullOrEmpty(command.Aliases) ? $"别名: {string.Join(',', command.Aliases!)}" : string.Empty)}");
}

sb.Append($"共 {count} 条指令可使用");
Expand Down
14 changes: 0 additions & 14 deletions HeyBoxChatBotCs.Api/Commands/ConsoleCommands/StopServerCommand.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using HeyBoxChatBotCs.Api.Commands.Interfaces;
using HeyBoxChatBotCs.Api.Enums;
using HeyBoxChatBotCs.Api.Features;
using HeyBoxChatBotCs.Api.Features.Bot;

namespace HeyBoxChatBotCs.Api.Commands.ConsoleCommands;
Expand All @@ -23,18 +21,6 @@ public bool Execute(ArraySegment<string> args, out string response)
response = string.Empty;
return true;
#endif
if (Bot.Instance is null)
{
Log.Error("停止Bot时发现Bot竟然未开启!?");
if (args.Count != 0 && int.TryParse(args.ElementAt(0), out code))
{
}

Misc.Misc.Exit(code);
response = string.Empty;
return true;
}

Bot.Instance.Close();

Check warning on line 24 in HeyBoxChatBotCs.Api/Commands/ConsoleCommands/StopServerCommand.cs

View workflow job for this annotation

GitHub Actions / build

Unreachable code detected
response = "正在停止程序运行!";
return true;
Expand Down
4 changes: 1 addition & 3 deletions HeyBoxChatBotCs.Api/Commands/Interfaces/ICommandBase.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using HeyBoxChatBotCs.Api.Enums;

namespace HeyBoxChatBotCs.Api.Commands.Interfaces;
namespace HeyBoxChatBotCs.Api.Commands.Interfaces;

public interface ICommandBase
{
Expand Down
2 changes: 0 additions & 2 deletions HeyBoxChatBotCs.Api/Commands/Interfaces/IUserCommand.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using HeyBoxChatBotCs.Api.Commands.CommandSystem;
using HeyBoxChatBotCs.Api.Enums;
using HeyBoxChatBotCs.Api.Features;

namespace HeyBoxChatBotCs.Api.Commands.Interfaces;

Expand Down
3 changes: 1 addition & 2 deletions HeyBoxChatBotCs.Api/Delegates.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using HeyBoxChatBotCs.Api.Features;
using HeyBoxChatBotCs.Api.ServerMessageHandler.ServerMessageDatas;
using HeyBoxChatBotCs.Api.ServerMessageHandler.ServerMessageDatas;

namespace HeyBoxChatBotCs.Api;

Expand Down
1 change: 1 addition & 0 deletions HeyBoxChatBotCs.Api/Enums/BotAction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ public enum BotAction
{
Connect,
SendMessage,
Upload
}
4 changes: 1 addition & 3 deletions HeyBoxChatBotCs.Api/EventArgs/Interfaces/IBot.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using HeyBoxChatBotCs.Api.Features.Bot;

namespace HeyBoxChatBotCs.Api.EventArgs.Interfaces;
namespace HeyBoxChatBotCs.Api.EventArgs.Interfaces;

public interface IBot : IEvent
{
Expand Down
84 changes: 78 additions & 6 deletions HeyBoxChatBotCs.Api/Features/Bot/Bot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@
using System.Text.Json;
using HeyBoxChatBotCs.Api.Commands.CommandSystem;
using HeyBoxChatBotCs.Api.Enums;
using HeyBoxChatBotCs.Api.Features.Bot.BotActionResult;
using HeyBoxChatBotCs.Api.Features.Message;
using HeyBoxChatBotCs.Api.Features.Network;
using HeyBoxChatBotCs.Api.Features.Network.HttpBody;

namespace HeyBoxChatBotCs.Api.Features.Bot;

Expand Down Expand Up @@ -88,13 +90,27 @@ public void Close()

string json = JsonSerializer.Serialize(body, BotActionJsonSerializerOptions);
Log.Debug("BOT动作的JSON为:" + json);
return await HttpRequest.Post<T>(uri!, json, new Dictionary<string, string>
return await HttpRequest.Post<T>(uri!, new RawBody(json, contentType), new Dictionary<string, string>
{
{ "token", Token }
}, contentType: contentType);
});
}

public async void SendMessage(MessageBase message)
protected async Task<HttpResponseMessageValue<T?>?> BotSendAction<T>(FormDataBody body, BotAction action)
{
if (!BotRequestUrl.TryGetUri(action, out Uri? uri))
{
Log.Error($"Bot发送{action.ToString()}时未找到URI!");
return default;
}

return await HttpRequest.Post<T>(uri!, body, new Dictionary<string, string>
{
{ "token", Token }
});
}

public void SendMessage(MessageBase message)
{
if (!IsRunning)
{
Expand All @@ -104,8 +120,9 @@ public async void SendMessage(MessageBase message)

try
{
HttpResponseMessageValue<SendMessageResult?>? result =
await BotSendAction<SendMessageResult>(message, BotAction.SendMessage);
HttpResponseMessageValue<BotActionResult<SendMessageResult>?>? result =
BotSendAction<BotActionResult<SendMessageResult>>(message, BotAction.SendMessage)
.Result;
if (result?.Value is null)
{
Log.Error("发送信息后返回的数据为空!");
Expand All @@ -114,7 +131,7 @@ public async void SendMessage(MessageBase message)

if (!result.Response.IsSuccessStatusCode)
{
Log.Error($"发送信息失败,返回信息:{result.Value.Message}!");
Log.Error($"发送信息失败,返回信息:{result.Value.Message}!状态:{result.Value.Status}");
}
else
{
Expand All @@ -126,4 +143,59 @@ public async void SendMessage(MessageBase message)
Log.Error("发送信息时出错:" + exception);
}
}

public void Upload(FileInfo file, string fileName = "")
{
Upload(file.FullName, fileName);
}

public Uri? Upload(string filePath, string fileName = "")
{
try
{
HttpResponseMessageValue<BotActionResult<UploadResult>?>? result;
if (!string.IsNullOrEmpty(fileName))
{
result = BotSendAction<BotActionResult<UploadResult>>(
new FormDataBody(new FormDataBodyFile("file", filePath, fileName)),
BotAction.Upload).Result;
}
else
{
result = BotSendAction<BotActionResult<UploadResult>>(
new FormDataBody(new FormDataBodyFile("file", filePath)),
BotAction.Upload).Result;
}

if (result is null)
{
Log.Error("上传文件文件返回为空!");
return null;
}

if (result.Value is null)
{
Log.Error("上传文件后返回的数据为空!");
return null;
}

if (!result.Response.IsSuccessStatusCode)
{
Log.Error($"上传文件失败,返回信息:{result.Value.Message}!状态:{result.Value.Status}");
return null;
}
else
{
Log.Debug(
$"上传文件返回,信息:\"{result.Value.Message}\",状态:{result.Value.Status},链接:{result.Value.Result.Uri}");
}

return result.Value.Result.Uri;
}
catch (Exception exception)
{
Log.Error("上传媒体文件遇到错误:" + exception);
return null;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System.Text.Json.Serialization;

#pragma warning disable CS8618 // 在退出构造函数时,不可为 null 的字段必须包含非 null 值。请考虑声明为可以为 null。

namespace HeyBoxChatBotCs.Api.Features.Bot.BotActionResult;

public class BotActionResult<TResult> where TResult : new()
{
[JsonPropertyName("msg")] public string Message { get; init; }
[JsonPropertyName("result")] public TResult Result { get; init; }
[JsonPropertyName("status")] public string Status { get; init; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System.Text.Json.Serialization;

namespace HeyBoxChatBotCs.Api.Features.Bot.BotActionResult;

public class SendMessageResult
{
public class MessageResult
{
[JsonPropertyName("chatmobile_ack_id")]
public string ChatMobileAckId { get; init; }

[JsonPropertyName("heychat_ack_id")] public string HeyChatAckId { get; init; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System.Text.Json.Serialization;

namespace HeyBoxChatBotCs.Api.Features.Bot.BotActionResult;

public class UploadResult
{
[JsonPropertyName("url")]
public Uri Uri { get; init; }
}
33 changes: 23 additions & 10 deletions HeyBoxChatBotCs.Api/Features/BotUrl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,33 @@ public static class BotRequestUrl
{
private static Dictionary<BotAction, RequestUri> BotActionToUri { get; } = new()
{
[BotAction.Connect] = new RequestUri("wss://chat.xiaoheihe.cn/", "/chatroom/ws/connect",
new NameValueCollection
{
{ "chat_os_type", "bot" },
{ "client_type", "heybox_chat" },
{ "chat_version", "1.27.2" }
}),
[BotAction.SendMessage] = new RequestUri("https://chat.xiaoheihe.cn/", "/chatroom/v2/channel_msg/send",
new NameValueCollection
{
BotAction.Connect, new RequestUri("wss://chat.xiaoheihe.cn/", "/chatroom/ws/connect",
new NameValueCollection
{
{ "chat_os_type", "bot" },
{ "client_type", "heybox_chat" },
{ "chat_version", "1.27.2" }
})
},
{
BotAction.SendMessage, new RequestUri("https://chat.xiaoheihe.cn/", "/chatroom/v2/channel_msg/send",
new NameValueCollection
{
{ "chat_os_type", "bot" },
{ "client_type", "heybox_chat" },
{ "chat_version", "1.27.2" }
})
},
{
BotAction.Upload,
new RequestUri("https://chat-upload.xiaoheihe.cn/", "/upload", new NameValueCollection()
{
{ "chat_os_type", "bot" },
{ "client_type", "heybox_chat" },
{ "chat_version", "1.27.2" }
}),
})
}
};

public static IReadOnlyDictionary<BotAction, RequestUri> UriDictionary => BotActionToUri;
Expand Down
1 change: 0 additions & 1 deletion HeyBoxChatBotCs.Api/Features/Events/User.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using HeyBoxChatBotCs.Api.EventArgs.User;
using HeyBoxChatBotCs.Api.ServerMessageHandler.ServerMessageDatas;

namespace HeyBoxChatBotCs.Api.Features.Events;

Expand Down
3 changes: 1 addition & 2 deletions HeyBoxChatBotCs.Api/Features/Message/ImageMessage.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Net.Mime;
using System.Text.Json.Serialization;
using System.Text.Json.Serialization;
using HeyBoxChatBotCs.Api.Enums;

namespace HeyBoxChatBotCs.Api.Features.Message;
Expand Down
3 changes: 1 addition & 2 deletions HeyBoxChatBotCs.Api/Features/Message/MarkdownMessage.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Text.Json.Serialization;
using HeyBoxChatBotCs.Api.Enums;

namespace HeyBoxChatBotCs.Api.Features.Message;
Expand Down
3 changes: 1 addition & 2 deletions HeyBoxChatBotCs.Api/Features/Message/MessageBase.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Text.Encodings.Web;
using System.Text.Json;
using System.Text.Json;
using System.Text.Json.Serialization;
using HeyBoxChatBotCs.Api.Enums;

Expand Down
4 changes: 1 addition & 3 deletions HeyBoxChatBotCs.Api/Features/Network/BotWebSocket.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
using System.Collections.Specialized;
using System.Net.WebSockets;
using System.Text;
using System.Web;
using HeyBoxChatBotCs.Api.Enums;

namespace HeyBoxChatBotCs.Api.Features.Network;
Expand Down Expand Up @@ -122,7 +120,7 @@ protected void ReceiveServerMessage(object? ctsObject)
}
else
{
Log.Debug("已接受到服务器信息:" + sb.ToString());
Log.Debug("已接受到服务器信息:" + sb);
ReceiveMessage?.Invoke(sb.ToString());
}

Expand Down
6 changes: 6 additions & 0 deletions HeyBoxChatBotCs.Api/Features/Network/FileBody.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace HeyBoxChatBotCs.Api.Features.Network;

public class FileBody
{

}
Loading

0 comments on commit 9f18c88

Please sign in to comment.