how to get channel's members count with telegram api
Asked Answered
C

3

10

I want to get a channel's members' count but I don't know which method should I use?

I am not admin in that channel, I just want to get the count number.

EDIT:I am using main telegram api, not telegram Bot api

Connate answered 27/8, 2017 at 8:44 Comment(0)
G
10

You can use getChatMembersCount method.

Use this method to get the number of members in a chat.

Greenheart answered 27/8, 2017 at 9:10 Comment(0)
H
5

It worked for me :)

from telethon import TelegramClient, sync
from telethon.tl.functions.channels import GetFullChannelRequest


api_id = API ID
api_hash = 'API HASH'

client = TelegramClient('session_name', api_id, api_hash)
client.start()
if (client.is_user_authorized() == False):
    phone_number = 'PHONE NUMBER'
    client.send_code_request(phone_number)
    myself = client.sign_in(phone_number, input('Enter code: '))
channel = client.get_entity('CHANNEL LINK')

members = client.get_participants(channel)
print(len(members))
Hagfish answered 1/8, 2018 at 15:46 Comment(0)
A
3

It is possible to do it also through GetFullChannelRequest in telethon

async def main():
    async with client_to_manage as client:
        full_info = await client(GetFullChannelRequest(channel="moscowproc"))
        print(f"count: {full_info.full_chat.participants_count}")


if __name__ == '__main__':
    client_to_manage.loop.run_until_complete(main())

or to write it without async/await

def main():
    with client_to_manage as client:
        full_info = client.loop.run_until_complete(client(GetFullChannelRequest(channel="moscowproc")))
        print(f"count: {full_info.full_chat.participants_count}")


if __name__ == '__main__':
    main()

Also as above was said, it is also feasible by bot-api with
getChatMembersCount method. You can curl it or use python to query needed url

with python code can look like this one:

import json
from urllib.request import urlopen

url ="https://api.telegram.org/bot<your-bot-api-token>/getChatMembersCount?chat_id=@<channel-name>"
with urlopen(url) as f:
    resp = json.load(f)

print(resp['result'])

where <your-bot-api-token> is token provided by BotFather, and <channel-name> is channel name which amount of subscribers you want to know (of course, everything without "<>")

to check firstly, simply curl it:

curl https://api.telegram.org/bot<your-bot-api-token>/getChatMembersCount?chat_id=@<channel-name>
Avert answered 24/6, 2021 at 12:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.