LonamiWebs / Telethon

Pure Python 3 MTProto API Telegram client library, for bots too!

Home Page:https://docs.telethon.dev

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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

TipsterTrust opened this issue · comments

Describe your suggested feature

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 telethon import TelegramClient, errors
from telethon.tl.functions.channels import GetParticipantsRequest
from telethon.tl.types import ChannelParticipantsSearch
from telethon.tl.functions.channels import GetFullChannelRequest
from datetime import datetime, timedelta, timezone

async def get_participants():
    api_id = ""
    api_hash = ""
    channel_id = 
    limite_inactividad = datetime.now(timezone.utc) - timedelta(days=90)
    offset_increment = 200

    async with TelegramClient('xxx', api_id, api_hash) as client:
        try:
            channel_full_info = await client(GetFullChannelRequest(channel=channel_id))
            channel_count = channel_full_info.full_chat.participants_count
            print("Channel count: " + str(channel_count))
            offset = 0
            while offset < channel_count:
                participants = await client(GetParticipantsRequest(
                    channel=channel_id,
                    filter=ChannelParticipantsSearch(''),
                    offset=offset,
                    limit=10000,
                    hash=0
                ))

                i = 1
                for u in participants.users:
                    print(f"Member Number: {offset + i} Name: {u.first_name}")  
                    if u.deleted or u.status == "deleted":
                        await client.kick_participant(channel_id, u.id)
                        print(f"Removed deleted user: {u.id}")
                    elif u.status == "offline" and u.status.was_online and u.status.was_online < limite_inactividad:
                        await client.kick_participant(channel_id, u.id)
                        print(f"Removed inactive user: {u.id}")
                    i += 1

                offset += offset_increment
                print("RETURN BACK " + str(offset))
        except errors.FloodWaitError as e:
            print(f"FloodWaitError: You have exceeded Telegram's rate limit. Please wait for {e.seconds} seconds.")

if __name__ == "__main__":
    import asyncio
    asyncio.run(get_participants())

However, I can only get 200 members, even if I move the offset, the last member its still the 200th. I mean, if I put the offset on 10 I just get 190 members and 0 if I put it on 200. Is there any wa to filter members that are deleted or last seen a long time ago?

Thanks!

Checklist

  • I have searched for this issue before posting it and there isn't a duplicate.

As far as I'm aware Telegram's API doesn't support this, so it can't be implemented in Telethon either.