如何使用Python获取Telegram中群组/频道中所有用户的用户ID?

问题描述 投票:0回答:1

我正在尝试使用 Python 获取 Telegram 中组/频道中所有用户的用户 ID。我尝试过使用 telebot 库中的 get_chat_members() 方法,但此方法不可用。

有没有其他方法可以使用Python获取Telegram中群组/频道中所有用户的用户ID?

下面的代码适用于管理员,但我需要所有用户。

bot = telebot.TeleBot(bot_token)

administrators = bot.get_chat_administrators(chat_id)

print("List of Administrators:")
for admin in administrators:
    print(f"{admin.user.first_name} (ID: {admin.user.id})")

bot.stop_polling()

预先感谢您的帮助!

python-3.x telegram-bot python-telegram-bot
1个回答
0
投票

所以,我找到了答案。无法通过远程机器人获取详细信息。

解决方案是使用 telethon 中的 TelegramClient。我将把它留在这里给任何遇到同样问题的人。

功劳归于人。

首先,在这里创建一个帐户,获取您的 api_id 和 api_hash

现在下面的代码应该可以完成它:

from telethon import TelegramClient
import asyncio

api_id = '' # Your API_ID
api_hash = "" # Your APP_ID
bot_token = "" # via botfather
group_id = Your_chat_id

async def get_users(client, group_id):
    async for user in client.iter_participants(group_id):
        if not user.deleted:
            print("id:", user.id, "username:", user.username) 

bot = TelegramClient('bot', api_id, api_hash).start(bot_token=bot_token)

with bot:
    asyncio.get_event_loop().run_until_complete(get_users(bot, group_id))
© www.soinside.com 2019 - 2024. All rights reserved.