Skip to content

Commit

Permalink
修复可能出现的错误
Browse files Browse the repository at this point in the history
  • Loading branch information
hatulaile committed Sep 17, 2024
1 parent 26505a2 commit de91e79
Show file tree
Hide file tree
Showing 5 changed files with 177 additions and 80 deletions.
32 changes: 24 additions & 8 deletions HeyBoxChatBotCs.Api/Features/Bot/Bot.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Text.Encodings.Web;
using System.Text.Json;
using HeyBoxChatBotCs.Api.Commands.CommandSystem;
using HeyBoxChatBotCs.Api.Enums;
Expand All @@ -8,6 +9,11 @@ namespace HeyBoxChatBotCs.Api.Features.Bot;

public class Bot
{
public static readonly JsonSerializerOptions BotActionJsonSerializerOptions = new()
{
Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping
};

public static Bot? Instance { get; private set; }

public Bot(string id, string token)
Expand Down Expand Up @@ -68,22 +74,24 @@ public void Close()
IsRunning = false;
BotWebSocket?.Dispose();
Log.Info("已关闭Bot,即将退出程序!");
Misc.Misc.Exit(0);
Misc.Misc.Exit();
}

protected async Task<T?> BotSendAction<T>(object body, BotAction action)
protected async Task<HttpResponseMessageValue<T?>?> BotSendAction<T>(object body, BotAction action,
string contentType = "application/json")
{
if (!BotRequestUrl.TryGetUri(action, out Uri? uri))
{
Log.Error($"Bot发送{action.ToString()}时未找到URI!");
return default;
}

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

public async void SendMessage(MessageBase message)
Expand All @@ -96,14 +104,22 @@ public async void SendMessage(MessageBase message)

try
{
SendMessageResult? result = await BotSendAction<SendMessageResult>(message, BotAction.SendMessage);
if (result is null)
HttpResponseMessageValue<SendMessageResult?>? result =
await BotSendAction<SendMessageResult>(message, BotAction.SendMessage);
if (result?.Value is null)
{
Log.Error("发送信息后返回的数据为空!");
return;
}

Log.Debug($"发送信息 \"{result.Message}\" 成功,状态:{result.Status}.");
if (!result.Response.IsSuccessStatusCode)
{
Log.Error($"发送信息失败,返回信息:{result.Value.Message}!");
}
else
{
Log.Debug($"发送信息返回,信息:\"{result.Value.Message}\",状态:{result.Value.Status}.");
}
}
catch (Exception exception)
{
Expand Down
29 changes: 29 additions & 0 deletions HeyBoxChatBotCs.Api/Features/Message/ImageMessage.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using System.Net.Mime;
using System.Text.Json.Serialization;
using HeyBoxChatBotCs.Api.Enums;

namespace HeyBoxChatBotCs.Api.Features.Message;

public class ImageMessage : MessageBase
{
public ImageMessage(string image, string channelId, string roomId, string replyMessageId = "", string? ackId = null)
: this(new Uri(image),
channelId, roomId, replyMessageId, ackId)
{
}

public ImageMessage(Uri image, Channel channelId, Room roomId, string replyMessageId = "", string? ackId = null) :
this(image,
channelId.Id, roomId.Id, replyMessageId, ackId)
{
}

public ImageMessage(Uri image, string channelId, string roomId, string replyMessageId = "",
string? ackId = null) : base(
ackId ?? MessageAck.GetAckId(), MessageType.Image, channelId, roomId, replyMessageId)
{
Uri = image;
}

[JsonPropertyName("img")] public Uri Uri { get; init; }
}
39 changes: 3 additions & 36 deletions HeyBoxChatBotCs.Api/Features/Message/MarkdownMessage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public class MarkdownMessage : MessageBase
{
public MarkdownMessage(string message, Room room, Channel channel, string replyMessageId = "",
User[]? atUser = null,
string[]? atRole = null, Channel[]? mentionChannel = null, MarkdownMessageAddition? addition = null,
string[]? atRole = null, Channel[]? mentionChannel = null, MessageAddition? addition = null,
string? ackId = null) : this(
message,
room.Id, channel.Id, replyMessageId, atUser?.Select(x => x.UserId.ToString()).ToArray(), atRole,
Expand All @@ -18,54 +18,21 @@ public MarkdownMessage(string message, Room room, Channel channel, string replyM

public MarkdownMessage(string message, string roomId, string channelId, string replyMessageId = "",
string[]? atUser = null,
string[]? atRole = null, string[]? mentionChannelId = null, MarkdownMessageAddition? addition = null,
string[]? atRole = null, string[]? mentionChannelId = null, MessageAddition? addition = null,
string? ackId = null) : base(
ackId ?? MessageAck.GetAckId(),
MessageType.MarkdownPing, channelId, roomId)
MessageType.MarkdownPing, channelId, roomId, replyMessageId, addition)
{
Message = message;
ReplyMessageId = replyMessageId;
AtUserId = string.Join(',', atUser ?? []);
AtRoleId = string.Join(',', atRole ?? []);
MentionChannelId = string.Join(',', mentionChannelId ?? []);
Addition = JsonSerializer.Serialize(addition ?? new MarkdownMessageAddition());
}

[JsonPropertyName("msg")] public string Message { get; init; }
[JsonPropertyName("reply_id")] public string ReplyMessageId { get; init; }
[JsonPropertyName("at_user_id")] public string AtUserId { get; init; }
[JsonPropertyName("at_role_id")] public string AtRoleId { get; init; }

[JsonPropertyName("mention_channel_id")]
public string MentionChannelId { get; init; }

[JsonPropertyName("addition")] public string Addition { get; init; }
}

public class MarkdownMessageAddition
{
public MarkdownMessageAddition(MarkdownMessageAdditionImage[]? additionImages = null)
{
ImageFilesInfo = additionImages ?? [];
}

[JsonPropertyName("img_files_info")] public MarkdownMessageAdditionImage[] ImageFilesInfo { get; init; }

public class MarkdownMessageAdditionImage
{
public MarkdownMessageAdditionImage(string uri, int width, int height) : this(new Uri(uri), width, height)
{
}

public MarkdownMessageAdditionImage(Uri uri, int width, int height)
{
Uri = uri;
Width = width;
Height = height;
}

[JsonPropertyName("uri")] public Uri Uri { get; init; }
[JsonPropertyName("width")] public int Width { get; init; }
[JsonPropertyName("height")] public int Height { get; init; }
}
}
44 changes: 42 additions & 2 deletions HeyBoxChatBotCs.Api/Features/Message/MessageBase.cs
Original file line number Diff line number Diff line change
@@ -1,20 +1,60 @@
using System.Text.Json.Serialization;
using System.Text.Encodings.Web;
using System.Text.Json;
using System.Text.Json.Serialization;
using HeyBoxChatBotCs.Api.Enums;

namespace HeyBoxChatBotCs.Api.Features.Message;

[JsonDerivedType(typeof(MarkdownMessage))]
[JsonDerivedType(typeof(ImageMessage))]
public abstract class MessageBase
{
protected MessageBase(string ackId, MessageType type, string channelId, string roomId)
protected MessageBase(string ackId, MessageType type, string channelId, string roomId, string replyMessageId = "",
MessageAddition? addition = null)
{
ReplyMessageId = replyMessageId;
AckId = ackId;
Type = type;
ChannelId = channelId;
RoomId = roomId;
Addition = JsonSerializer.Serialize(addition ?? new MessageAddition(), Bot.Bot.BotActionJsonSerializerOptions);
}


[JsonPropertyName("reply_id")] public string ReplyMessageId { get; init; }
[JsonPropertyName("heychat_ack_id")] public string AckId { get; init; }
[JsonPropertyName("msg_type")] public MessageType Type { get; init; }
[JsonPropertyName("channel_id")] public string ChannelId { get; init; }
[JsonPropertyName("room_id")] public string RoomId { get; init; }

[JsonPropertyName("addition")] public string Addition { get; init; }
}

public class MessageAddition
{
public MessageAddition(MessageAdditionImage[]? additionImages = null)
{
ImageFilesInfo = additionImages ?? [];
}

[JsonPropertyName("img_files_info")] public MessageAdditionImage[] ImageFilesInfo { get; init; }
}

public class MessageAdditionImage
{
public MessageAdditionImage(string uri, int width, int height) : this(new Uri(uri), width,
height)
{
}

public MessageAdditionImage(Uri uri, int width, int height)
{
Uri = uri;
Width = width;
Height = height;
}

[JsonPropertyName("uri")] public Uri Uri { get; init; }
[JsonPropertyName("width")] public int Width { get; init; }
[JsonPropertyName("height")] public int Height { get; init; }
}
Loading

0 comments on commit de91e79

Please sign in to comment.