简体   繁体   中英

How do I know if a telegram user joined my channel using telegram bot api?

Please take a look at this link & this .
Based on second link i did this :

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;
    }
}

But i have this error :

An exception of type 'System.AggregateException' occurred in mscorlib.dll but was not handled in user code

Additional information: One or more errors occurred.

What is the problem & how can i fix it?

What is channel name?

Did i do right about channel name?

GetChatMemberAsync() tells channel name is ChatId , How can i get ChatId of my channel?

What is the problem & how can i fix it?

Your bot should be add as administrator in the goal channel.
Goal channel should be public .

What is channel name?

The correct is : @Channel_Name

Did i do right about channel name?

Yes,

Here is the correct method :

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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM