tel2 / groub.py
mfoud444's picture
all
30855e5
import csv
import asyncio
from telethon import TelegramClient
from tqdm import tqdm
# Replace with your API credentials (from https://my.telegram.org/apps)
API_ID = 25216912 # Your API ID
API_HASH = "f65f6050fe9b342a4996c59e4283ab5e"
PHONE_NUMBER = "+9677" # Your phone number with country code
SESSION = "session/a"
# Output CSV file name
CSV_FILENAME = "telegram_groups.csv"
async def get_group_links():
async with TelegramClient(SESSION, API_ID, API_HASH) as client:
await client.start(PHONE_NUMBER) # Login with your phone number
dialogs = await client.get_dialogs()
groups = [d for d in dialogs if d.is_group or d.is_channel]
# Open a CSV file to save the data
with open(CSV_FILENAME, mode="a", newline="", encoding="utf-8") as file:
writer = csv.writer(file)
writer.writerow(["phone_number", "group_name", "username", "group_id", "member_count"])
# Use tqdm to show progress bar
for group in tqdm(groups, desc="Fetching groups", unit="group"):
group_name = group.title
group_id = group.entity.id
# Handle missing username properly
username = f"https://t.me/{group.entity.username}" if hasattr(group.entity, "username") and group.entity.username else "private_group"
try:
# Get member count (only works if you have access)
entity = await client.get_entity(group_id)
member_count = entity.participants_count if hasattr(entity, "participants_count") else "unknown"
except:
member_count = "unknown"
# Save to CSV file (adds phone number)
writer.writerow([PHONE_NUMBER, group_name, username, group_id, member_count])
print(f"\n✅ Data saved to {CSV_FILENAME}")
# Run the async function
asyncio.run(get_group_links())