Skip to content

Instantly share code, notes, and snippets.

@Foadsf
Created November 14, 2023 14:10
Show Gist options
  • Save Foadsf/1dc61d22daf5e5d1d085cad43f00452b to your computer and use it in GitHub Desktop.
Save Foadsf/1dc61d22daf5e5d1d085cad43f00452b to your computer and use it in GitHub Desktop.
Python script using Telethon to unban removed users in a Telegram group. This script fetches the list of users who have been kicked from a Telegram group and unbans them, enabling them to rejoin. It handles rate limits and provides an efficient way to manage group memberships.
import asyncio
from telethon.tl.types import ChannelParticipantsKicked
from telethon.sync import TelegramClient
from telethon.errors import FloodWaitError
# Replace these with your own Telegram API ID and Hash
api_id = 'YOUR_API_ID'
api_hash = 'YOUR_API_HASH'
# Replace with the positive integer ID of your group (omit the '-' sign)
group_id = YOUR_GROUP_ID # Example: 123456789 (not -123456789 or '123456789')
client = TelegramClient('session_name', api_id, api_hash)
async def main():
await client.start()
# Fetching the list of users who have been kicked from the group
kicked_members = await client.get_participants(group_id, filter=ChannelParticipantsKicked)
# Unban each member with a delay to avoid flood wait limits
for member in kicked_members:
try:
await client.edit_permissions(group_id, member, view_messages=True)
print(f"Unbanned {member.first_name} (ID: {member.id})")
# Delay between each unban to avoid hitting rate limits
await asyncio.sleep(10)
except FloodWaitError as e:
# Handling the flood wait error by waiting for the required duration
print(f"Rate limit hit, waiting for {e.seconds} seconds.")
await asyncio.sleep(e.seconds)
# Running the main function
with client:
client.loop.run_until_complete(main())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment