-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlist_channels.py
More file actions
258 lines (220 loc) · 7.89 KB
/
list_channels.py
File metadata and controls
258 lines (220 loc) · 7.89 KB
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
#!/usr/bin/env python3
"""
List Telegram Channels
Shows all channels/chats the logged-in user has access to.
Saves channel metadata to raw/channels.json (only for channels in config.toml)
"""
import asyncio
import json
import os
import sys
from pathlib import Path
from dotenv import load_dotenv
from telethon import TelegramClient
from telethon.tl.types import Channel, Chat, User
# Use built-in tomllib for Python 3.11+, fallback to tomli for older versions
if sys.version_info >= (3, 11):
import tomllib
else:
try:
import tomli as tomllib
except ImportError:
import toml as tomllib
# Load environment variables
load_dotenv()
def load_config():
"""Load configuration from config.toml"""
config_path = Path(__file__).parent / "config.toml"
if not config_path.exists():
return None
with open(config_path, "rb") as f:
return tomllib.load(f)
async def main():
"""List all channels/chats the user has access to."""
print("📋 Listing Your Telegram Channels\n")
# Load config to filter which channels to save
config = load_config()
config_channel_ids = set()
config_group_ids = set()
if config:
config_group_ids = set(config.get("group_chats", {}).get("include", []))
config_channel_ids = set(config.get("channels", {}).get("include", []))
# Get environment variables
api_id = int(os.getenv("TELEGRAM_APP_ID", "0"))
api_hash = os.getenv("TELEGRAM_APP_HASH", "")
phone = os.getenv("TELEGRAM_PHONE", "")
if not api_id or not api_hash:
print("❌ Error: Missing Telegram credentials in .env file")
print("Please set TELEGRAM_APP_ID and TELEGRAM_APP_HASH")
sys.exit(1)
# Initialize Telegram client with session file
session_file = "telegram_session"
client = TelegramClient(session_file, api_id, api_hash)
try:
print("🔌 Connecting to Telegram...")
await client.start(
phone=lambda: phone
or input(
"📱 Enter your phone number (with country code, e.g., +1234567890): "
),
password=lambda: input("🔒 Enter your 2FA password (if enabled): "),
code_callback=lambda: input("💬 Enter the code Telegram sent you: "),
)
if not await client.is_user_authorized():
print("❌ Authorization failed. Please try again.")
sys.exit(1)
me = await client.get_me()
print(f"✅ Connected as: {me.first_name} (@{me.username or 'no username'})\n")
print("=" * 80)
print("Fetching your channels and chats...")
print("=" * 80)
# Get all dialogs (chats/channels)
dialogs = await client.get_dialogs()
channels = []
groups = []
users = []
for dialog in dialogs:
entity = dialog.entity
if isinstance(entity, Channel):
if entity.broadcast:
# It's a channel
channels.append(
{
"title": entity.title,
"username": entity.username,
"id": entity.id,
"access_hash": entity.access_hash,
}
)
else:
# It's a supergroup
groups.append(
{
"title": entity.title,
"username": entity.username,
"id": entity.id,
"access_hash": entity.access_hash,
}
)
elif isinstance(entity, Chat):
# Regular group
groups.append(
{
"title": entity.title,
"username": None,
"id": entity.id,
"access_hash": None,
}
)
elif isinstance(entity, User):
# Private chat
users.append(
{
"name": f"{entity.first_name or ''} {entity.last_name or ''}".strip(),
"username": entity.username,
"id": entity.id,
}
)
# Print channels
print(f"\n📢 CHANNELS ({len(channels)})")
print("=" * 80)
if channels:
for ch in channels:
username_str = (
f"@{ch['username']}" if ch["username"] else "(no username)"
)
print(f" • {ch['title']}")
print(f" Username: {username_str}")
print(f" ID: {ch['id']}")
print(f" Config: {ch['id']}")
print()
else:
print(" (No channels found)\n")
# Print groups
print(f"👥 GROUPS & SUPERGROUPS ({len(groups)})")
print("=" * 80)
if groups:
for grp in groups:
username_str = (
f"@{grp['username']}" if grp["username"] else "(no username)"
)
print(f" • {grp['title']}")
print(f" Username: {username_str}")
print(f" ID: {grp['id']}")
print(f" Config: {grp['id']}")
print()
else:
print(" (No groups found)\n")
# Print summary
print("=" * 80)
print("📊 SUMMARY")
print("=" * 80)
print(f" Channels: {len(channels)}")
print(f" Groups: {len(groups)}")
print(f" Private chats: {len(users)}")
print(f" Total: {len(dialogs)}")
print()
# Print config help
print("=" * 80)
print("💡 HOW TO USE IN config.toml")
print("=" * 80)
print("Add to your config.toml under [channels] include:")
print()
print("[channels]")
print("include = [")
# Show first 5 channels as examples
count = 0
for ch in channels[:5]:
print(f" {ch['id']}, # {ch['title']}")
count += 1
if len(channels) > 5:
print(f" # ... and {len(channels) - 5} more channels")
print("]")
print()
# Save channels and groups to raw/channels.json (only those in config.toml)
raw_dir = Path("raw")
raw_dir.mkdir(exist_ok=True)
channels_data = []
for ch in channels:
if ch["id"] in config_channel_ids:
channels_data.append(
{
"channel_id": ch["id"],
"name": ch["title"],
"username": ch["username"],
"is_group": False,
}
)
for grp in groups:
if grp["id"] in config_group_ids:
channels_data.append(
{
"channel_id": grp["id"],
"name": grp["title"],
"username": grp["username"],
"is_group": True,
}
)
channels_file = raw_dir / "channels.json"
with open(channels_file, "w", encoding="utf-8") as f:
json.dump(channels_data, f, indent=2, ensure_ascii=False)
print(
f"💾 Saved {len(channels_data)} channels/groups from config.toml to {channels_file}"
)
print()
print("✅ Done!\n")
except Exception as e:
print(f"❌ Error: {e}")
sys.exit(1)
finally:
if client.is_connected():
await client.disconnect()
if __name__ == "__main__":
try:
asyncio.run(main())
except KeyboardInterrupt:
print("\n⚠️ Interrupted by user")
sys.exit(0)
except Exception as e:
print(f"❌ Error: {e}")
sys.exit(1)