我想获得一个频道的成员计数,但我不知道我应该使用哪种方法?
我不是那个频道的管理员,我只是想得到计数。
编辑:我使用的是主要的电报api,而不是电报机器人api
发布于 2018-08-01 23:46:47
它对我很有效:)
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))
发布于 2021-06-24 20:32:35
也可以通过telethon中的GetFullChannelRequest做到这一点
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())
或者在不使用异步/等待的情况下写入
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编写的代码可以看起来像这样:
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>
是您想要知道的订阅者数量的频道名称(当然,不包括“<>”的所有内容)
要首先检查,只需将其卷曲即可:
curl https://api.telegram.org/bot<your-bot-api-token>/getChatMembersCount?chat_id=@<channel-name>
https://stackoverflow.com/questions/45903197
复制相似问题