【问题标题】:How to get all users in telegram chat with telebot or requests lib in Python?如何让所有用户与 Telebot 进行电报聊天或在 Python 中请求 lib?
【发布时间】:2020-09-11 04:56:50
【问题描述】:

我想获取我的频道/聊天中所有用户的列表。人们建议使用 telethon,但我无法开始使用 telethone 并想改用请求。你能帮帮我吗?

我试过了

import json 
import requests
URL = "https://api.telegram.org/bot{}/".format(TOKEN) 
url = URL + "getFullChat?chat_id={}".format(chat_id)
req = requests.get(url)

TOKEN 是我的机器人 ID,chat_id 是聊天,我想从中获取列表。

req.content

还给我

b'{"ok":false,"error_code":404,"description":"Not Found"}'

https://core.telegram.org/method/channels.getFullChannel

https://core.telegram.org/method/messages.getFullChat

【问题讨论】:

    标签: python python-requests telegram


    【解决方案1】:

    我解决了我的问题,这里是代码

    from telethon.sync import TelegramClient 
    
    api_id = XXX # int
    api_hash = 'XXX'
     
    client = TelegramClient('session_name_choose_yourself', api_id, api_hash)
     
    assert client.start()
     
    if not client.is_user_authorized():
        client.send_code_request(bot_token)
     
    entity=client.get_entity("@ChatUserName")
    users = client.get_participants(entity)
    print(len(users))
     
    for i in users:
        print(i.id, i.username, i.first_name, i.last_name)
    

    创建的会话将存储您需要手动添加的密码和/或电话号码

    【讨论】:

      猜你喜欢
      • 2022-10-18
      • 2021-12-04
      • 1970-01-01
      • 1970-01-01
      • 2020-07-06
      • 2020-07-28
      • 2018-11-20
      • 1970-01-01
      • 2018-05-21
      相关资源
      最近更新 更多