首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何使用telegram api获取通道成员数

如何使用telegram api获取通道成员数
EN

Stack Overflow用户
提问于 2017-08-27 16:44:26
回答 2查看 9.9K关注 0票数 8

我想获得一个频道的成员计数,但我不知道我应该使用哪种方法?

我不是那个频道的管理员,我只是想得到计数。

编辑:我使用的是主要的电报api,而不是电报机器人api

EN

回答 2

Stack Overflow用户

发布于 2018-08-01 23:46:47

它对我很有效:)

代码语言:javascript
复制
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))
票数 5
EN

Stack Overflow用户

发布于 2021-06-24 20:32:35

也可以通过telethon中的GetFullChannelRequest做到这一点

代码语言:javascript
复制
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())

或者在不使用异步/等待的情况下写入

代码语言:javascript
复制
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()

同样如上所述,它也是可行的bot-api与

getChatMembersCount方法。您可以卷曲它或使用python查询所需的url。

用python编写的代码可以看起来像这样:

代码语言:javascript
复制
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'])

其中<your-bot-api-token>是BotFather提供的令牌,<channel-name>是您想要知道的订阅者数量的频道名称(当然,不包括“<>”的所有内容)

要首先检查,只需将其卷曲即可:

代码语言:javascript
复制
curl https://api.telegram.org/bot<your-bot-api-token>/getChatMembersCount?chat_id=@<channel-name>
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45903197

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档