-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtelegram_channel_export.py
57 lines (46 loc) · 1.99 KB
/
telegram_channel_export.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import csv
import logging
from telethon.sync import TelegramClient
from telethon.errors import SessionPasswordNeededError
# Configure logging
logging.basicConfig(level=logging.INFO, format="%(message)s")
# Replace with your Telegram API credentials
API_ID = YOUR_API_ID # Example: 123456
API_HASH = "YOUR_API_HASH" # Example: "abcdef1234567890abcdef1234567890"
SESSION_NAME = "telegram_channel_export"
# Output file
OUTPUT_FILE = "telegram_channels.csv"
def main():
"""Exports Telegram channels to a CSV file including channel links."""
try:
with TelegramClient(SESSION_NAME, API_ID, API_HASH) as client:
logging.info("✅ Connected to Telegram. Fetching channels...")
channels = []
for dialog in client.iter_dialogs():
if dialog.is_channel:
username = dialog.entity.username
if username:
link = f"https://t.me/{username}"
else:
link = "Private"
channels.append([
dialog.title,
dialog.entity.id,
username or "Private",
link
])
if not channels:
logging.info("⚠️ No channels found!")
return
# Save to CSV
with open(OUTPUT_FILE, "w", newline="", encoding="utf-8") as file:
writer = csv.writer(file)
writer.writerow(["Channel Name", "Channel ID", "Username", "Channel Link"])
writer.writerows(channels)
logging.info(f"✅ Export completed! Channels saved in '{OUTPUT_FILE}'")
except SessionPasswordNeededError:
logging.error("⚠️ Two-Step Verification is enabled. Please provide your password.")
except Exception as e:
logging.error(f"❌ An error occurred: {e}")
if __name__ == "__main__":
main()