You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何利用Telegram Bot检测已删除账号及批量发送前筛选失效ChatID

如何检测Telegram已删除账号并避免向其发送消息

嘿,刚好我之前用Mr.RoundRobin的Telegram.Bot库处理过类似的批量消息场景,检测已删除账号其实有两个实用的思路,我给你详细拆解下:

方法一:提前批量检测账号有效性

这种方式适合在发送消息前先过滤掉所有无效账号,避免后续发送时的无效请求。核心逻辑是调用Telegram Bot API的GetChatAsync接口——已删除的账号对应的Chat是不存在的,调用该接口会抛出特定异常。

以下是C#代码示例:

using Telegram.Bot;
using Telegram.Bot.Exceptions;
using System.Collections.Generic;
using System.Threading.Tasks;

var botToken = "你的Bot令牌";
var botClient = new TelegramBotClient(botToken);
// 替换成你的目标ChatID列表
var targetChatIds = new List<long> { 123456789, 987654321, ... };
var invalidChatIds = new List<long>();

foreach (var chatId in targetChatIds)
{
    try
    {
        // 尝试获取Chat信息
        var chat = await botClient.GetChatAsync(chatId);
        // 能获取到信息说明账号有效
        Console.WriteLine($"Chat {chatId} 状态正常");
    }
    catch (ApiRequestException ex)
    {
        // 根据错误码和消息判断是否为已删除账号
        if ((ex.ErrorCode == 400 && ex.Message.Contains("chat not found")) 
            || (ex.ErrorCode == 403 && ex.Message.Contains("user is deactivated")))
        {
            Console.WriteLine($"Chat {chatId} 已删除或无效,加入黑名单");
            invalidChatIds.Add(chatId);
        }
        else
        {
            // 处理其他异常(如Bot被屏蔽、网络问题等)
            Console.WriteLine($"检查Chat {chatId} 时出错: {ex.Message}");
        }
    }
    // 加延迟避免触发API速率限制
    await Task.Delay(50);
}

// 后续发送时跳过invalidChatIds中的ID

优缺点:

  • 优点:提前过滤无效账号,发送流程更顺畅,减少后续错误处理逻辑
  • 缺点:如果ChatID数量极大,会消耗额外的API请求,需要严格控制请求频率

方法二:发送时实时捕获无效账号

如果不想提前消耗大量API请求做检测,也可以在发送消息的同时捕获异常,实时标记无效账号。这种方式更节省前期资源,适合消息发送频率不高的场景。

代码示例:

using Telegram.Bot;
using Telegram.Bot.Exceptions;
using System.Collections.Generic;
using System.Threading.Tasks;

var botToken = "你的Bot令牌";
var botClient = new TelegramBotClient(botToken);
var targetChatIds = new List<long> { 123456789, 987654321, ... };
var messageContent = "你要发送的消息内容";
var invalidChatIds = new List<long>();

foreach (var chatId in targetChatIds)
{
    // 跳过已标记的无效账号
    if (invalidChatIds.Contains(chatId)) continue;

    try
    {
        await botClient.SendTextMessageAsync(chatId, messageContent);
        Console.WriteLine($"消息已成功发送到 {chatId}");
    }
    catch (ApiRequestException ex)
    {
        if ((ex.ErrorCode == 400 && ex.Message.Contains("chat not found")) 
            || (ex.ErrorCode == 403 && ex.Message.Contains("user is deactivated")))
        {
            Console.WriteLine($"Chat {chatId} 已删除,跳过");
            invalidChatIds.Add(chatId);
        }
        else
        {
            Console.WriteLine($"发送到 {chatId} 失败: {ex.Message}");
            // 可选:针对其他错误(如网络波动)添加重试逻辑
        }
    }
    await Task.Delay(50);
}

关键注意事项

  • API速率限制:Telegram Bot API限制每秒最多30次请求,批量处理时一定要添加适当延迟(如50-100ms),避免被临时限制访问。
  • 错误判断逻辑:优先以ErrorCode为准,再结合错误消息关键词——比如400对应"chat not found"(账号不存在),403对应"user is deactivated"(账号已注销),而"bot was blocked by the user"属于用户主动屏蔽,你可以根据需求决定是否标记为无效。
  • 缓存无效ID:把检测到的无效ChatID存储到本地文件或数据库,下次批量发送时直接跳过,避免重复检测浪费资源。

内容的提问来源于stack exchange,提问作者Saber Motamedi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 07:33:07