how to get channel's members count with telegram api

10,542

Solution 1

You can use getChatMembersCount method.

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

Solution 2

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))

Solution 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>
Share:
10,542

Related videos on Youtube

Leo
Author by

Leo

Coding

Updated on June 04, 2022

Comments

  • Leo
    Leo almost 2 years

    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