【发布时间】:2018-02-04 18:58:33
【问题描述】:
我想获取频道的会员人数,但不知道应该使用哪种方法?
我不是那个频道的管理员,我只是想得到计数。
编辑:我使用的是主电报 api,而不是电报 Bot api
【问题讨论】:
我想获取频道的会员人数,但不知道应该使用哪种方法?
我不是那个频道的管理员,我只是想得到计数。
编辑:我使用的是主电报 api,而不是电报 Bot api
【问题讨论】:
也可以通过 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 方法也是可行的。你可以 curl 或者使用 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>
【讨论】:
它对我有用:)
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))
【讨论】:
你可以使用getChatMembersCount方法。
使用此方法获取聊天中的成员数。
【讨论】: