From dad8a66c08cda83531ab6fe650ddbba2e4d04ec7 Mon Sep 17 00:00:00 2001 From: meoacgx Date: Fri, 19 Dec 2025 20:15:43 +0800 Subject: [PATCH] =?UTF-8?q?=E8=B4=A6=E5=8F=B7=E7=8A=B6=E6=80=81=E6=A3=80?= =?UTF-8?q?=E6=B5=8B=E5=A2=9E=E5=BC=BA=EF=BC=9A=E5=A2=9E=E5=8A=A0=E5=88=9B?= =?UTF-8?q?=E5=BB=BA=E9=A2=91=E9=81=93=E5=86=BB=E7=BB=93=E6=B7=B1=E5=BA=A6?= =?UTF-8?q?=E6=8E=A2=E6=B5=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Telegram/AccountTelegramToolsService.cs | 82 ++++++++++++++++++- .../Components/Pages/Accounts.razor | 9 +- 2 files changed, 89 insertions(+), 2 deletions(-) diff --git a/src/TelegramPanel.Core/Services/Telegram/AccountTelegramToolsService.cs b/src/TelegramPanel.Core/Services/Telegram/AccountTelegramToolsService.cs index 52ae355..427b561 100644 --- a/src/TelegramPanel.Core/Services/Telegram/AccountTelegramToolsService.cs +++ b/src/TelegramPanel.Core/Services/Telegram/AccountTelegramToolsService.cs @@ -32,7 +32,10 @@ public class AccountTelegramToolsService _logger = logger; } - public async Task RefreshAccountStatusAsync(int accountId) + /// + /// 刷新账号状态(可选深度探测:检测“创建频道接口是否被冻结”,会创建并删除一个测试频道) + /// + public async Task RefreshAccountStatusAsync(int accountId, bool probeCreateChannel = false) { var checkedAt = DateTime.UtcNow; try @@ -77,6 +80,38 @@ public class AccountTelegramToolsService else if (profile.IsRestricted) summary = "账号受限(Restricted)"; + if (probeCreateChannel) + { + var probe = await ProbeCreateChannelCapabilityAsync(client, accountId); + if (probe.IsFrozen) + { + return new TelegramAccountStatusResult( + Ok: false, + Summary: "创建频道受限(冻结)", + Details: $"创建频道探测:{probe.Message}{Environment.NewLine}{BuildProfileDetails(profile)}", + CheckedAtUtc: checkedAt, + Profile: profile); + } + + if (!probe.Success) + { + return new TelegramAccountStatusResult( + Ok: false, + Summary: "创建频道探测失败", + Details: $"创建频道探测:{probe.Message}{Environment.NewLine}{BuildProfileDetails(profile)}", + CheckedAtUtc: checkedAt, + Profile: profile); + } + + // 探测成功,不影响原状态,仅补充详情 + return new TelegramAccountStatusResult( + Ok: true, + Summary: summary, + Details: $"创建频道探测:可用(已自动清理测试频道){Environment.NewLine}{BuildProfileDetails(profile)}", + CheckedAtUtc: checkedAt, + Profile: profile); + } + return new TelegramAccountStatusResult( Ok: true, Summary: summary, @@ -311,6 +346,51 @@ public class AccountTelegramToolsService return $"昵称:{profile.DisplayName};用户名:{profile.Username ?? "-"};UserId:{profile.UserId};标记:{flagText}"; } + private async Task ProbeCreateChannelCapabilityAsync(Client client, int accountId) + { + // 注意:这是“深度探测”,会创建并删除一个测试频道。 + var title = $"tp-check-{DateTime.UtcNow:MMddHHmmss}"; + const string about = "Telegram Panel create-channel probe (auto delete)"; + + try + { + UpdatesBase updates; + try + { + updates = await client.Channels_CreateChannel(title: title, about: about, broadcast: true); + } + catch (RpcException ex) when (ex.Code == 420 && string.Equals(ex.Message, "FROZEN_METHOD_INVALID", StringComparison.OrdinalIgnoreCase)) + { + return new CreateChannelProbeResult(false, true, "Telegram 返回 FROZEN_METHOD_INVALID(创建频道接口被冻结)"); + } + + var channel = updates.Chats.Values.OfType().FirstOrDefault(); + if (channel == null) + return new CreateChannelProbeResult(false, false, "创建测试频道失败:未返回 Channel"); + + try + { + // 立即删除,避免留下垃圾频道 + var input = new InputChannel(channel.id, channel.access_hash); + await client.Invoke(new TL.Channels_DeleteChannel { channel = input }); + } + catch (Exception ex) + { + _logger.LogWarning(ex, "Probe channel created but failed to delete (account {AccountId}, channel {ChannelId})", accountId, channel.id); + return new CreateChannelProbeResult(false, false, $"创建测试频道成功,但删除失败:{ex.Message}(请手动删除频道:{title})"); + } + + return new CreateChannelProbeResult(true, false, "可用"); + } + catch (Exception ex) + { + var msg = ex.Message ?? "未知错误"; + return new CreateChannelProbeResult(false, false, msg); + } + } + + private sealed record CreateChannelProbeResult(bool Success, bool IsFrozen, string Message); + private static (string summary, string details) MapTelegramException(Exception ex) { var msg = ex.Message ?? string.Empty; diff --git a/src/TelegramPanel.Web/Components/Pages/Accounts.razor b/src/TelegramPanel.Web/Components/Pages/Accounts.razor index a7caba1..57299eb 100644 --- a/src/TelegramPanel.Web/Components/Pages/Accounts.razor +++ b/src/TelegramPanel.Web/Components/Pages/Accounts.razor @@ -217,7 +217,14 @@ try { loading = true; - var result = await AccountTelegramTools.RefreshAccountStatusAsync(account.Id); + bool? probeResult = await DialogService.ShowMessageBox( + "刷新 Telegram 状态", + "是否进行深度探测(将创建并删除一个测试频道,用于判断“创建频道接口是否被冻结”)?", + yesText: "深度探测", + cancelText: "普通刷新"); + + var probe = probeResult == true; + var result = await AccountTelegramTools.RefreshAccountStatusAsync(account.Id, probeCreateChannel: probe); telegramStatus[account.Id] = result; // 资料刷新后重新加载(昵称/用户名/用户ID 可能更新)