我怎么知道电报用户是否使用电报bot API加入了我的频道?

时间:2019-03-15 13:40:38

标签: c# telegram-bot

请查看this linkthis
基于第二个链接,我做到了:

public static class Program {
    private static readonly TelegramBotClient Bot = new TelegramBotClient("My Token");

    public static void Main(string[] args) {

        var me = Bot.GetMeAsync().Result;
        Console.Title = me.Username;

        Bot.OnMessage += BotOnMessageReceived;
        Bot.OnMessageEdited += BotOnMessageReceived;
        Bot.OnCallbackQuery += BotOnCallbackQueryReceived;
        Bot.OnInlineQuery += BotOnInlineQueryReceived;
        Bot.OnInlineResultChosen += BotOnChosenInlineResultReceived;
        Bot.OnReceiveError += BotOnReceiveError;

        Bot.StartReceiving(Array.Empty < UpdateType > ());
        Console.WriteLine($ "Start listening for @{me.Username}");
        Console.ReadLine();
        Bot.StopReceiving();
    }

    private static async void BotOnMessageReceived(object sender, MessageEventArgs messageEventArgs) {
        var message = messageEventArgs.Message;

        if (message == null || message.Type != MessageType.Text) return;

        bool is_member_of_channel = Is_Member_Of_Channel("@Channel_Name", message.From.Id);
    }

    private static bool Is_Member_Of_Channel(string channel_name, int user_id) {
        var t = Bot.GetChatMemberAsync(channel_name, user_id);
        if (t.Result.Status.ToString().Length > 25) return false;
        return true;
    }
}

但是我有这个错误:

  

类型的异常   mscorlib.dll中发生“ System.AggregateException”,但未发生   用用户代码处理

     

其他信息:发生一个或多个错误。

有什么问题,我该如何解决?

什么是频道名称?

我对频道名称正确吗?

GetChatMemberAsync()告诉频道名称为ChatId,如何获得频道的ChatId

1 个答案:

答案 0 :(得分:0)

有什么问题,我该如何解决?

  

您的漫游器应在目标渠道中添加为administrator
     目标   频道应为public

什么是频道名称?

  

正确的是:@Channel_Name

我对频道名称正确吗?

  

是,

这是正确的方法:

private static bool Is_Member_Of_Channel(string channel_name, int user_id)
{
    //Status Values
    //Creator
    //Member
    //Left
    var t = Bot.GetChatMemberAsync(channel_name, user_id);
    if (t.Result.Status.ToString() == "Left")
        return false;
    return true;
}