Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fetch more than 200 or filter by deleted / last seen a long time ago #4342 #1412

Open
2 tasks done
TipsterTrust opened this issue Apr 4, 2024 · 1 comment
Open
2 tasks done

Comments

@TipsterTrust
Copy link

Checklist

  • I believe the idea is awesome and would benefit the framework
  • I have searched in the issue tracker for similar requests, including closed ones

Description

Hi,
I'm trying to develop a bot to clean deleted accounts and last seen a long time ago on my channels. Some people with bad intentions added me many fake users and I don't want em on my channels. This is my current code:

from pyrogram import Client
from pyrogram.types import ChatPermissions
from datetime import datetime
import time

# Inserta el chat_id de la conversación de Telegram donde se ejecutará el bot
chat_id = 

# Inserta el token de tu bot de Telegram
bot_token = ""

# Inserta tu api_hash y api_id de Pyrogram
api_id = ""
api_hash = ""


async def remove_deleted_accounts(client, chat_id, offset=0, step=200):
    total = await client.get_chat_members_count(chat_id)
    print("Total members: " + str(total))
    itrs = (total//step) + 1
    members_list = []
    itr = 1
    while itr <= itrs:
        time.sleep(1)
        print("VUELTA")
        async for member in client.get_chat_members(chat_id, query=""):
            try:
                print(str(member.user.id) + ' ' + str(member.user.username) + ' ' + str(member.user.status))
                if member.user.is_deleted or str(member.user.status) == "UserStatus.LONG_AGO":
                    user_id = member.user.id
                    print(str(datetime.now()) + f" Removing deleted account: {user_id}")
                    await client.ban_chat_member(chat_id, user_id)
                    # Espera un breve momento para evitar el exceso de solicitudes
                    time.sleep(1)
            except Exception as e:
                print(e)
    offset += step
    itr += 1


async def main():
    async with Client("bot", bot_token=bot_token, api_id=api_id, api_hash=api_hash) as app:
        # Llama a la función para remover cuentas eliminadas
        await remove_deleted_accounts(app, chat_id)

if __name__ == "__main__":
    # Execute the coroutine using asyncio
    import asyncio
    asyncio.run(main())

However, I can only get 200 members. Is there any wa to filter members that are deleted or last seen a long time ago?

Thanks!

@KurimuzonAkuma
Copy link

You can't get more than 200 users in a channel and 10k in a chat - that's a telegram limitation.
The most you can do is to try to get all users by searching through get_chat_members with query parameter.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

No branches or pull requests

2 participants