sochix / TLSharp

Telegram client library implemented in C#
1.01k stars 380 forks source link

How to get the chat participants? #912

Closed maric010 closed 4 years ago

maric010 commented 4 years ago

not a channel but a chat (group)

CheshireCaat commented 4 years ago

Please check other issues (and closed ones too) before asking :)

First of all, take a look at https://github.com/sochix/TLSharp/issues/461 You need to use TLRequestGetParticipants, but keep in mind that you need to get information about the chat or channel before executing it.

I can also note that this request can return only 10 thousand participants. If you want more, you should use https://core.telegram.org/type/ChannelParticipantsFilter with TLRequestGetParticipants.

See more here: https://github.com/LonamiWebs/Telethon/issues/580

maric010 commented 4 years ago

I tried. ` var foundc = await newsession.SearchUserAsync(textBox3.Text, 1); var hannel = foundc.Chats.Where(c => c.GetType() == typeof(TLChannel)) .Cast() .FirstOrDefault(c => c.Username == (textBox3.Text));

        var request = new TLRequestGetParticipants 
        { Offset = 0, Limit = 50, Channel = new TLInputChannel { AccessHash = hannel.AccessHash.Value, ChannelId = hannel.Id }, Filter = new TLChannelParticipantsRecent() }; 
        TLChannelParticipants found = await newsession.SendRequestAsync<TLChannelParticipants>(request);

        listBox1.Items.Add(found.Participants[0]);
        listBox1.Items.Add(found.Participants[1]);
        listBox1.Items.Add(found.Participants[2]);

` and got it загружено (2)

CheshireCaat commented 4 years ago

@maric010 The answer is in your question. You add a custom class object to the ListBox (TLChannelParticipant). What do you think the ListBox should show? It's not a TLSharp-specific question, check out the ListBox docs and examples. If you want to add only usernames, you need something like found.Participants[0].Username or ValueMember/DataMember in the ListBox properties.

maric010 commented 4 years ago

`public class ChannelInfo { public TLChannel Channel { get; set; } public TeleSharp.TL.Messages.TLChatFull ChatFull { get; set; } public List Users { get; set; } = new List();

        private DateTime _dateCreated;
        public DateTime DateCreated
        {
            get { return _dateCreated; }
            set
            {
                _dateCreated = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc).AddSeconds(Channel.Date).ToLocalTime();
            }
        }
    }

    public async Task<ChannelInfo> GetChatInfo(string groupName)
    {
        TelegramClient newsession = new TelegramClient(864879, "41b73a8c3b79529aa53e5ad889d8749f", null, "session" + sessions);
        await newsession.ConnectAsync();
        //if (!await AuthUser()) return null;
        var result = new ChannelInfo();
        var dialogs = (TLDialogs)await newsession.GetUserDialogsAsync();
        var main = dialogs.Chats.Where(c => c.GetType() == typeof(TLChannel))
                    .Cast<TLChannel>()
                    .FirstOrDefault(c => c.Title == (groupName));
        var req = new TLRequestGetFullChannel()
        {
            Channel = new TLInputChannel() { AccessHash = main.AccessHash.Value, ChannelId = main.Id }
        };

        var res = await newsession.SendRequestAsync<TeleSharp.TL.Messages.TLChatFull>(req);

        //we have to do this in slices
        var offset = 0;
        result.Channel = main;
        result.ChatFull = res;
        while (offset < (res.FullChat as TLChannelFull).ParticipantsCount)
        {
            var pReq = new TLRequestGetParticipants()
            {
                Channel = new TLInputChannel() { AccessHash = main.AccessHash.Value, ChannelId = main.Id },
                Filter = new TLChannelParticipantsRecent() { },
                Limit = 200,
                Offset = offset
            };
            var pRes = await newsession.SendRequestAsync<TLChannelParticipants>(pReq);
            result.Users.AddRange(pRes.Users.Cast<TLUser>());
            offset += 200;
            //await Task.Delay(500);
        }

        return result;
    }

    private async void button1_Click(object sender, EventArgs e)
    {
        TelegramClient newsession = new TelegramClient(864879, "41b73a8c3b79529aa53e5ad889d8749f", null, "session" + sessions);
        await newsession.ConnectAsync();
        ChannelInfo testbek = await GetChatInfo(textBox3.Text);
        richTextBox1.Text = Convert.ToString(testbek.Users[1].Username);
    }`

it's working in vs 2019 thanks big

kratossz commented 4 years ago

@maric010 are you fixed this problem?