Skip to content
This repository has been archived by the owner on Dec 5, 2021. It is now read-only.

how to get all members in channel? #461

Closed
elmagekmosaad opened this issue Apr 25, 2017 · 12 comments
Closed

how to get all members in channel? #461

elmagekmosaad opened this issue Apr 25, 2017 · 12 comments

Comments

@elmagekmosaad
Copy link

how to get all members in channel?

@nazmotka
Copy link

You need a channel id and hash to get things done, here is a code sample:
var request = new TLRequestGetParticipants { offset = offset, limit = limit, channel = new TLInputChannel { access_hash = channelHash, channel_id = channelId }, filter = new TLChannelParticipantsRecent() }; TLChannelParticipants found = await client.SendRequestAsync<TLChannelParticipants>(request);

@elmagekmosaad
Copy link
Author

can u tell me whene i can get chanal hash?
and
can u give me code vb.net not #c ?

@nazmotka
Copy link

nazmotka commented May 3, 2017

Sorry, I haven't written any vb.net code, but there must be online tools for code conversion. Here is simplified c# code you can use to get the hash:

TLFound found = await client.SearchUserAsync(channelName, 1);
 var channel = found.chats.lists.OfType<TLChannel>().FirstOrDefault();
if (channel != null)
{
   //channel object will contain 'id' and 'access_hash' properties you need
}

@elmagekmosaad
Copy link
Author

ok thanks

@parabola949
Copy link

parabola949 commented May 12, 2017

You'll also need to use offsets to get all the members, if a large group. Here's a snippet from one of my projects, though I like how @an2114 is getting the hash better, I'll probably use that :)
(note - I used the string groupname for a reason, I am well aware of how to use ids. I'd recommend using the id)

public static async Task<ChannelInfo> GetChatInfo(string groupName)
        {
            if (! await AuthUser()) return null;
            var result = new ChannelInfo();
            var dialogs = (TLDialogs)await client.GetUserDialogsAsync();
            var main = dialogs.chats.lists.Where(c => c.GetType() == typeof(TLChannel))
                        .Cast<TLChannel>()
                        .FirstOrDefault(c => c.title == (groupName));
            var req = new TLRequestGetFullChannel()
            {
                channel = new TLInputChannel() { access_hash = main.access_hash.Value, channel_id = main.id }
            };
             
            var res = await client.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.full_chat as TLChannelFull).participants_count)
            {
                var pReq = new TLRequestGetParticipants()
                {
                    channel = new TLInputChannel() { access_hash = main.access_hash.Value, channel_id = main.id },
                    filter = new TLChannelParticipantsRecent() { },
                    limit = 200,
                    offset = offset
                };
                var pRes = await client.SendRequestAsync<TLChannelParticipants>(pReq);
                result.Users.AddRange(pRes.users.lists.Cast<TLUser>());
                offset += 200;
                await Task.Delay(500);
            }

            return result;
        }

Channelnfo() was just a quick class I made, modify as needed of course

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

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

@hosseinGanjyar
Copy link

I've error: "CHAT_ADMIN_REQUIRED"

@nazmotka
Copy link

That happens because of a security configuration of a channel that means a user is not allowed to view the members. You can see the same behavior in different Telegram clients (e.g. a desktop or web app).

@hosseinGanjyar
Copy link

hosseinGanjyar commented Aug 22, 2017

Many times TLRequestGetParticipants return 1 more record. That's mean if I want to get 200 of 4200 contacts, many times get 201 contacts in result....!!!
@knocte ,This is bug of TELESHARP

@knocte
Copy link
Collaborator

knocte commented Aug 22, 2017

@hosseinGanjyar good catch! please propose a pull-request to fix it

@ghost
Copy link

ghost commented Mar 8, 2018

also where can i put these codes?

@knocte
Copy link
Collaborator

knocte commented Mar 9, 2018

We don't use github issues as a forum but to report bugs, so I'm closing this. If you want to have a more forum-like experience, you can ask questions in our gitter and telegram channels.

@knocte knocte closed this as completed Mar 9, 2018
@evo11x
Copy link

evo11x commented Aug 3, 2018

It is blocking in this line forever
var dialogs = (TLDialogs)await client.GetUserDialogsAsync();
and no error

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants