From 68d41ad4377ce4673e2d2fa2b19aafcdd90ab2a4 Mon Sep 17 00:00:00 2001 From: meoacgx Date: Sat, 20 Dec 2025 23:21:55 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20Bot=20=E7=BC=96=E8=BE=91=E9=A2=91?= =?UTF-8?q?=E9=81=93=E5=BF=BD=E7=95=A5=E6=9C=AA=E4=BF=AE=E6=94=B9=E9=94=99?= =?UTF-8?q?=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Services/Telegram/BotTelegramService.cs | 46 +++++++++++++++---- 1 file changed, 38 insertions(+), 8 deletions(-) diff --git a/src/TelegramPanel.Core/Services/Telegram/BotTelegramService.cs b/src/TelegramPanel.Core/Services/Telegram/BotTelegramService.cs index c526a43..f93e731 100644 --- a/src/TelegramPanel.Core/Services/Telegram/BotTelegramService.cs +++ b/src/TelegramPanel.Core/Services/Telegram/BotTelegramService.cs @@ -282,17 +282,31 @@ public class BotTelegramService if (string.IsNullOrWhiteSpace(title)) throw new ArgumentException("频道标题不能为空", nameof(title)); - await _api.CallAsync(bot.Token, "setChatTitle", new Dictionary + try { - ["chat_id"] = channelTelegramId.ToString(), - ["title"] = title - }, cancellationToken); + await _api.CallAsync(bot.Token, "setChatTitle", new Dictionary + { + ["chat_id"] = channelTelegramId.ToString(), + ["title"] = title + }, cancellationToken); + } + catch (Exception ex) when (IsBotApiNotModified(ex, "setChatTitle")) + { + // ignore: title not modified + } - await _api.CallAsync(bot.Token, "setChatDescription", new Dictionary + try { - ["chat_id"] = channelTelegramId.ToString(), - ["description"] = about ?? "" - }, cancellationToken); + await _api.CallAsync(bot.Token, "setChatDescription", new Dictionary + { + ["chat_id"] = channelTelegramId.ToString(), + ["description"] = about ?? "" + }, cancellationToken); + } + catch (Exception ex) when (IsBotApiNotModified(ex, "setChatDescription")) + { + // ignore: description not modified + } return true; } @@ -361,6 +375,22 @@ public class BotTelegramService return true; } + private static bool IsBotApiNotModified(Exception ex, string method) + { + // Telegram Bot API 会在“内容未修改”时返回 400: + // - setChatTitle: "Bad Request: chat title is not modified" + // - setChatDescription: "Bad Request: chat description is not modified" + // 这类情况不应该阻塞后续操作(例如继续设置头像)。 + if (ex is not InvalidOperationException) + return false; + + var msg = ex.Message ?? string.Empty; + if (!msg.Contains($"Bot API 调用失败:{method} (400)", StringComparison.OrdinalIgnoreCase)) + return false; + + return msg.Contains("is not modified", StringComparison.OrdinalIgnoreCase); + } + public async Task PromoteChatMemberAsync(int botId, IReadOnlyList channelTelegramIds, long userId, BotAdminRights rights, CancellationToken cancellationToken) { var bot = await _botManagement.GetBotAsync(botId)