【问题标题】:how to get channel's members count with telegram api如何使用电报 api 获取频道的成员数
【发布时间】:2018-02-04 18:58:33
【问题描述】:

我想获取频道的会员人数,但不知道应该使用哪种方法?

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

编辑:我使用的是主电报 api,而不是电报 Bot api

【问题讨论】:

    标签: api telegram


    【解决方案1】:

    也可以通过 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'])
    

    其中&lt;your-bot-api-token&gt; 是BotFather 提供的令牌,&lt;channel-name&gt; 是频道名称,您想知道多少订阅者(当然,没有“”的所有内容)

    首先检查,只需卷曲它:

    curl https://api.telegram.org/bot<your-bot-api-token>/getChatMembersCount?chat_id=@<channel-name>
    

    【讨论】:

      【解决方案2】:

      它对我有用:)

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

      【讨论】:

        【解决方案3】:

        你可以使用getChatMembersCount方法。

        使用此方法获取聊天中的成员数。

        【讨论】:

          猜你喜欢
          • 2016-09-08
          • 2018-03-25
          • 2018-01-30
          • 2022-07-16
          • 2020-09-11
          • 2020-11-01
          • 1970-01-01
          • 1970-01-01
          • 2017-07-12
          相关资源
          最近更新 更多