From fc5fdb9913e673585cfeed6f1bfe29e50166e42e Mon Sep 17 00:00:00 2001 From: chr_ Date: Tue, 21 Feb 2023 17:01:23 +0800 Subject: [PATCH] bugfix --- Directory.Build.props | 2 +- XinjingdailyBot.Command/AdminCommand.cs | 8 +- XinjingdailyBot.Command/NormalCommand.cs | 27 +++-- XinjingdailyBot.Command/SuperCommand.cs | 2 +- .../Extensions/ChatExtension.cs | 10 +- .../Helper/IMarkupHelperService.cs | 2 +- .../Bot/Common/DispatcherService.cs | 2 +- .../Bot/Common/PollingService.cs | 2 +- .../Bot/Handler/CommandHandler.cs | 4 +- .../Helper/MarkupHelperService.cs | 18 ++-- XinjingdailyBot.Tasks/ExpiredPostsTask.cs | 2 +- XinjingdailyBot.Tasks/PostAdvertiseTask.cs | 2 +- XinjingdailyBot.WebAPI/nlog.config | 100 ++++++++---------- 13 files changed, 93 insertions(+), 88 deletions(-) diff --git a/Directory.Build.props b/Directory.Build.props index 20437d73..90dba28d 100644 --- a/Directory.Build.props +++ b/Directory.Build.props @@ -1,6 +1,6 @@ - 2.0.18.2 + 2.0.19.0 diff --git a/XinjingdailyBot.Command/AdminCommand.cs b/XinjingdailyBot.Command/AdminCommand.cs index 71a9410e..b5714060 100644 --- a/XinjingdailyBot.Command/AdminCommand.cs +++ b/XinjingdailyBot.Command/AdminCommand.cs @@ -224,7 +224,7 @@ async Task exec() } catch (Exception ex) { - _logger.LogError("发送私聊消息失败 {Message}", ex.Message); + _logger.LogError(ex, "发送私聊消息失败"); } StringBuilder sb = new(); @@ -319,7 +319,7 @@ async Task exec() } catch (Exception ex) { - _logger.LogError("发送私聊消息失败 {Message}", ex.Message); + _logger.LogError(ex, "发送私聊消息失败"); } StringBuilder sb = new(); @@ -462,7 +462,7 @@ async Task exec() } catch (Exception ex) { - _logger.LogError("发送私聊消息失败 {Message}", ex.Message); + _logger.LogError(ex, "发送私聊消息失败"); } return sb.ToString(); @@ -1066,7 +1066,7 @@ async Task exec() } catch (Exception ex) { - _logger.LogError("向用户 {targetUser} 发送消息失败, {ex}", targetUser, ex); + _logger.LogError(ex, "向用户 {targetUser} 发送消息失败", targetUser); } } diff --git a/XinjingdailyBot.Command/NormalCommand.cs b/XinjingdailyBot.Command/NormalCommand.cs index 3ed143c3..ea39d897 100644 --- a/XinjingdailyBot.Command/NormalCommand.cs +++ b/XinjingdailyBot.Command/NormalCommand.cs @@ -343,7 +343,7 @@ public async Task QGetRandomPost(Users dbUser, CallbackQuery callbackQuery, stri bool hasSpoiler = randomPost.HasSpoiler; Chat? chat = callbackQuery.Message!.Chat; - if (randomPost.IsMediaGroup && false) + if (randomPost.IsMediaGroup) { var attachments = await _attachmentService.Queryable().Where(x => x.PostID == randomPost.Id).ToListAsync(); var group = new IAlbumInputMedia[attachments.Count]; @@ -366,7 +366,6 @@ public async Task QGetRandomPost(Users dbUser, CallbackQuery callbackQuery, stri } var messages = await _botClient.SendMediaGroupAsync(chat, group); - await _botClient.SendTextMessageAsync(chat, "随机稿件操作", replyMarkup: keyboard); } else @@ -374,7 +373,7 @@ public async Task QGetRandomPost(Users dbUser, CallbackQuery callbackQuery, stri Attachments attachment = await _attachmentService.Queryable().FirstAsync(x => x.PostID == randomPost.Id); var handler = randomPost.PostType switch { - MessageType.Text => _botClient.SendTextMessageAsync(chat, randomPost.Text, replyMarkup: keyboard), + MessageType.Text => _botClient.SendTextMessageAsync(chat, randomPost.Text), MessageType.Photo => _botClient.SendPhotoAsync(chat, new InputFileId(attachment.FileID), caption: randomPost.Text, parseMode: ParseMode.Html, replyMarkup: keyboard, hasSpoiler: hasSpoiler), MessageType.Audio => _botClient.SendAudioAsync(chat, new InputFileId(attachment.FileID), caption: randomPost.Text, parseMode: ParseMode.Html, replyMarkup: keyboard, title: attachment.FileName), MessageType.Video => _botClient.SendVideoAsync(chat, new InputFileId(attachment.FileID), caption: randomPost.Text, parseMode: ParseMode.Html, replyMarkup: keyboard, hasSpoiler: hasSpoiler), @@ -386,17 +385,31 @@ public async Task QGetRandomPost(Users dbUser, CallbackQuery callbackQuery, stri if (handler == null) { - await _botClient.AutoReplyAsync($"不支持的稿件类型: {randomPost.PostType}", callbackQuery); + await _botClient.AutoReplyAsync($"不支持的稿件类型: {randomPost.PostType}", callbackQuery, true); await _botClient.EditMessageTextAsync(callbackQuery.Message!, $"不支持的稿件类型: {randomPost.PostType}", null); return; } - var message = await handler; + try + { + var message = await handler; + } + catch (Exception ex) + { + int i = 0; + } } //去除第一条消息的按钮 - var kbd = args.Length > 3 ? _markupHelperService.LinkToOriginPostKeyboard(args[3]) : null; - await _botClient.EditMessageReplyMarkupAsync(callbackQuery.Message!, kbd); + if (args.Length > 3 && long.TryParse(args[3], out long msgId)) + { + var kbd = _markupHelperService.LinkToOriginPostKeyboard(msgId); + await _botClient.EditMessageReplyMarkupAsync(callbackQuery.Message!, kbd); + } + else + { + await _botClient.EditMessageReplyMarkupAsync(callbackQuery.Message!, null); + } } else { diff --git a/XinjingdailyBot.Command/SuperCommand.cs b/XinjingdailyBot.Command/SuperCommand.cs index e1f2d9d8..05f15ce6 100644 --- a/XinjingdailyBot.Command/SuperCommand.cs +++ b/XinjingdailyBot.Command/SuperCommand.cs @@ -75,7 +75,7 @@ public async Task ResponseRestart(Message message) catch (Exception ex) { await _botClient.SendCommandReply("启动进程遇到错误", message); - _logger.LogError("启动进程遇到错误", ex); + _logger.LogError(ex, "启动进程遇到错误"); } } diff --git a/XinjingdailyBot.Infrastructure/Extensions/ChatExtension.cs b/XinjingdailyBot.Infrastructure/Extensions/ChatExtension.cs index 136b628b..1c29da46 100644 --- a/XinjingdailyBot.Infrastructure/Extensions/ChatExtension.cs +++ b/XinjingdailyBot.Infrastructure/Extensions/ChatExtension.cs @@ -1,4 +1,5 @@ -using Telegram.Bot.Types; +using System.Threading.Channels; +using Telegram.Bot.Types; namespace XinjingdailyBot.Infrastructure.Extensions { @@ -33,5 +34,12 @@ public static string EscapedChatName(this Chat chat) { return chat.Title?.EscapeHtml() ?? ""; } + + public static string GetMessageLink (this Chat chat,long messageId) + { + string link = !string.IsNullOrEmpty(chat.Username) ? $"https://t.me/{chat.Username}/{messageId}" : $"https://t.me/c/{chat.Id}/{messageId}"; + return link; + + } } } diff --git a/XinjingdailyBot.Interface/Helper/IMarkupHelperService.cs b/XinjingdailyBot.Interface/Helper/IMarkupHelperService.cs index 3446ad22..a2c88e71 100644 --- a/XinjingdailyBot.Interface/Helper/IMarkupHelperService.cs +++ b/XinjingdailyBot.Interface/Helper/IMarkupHelperService.cs @@ -7,7 +7,7 @@ public interface IMarkupHelperService { InlineKeyboardMarkup DirectPostKeyboard(bool anymouse, int tagNum, bool? hasSpoiler); InlineKeyboardMarkup? LinkToOriginPostKeyboard(Posts post); - InlineKeyboardMarkup? LinkToOriginPostKeyboard(string link); + InlineKeyboardMarkup? LinkToOriginPostKeyboard(long messageId); InlineKeyboardMarkup PostKeyboard(bool anymouse); InlineKeyboardMarkup RandomPostMenuKeyboard(Users dbUser); InlineKeyboardMarkup RandomPostMenuKeyboard(Users dbUser, int tagNum); diff --git a/XinjingdailyBot.Service/Bot/Common/DispatcherService.cs b/XinjingdailyBot.Service/Bot/Common/DispatcherService.cs index c4448dec..02a6b9f8 100644 --- a/XinjingdailyBot.Service/Bot/Common/DispatcherService.cs +++ b/XinjingdailyBot.Service/Bot/Common/DispatcherService.cs @@ -71,7 +71,7 @@ private async Task UnPinMessage(Message message) } catch (Exception ex) { - _logger.LogError("取消置顶出错", ex); + _logger.LogError(ex, "取消置顶出错"); } } diff --git a/XinjingdailyBot.Service/Bot/Common/PollingService.cs b/XinjingdailyBot.Service/Bot/Common/PollingService.cs index 2f3fb10a..3c611ccf 100644 --- a/XinjingdailyBot.Service/Bot/Common/PollingService.cs +++ b/XinjingdailyBot.Service/Bot/Common/PollingService.cs @@ -71,7 +71,7 @@ private async Task DoWork(CancellationToken stoppingToken) catch (Exception ex) { - _logger.LogError("接收服务运行出错: {Exception}", ex); + _logger.LogError(ex,"接收服务运行出错"); await Task.Delay(TimeSpan.FromSeconds(5), stoppingToken); } } diff --git a/XinjingdailyBot.Service/Bot/Handler/CommandHandler.cs b/XinjingdailyBot.Service/Bot/Handler/CommandHandler.cs index 9c72f7db..f8b520e0 100644 --- a/XinjingdailyBot.Service/Bot/Handler/CommandHandler.cs +++ b/XinjingdailyBot.Service/Bot/Handler/CommandHandler.cs @@ -225,7 +225,7 @@ public async Task OnCommandReceived(Users dbUser, Message message) catch (Exception ex) { errorMsg = $"{ex.GetType} {ex.Message}"; - + _logger.LogError(ex, "命令 {cmd} 执行出错", cmd); await _botClient.SendCommandReply(_optionsSetting.Debug ? errorMsg : "遇到内部错误", message); } handled = true; @@ -356,7 +356,7 @@ public async Task OnQueryCommandReceived(Users dbUser, CallbackQuery query) catch (Exception ex) //无法捕获 TODO { errorMsg = $"{ex.GetType} {ex.Message}"; - + _logger.LogError(ex, "回调命令 {cmd} 执行出错", cmd); await _botClient.AutoReplyAsync(_optionsSetting.Debug ? errorMsg : "遇到内部错误", query, true); } handled = true; diff --git a/XinjingdailyBot.Service/Helper/MarkupHelperService.cs b/XinjingdailyBot.Service/Helper/MarkupHelperService.cs index 828a6632..ae8eae9c 100644 --- a/XinjingdailyBot.Service/Helper/MarkupHelperService.cs +++ b/XinjingdailyBot.Service/Helper/MarkupHelperService.cs @@ -1,5 +1,6 @@ using Telegram.Bot.Types.ReplyMarkups; using XinjingdailyBot.Infrastructure.Attribute; +using XinjingdailyBot.Infrastructure.Extensions; using XinjingdailyBot.Infrastructure.Localization; using XinjingdailyBot.Interface.Bot.Common; using XinjingdailyBot.Interface.Helper; @@ -316,18 +317,13 @@ public InlineKeyboardMarkup ReviewKeyboardB() public InlineKeyboardMarkup? LinkToOriginPostKeyboard(Posts post) { var channel = _channelService.AcceptChannel; - string? username = channel.Username; - - if (string.IsNullOrEmpty(username)) - { - return null; - } + string link = channel.GetMessageLink(post.PublicMsgID); InlineKeyboardMarkup keyboard = new(new[] { new [] { - InlineKeyboardButton.WithUrl($"在{channel.Title}中查看", $"https://t.me/{username}/{post.PublicMsgID}"), + InlineKeyboardButton.WithUrl($"在{channel.Title}中查看", link), }, }); return keyboard; @@ -338,9 +334,11 @@ public InlineKeyboardMarkup ReviewKeyboardB() /// /// /// - public InlineKeyboardMarkup? LinkToOriginPostKeyboard(string link) + public InlineKeyboardMarkup? LinkToOriginPostKeyboard(long messageId) { var channel = _channelService.AcceptChannel; + string link = channel.GetMessageLink(messageId); + InlineKeyboardMarkup keyboard = new(new[] { new [] @@ -434,14 +432,14 @@ public InlineKeyboardMarkup RandomPostMenuKeyboard(Users dbUser, int tagNum) public InlineKeyboardMarkup RandomPostMenuKeyboard(Users dbUser, Posts post, int tagId, string postType) { var channel = _channelService.AcceptChannel; - string link = !string.IsNullOrEmpty(channel.Username) ? $"https://t.me/{channel.Username}/{post.PublicMsgID}" : $"https://t.me/c/{channel.Id}/{post.PublicMsgID}"; + string link = channel.GetMessageLink(post.PublicMsgID); InlineKeyboardMarkup keyboard = new(new[] { new [] { InlineKeyboardButton.WithUrl($"在{channel.Title}中查看", link), - InlineKeyboardButton.WithCallbackData("再来一张",$"cmd {dbUser.UserID} randompost {tagId} {postType} {link}"), + InlineKeyboardButton.WithCallbackData("再来一张",$"cmd {dbUser.UserID} randompost {tagId} {postType} {post.PublicMsgID}"), }, }); diff --git a/XinjingdailyBot.Tasks/ExpiredPostsTask.cs b/XinjingdailyBot.Tasks/ExpiredPostsTask.cs index d0f4a873..0823dd54 100644 --- a/XinjingdailyBot.Tasks/ExpiredPostsTask.cs +++ b/XinjingdailyBot.Tasks/ExpiredPostsTask.cs @@ -124,7 +124,7 @@ public async Task Execute(IJobExecutionContext context) } catch (Exception ex) { - _logger.LogError("通知消息发送失败, 自动禁用更新 {error}", ex); + _logger.LogError(ex, "通知消息发送失败, 自动禁用更新"); user.PrivateChatID = -1; await Task.Delay(5000); } diff --git a/XinjingdailyBot.Tasks/PostAdvertiseTask.cs b/XinjingdailyBot.Tasks/PostAdvertiseTask.cs index 8fbdbf5d..e7a3ee1c 100644 --- a/XinjingdailyBot.Tasks/PostAdvertiseTask.cs +++ b/XinjingdailyBot.Tasks/PostAdvertiseTask.cs @@ -75,7 +75,7 @@ public async Task Execute(IJobExecutionContext context) } catch (Exception ex) { - _logger.LogError("投放广告出错: {error}", ex.Message); + _logger.LogError(ex, "投放广告出错"); } finally { diff --git a/XinjingdailyBot.WebAPI/nlog.config b/XinjingdailyBot.WebAPI/nlog.config index 74ef5544..0c2b56df 100644 --- a/XinjingdailyBot.WebAPI/nlog.config +++ b/XinjingdailyBot.WebAPI/nlog.config @@ -5,65 +5,51 @@ internalLogLevel="Warn" internalLogFile="${basedir}/logs/nlog-internal.txt"> - - - - + + + + - + - - - - - - - - + + + + - - - + + - - - - + + - - + + - - - + + + - - - + + + - + - - - - + + + - - - - + + + + + \ No newline at end of file