-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
474 lines (407 loc) · 12.9 KB
/
main.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
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
####################################################################
##################### CONFIGURATION ################################
####################################################################
api_id = 6 # int
api_hash = "eb06d4abfb49dc3eeb1aeb98ae0f581e" # string
bot_token = "5433648888:AAFYD1VZvo0jNl2wdPiS8XjxVQ3r_yj9b2k" # string
admins = "1872074304" # string
####################################################################
######################### IMPORTS ##################################
####################################################################
from uuid import uuid1
from datetime import datetime, timedelta
# from cairo import Error
from telethon import TelegramClient, events
from telethon.errors.rpcerrorlist import ApiIdInvalidError
from telethon.tl.functions.messages import (
ImportChatInviteRequest,
CheckChatInviteRequest,
)
from telethon.tl.functions.channels import JoinChannelRequest, LeaveChannelRequest
from telethon.tl.functions.account import UpdateNotifySettingsRequest
from telethon.tl.types import InputPeerNotifySettings, InputNotifyPeer
from apscheduler.schedulers.asyncio import AsyncIOScheduler
import os, asyncio, random, shutil, sys
####################################################################
########################## DATA SETUP ##############################
####################################################################
bot = TelegramClient(None, api_id=api_id, api_hash=api_hash).start(bot_token=bot_token)
APIS = [[api_id, api_hash]]
if os.path.exists("api.csv"):
APIS = [
line.split(",") for line in open("api.csv", "r", encoding="utf-8").readlines()
]
session = [
file.split(".")[0] for file in os.listdir("sessions/") if file.endswith(".session")
]
admin = [int(ad) for ad in admins.split()]
LOADED = []
UNLOADED = []
CLIENTS = []
SCHED = []
DATA = {}
JDATA = {}
LDATA = {}
LdDATA = {}
####################################################################
######################### FUNCTIONS ################################
####################################################################
async def load(phone, uid):
if phone in LOADED or phone in UNLOADED:
return
data = LdDATA.get(uid)
target = data["target"]
count = data.get("count") or 0
if target and count >= target:
return
try:
api_id, api_hash = random.choice(APIS)
client = TelegramClient(f"sessions/{phone}", api_id, api_hash)
await client.connect()
if not await client.is_user_authorized():
raise SystemError
client.phone = phone
LOADED.append(phone)
DATA[phone] = client
CLIENTS.append(client)
data["count"] += 1
except ApiIdInvalidError:
APIS.remove([api_id, api_hash])
except BaseException:
UNLOADED.append(phone)
session.remove(phone)
try:
await client.disconnect()
except:
pass
try:
if not os.path.isdir("expired"):
os.mkdir("expired")
shutil.move(f"sessions/{phone}.session", f"expired/")
except:
pass
async def load_all(num):
uid = str(uuid1())
LdDATA.update({uid: {"target": num or None, "count": 0}})
start = 0
for i in range((len(session) // 10) + 1):
task = []
for z in session[start : ((i + 1) * 10)]:
task.append(load(z, uid))
start += 10
if task:
await asyncio.gather(*task)
task.clear()
return LdDATA[uid]["count"]
async def disc_all(num=None):
for client in list(CLIENTS):
try:
await client.disconnect()
LOADED.remove(client.phone)
except:
pass
CLIENTS.clear()
return
def stop_interval():
for sched in SCHED:
sched.shutdown()
SCHED.clear()
JDATA.clear(), LDATA.clear()
return
async def join_chat(link, mute=False, num=None, clients=[]):
hash = False
if "@" in link:
chat = link.strip().split()[0]
elif "/joinchat/" in link:
chat = link.split("/")[-1].replace("+", "")
hash = True
elif "+" in link:
chat = link.split("/")[-1].replace("+", "")
hash = True
elif "-100" in link or link.isdigit():
chat = int(link)
else:
chat = link.strip().split()[0]
cnt = 0
for phone in clients:
try:
if num and cnt >= num:
break
client = DATA[phone]
if hash:
ch = await client(ImportChatInviteRequest(chat))
else:
ch = await client(JoinChannelRequest(chat))
cnt += 1
if LDATA.get(link):
LDATA[link].remove(client.phone)
if mute:
await client(
UpdateNotifySettingsRequest(
peer=InputNotifyPeer(ch.chats[0].id),
settings=InputPeerNotifySettings(
mute_until=datetime.now() + timedelta(days=365),
silent=True,
),
)
)
except Exception as er:
if JDATA.get(link):
JDATA[link].remove(client.phone)
print(er)
return cnt
async def leave_chat(link, num=None, clients=[]):
hash = False
if "@" in link:
chat = link.split()[0]
elif "/joinchat/" in link:
chat = link.split("/")[-1].replace("+", "")
hash = True
elif "+" in link:
chat = link.split("/")[-1].replace("+", "")
hash = True
elif "-100" in link or link.isdigit():
chat = int(link)
else:
chat = link.split()[0]
cnt = 0
for phone in clients:
try:
client = DATA[phone]
if hash:
ch = await client(CheckChatInviteRequest(chat))
if ch.left:
continue
chat = ch.id
await client(LeaveChannelRequest(chat))
cnt += 1
if JDATA.get(link):
JDATA[link].remove(client.phone)
if num and cnt >= num:
break
except:
if LDATA.get(link):
LDATA[link].remove(client.phone)
return cnt
async def sched_join(link, mute, count):
task = []
if not JDATA.get(link):
JDATA[link] = []
for phone in list(LOADED):
if phone in JDATA[link]:
continue
task.append(join_chat(link, mute, clients=[phone]))
JDATA[link].append(phone)
if len(task) >= count:
break
await asyncio.gather(*task)
async def sched_leave(link, count):
task = []
if not LDATA.get(link):
LDATA[link] = []
for phone in list(LOADED):
if phone in LDATA[link]:
continue
task.append(leave_chat(link, clients=[phone]))
LDATA[link].append(phone)
if len(task) >= count:
break
await asyncio.gather(*task)
def get_sec(inp):
if inp.isdigit():
return int(inp)
elif inp.endswith("s"):
return int(inp[:-1])
elif inp.endswith("m"):
return int(inp[:-1]) * 60
elif inp.endswith("h"):
return int(inp[:-1]) * 60 * 60
####################################################################
######################## COMMANDS ##################################
####################################################################
@bot.on(events.NewMessage(pattern="^/start"))
async def _(e):
await e.reply(
f"Hello Sir!!!\n\nTOTAL SESSIONS: {len(session)}\nTOTAL CONNECTED: {len(LOADED)}\nERROR CONNECTING: {len(UNLOADED)}"
)
@bot.on(events.NewMessage(pattern="^/load ?(.*)"))
async def _(e):
if not e.sender_id in admin:
return
cnt = e.pattern_match.group(1) or None
if cnt and cnt.isdigit():
cnt = int(cnt)
x = await e.reply(f"Loading {cnt if cnt else 'All' } Sessions...")
num = await load_all(num=cnt)
await x.edit(
f"Sucessfully loaded {num} Clients.\n\nTOTAL SESSIONS: {len(session)}\nTOTAL CONNECTED: {len(LOADED)}\nERROR CONNECTING: {len(UNLOADED)}"
)
@bot.on(events.NewMessage(pattern="^/end"))
async def _(e):
if not e.sender_id in admin:
return
x = await e.reply("Disconnecting...")
stop_interval()
await disc_all()
await x.edit("All Disconnected & Intervals Stopped.")
@bot.on(events.NewMessage(pattern="^/stop"))
async def _(e):
if not e.sender_id in admin:
return
stop_interval()
await e.reply("All Intervals Stopped")
@bot.on(events.NewMessage(pattern="^/leave (.*)"))
async def _(e):
if not e.sender_id in admin:
return
pat = e.pattern_match.group(1)
if not pat:
return
inp = pat.split()
if len(inp) == 2:
link, num = inp
interval = None
elif len(inp) == 3:
link, num, interval = inp
else:
return
num = int(num)
if not interval:
x = await e.reply(f"Leaving {link}")
nn = await leave_chat(link, clients=LOADED, num=num)
await x.edit(f"{nn} clients left.")
else:
interval_time = get_sec(interval)
if not interval_time:
return await e.reply("Wrong Interval time format.")
await e.reply(
f"PROCESS STARTED\n\n{num} client will left {link} on interval of {interval}"
)
await sched_leave(link, num)
schd = AsyncIOScheduler()
schd.add_job(
sched_leave, trigger="interval", args=[link, num], seconds=interval_time
)
schd.start()
SCHED.append(schd)
@bot.on(events.NewMessage(pattern="^/join (.*)"))
async def _(e):
if not e.sender_id in admin:
return
mute = False
pat = e.pattern_match.group(1)
if not pat:
return
inp = pat.split()
if len(inp) == 2:
link, num = inp
interval = None
elif len(inp) == 3:
link, num, interval = inp
else:
return
num = int(num)
if not interval:
x = await e.reply(f"Joining {link}")
nn = await join_chat(link, mute, num, clients=LOADED)
await x.edit(f"{nn} clients joined {link}")
else:
interval_time = get_sec(interval)
if not interval_time:
return await e.reply("Wrong Interval time format.")
await e.reply(
f"PROCESS STARTED\n\n{num} client will join {link} on interval of {interval}"
)
await sched_join(link, mute, num)
schd = AsyncIOScheduler()
schd.add_job(
sched_join,
trigger="interval",
args=[link, mute, num],
seconds=interval_time,
)
schd.start()
SCHED.append(schd)
@bot.on(events.NewMessage(pattern="^/joinmute (.*)"))
async def _(e):
if not e.sender_id in admin:
return
mute = True
pat = e.pattern_match.group(1)
if not pat:
return
inp = pat.split()
if len(inp) == 2:
link, num = inp
interval = None
elif len(inp) == 3:
link, num, interval = inp
else:
return
num = int(num)
if not interval:
x = await e.reply(f"Joining {link}")
nn = await join_chat(link, mute, num, clients=LOADED)
await x.edit(f"{nn} clients joined {link}")
else:
interval_time = get_sec(interval)
if not interval_time:
return await e.reply("Wrong Interval time format.")
await e.reply(
f"PROCESS STARTED\n\n{num} client will join {link} on interval of {interval}"
)
await sched_join(link, mute, num)
schd = AsyncIOScheduler()
schd.add_job(
sched_join,
trigger="interval",
args=[link, mute, num],
seconds=interval_time,
)
schd.start()
SCHED.append(schd)
@bot.on(events.NewMessage(pattern="^/admin"))
async def _(e):
await e.reply(admins)
@bot.on(events.NewMessage(pattern="^/help"))
async def _(e):
await e.reply(
"""
Some following command
/start
Start the bot
/load <number of acc>
Connect clients
__Note: without loading clients you cant use other func__
/join <link> <count> <interval>
You can use without interval too
__Note: in interval put- 1h or 1m or 1s__
/joinmute <link> <count> <interval>
You can use without interval too
__Note: in interval put- 1h or 1m or 1s__
/leave <link> <count> <interval>
You can use without interval too
__Note: in interval put- 1h or 1m or 1s__
/end
Disconnect all clients
/stop
Stop all running Interval Process.
/exit
Stop Everything & Bot off
/admins
To get ids of admin who can use bot.
"""
)
@bot.on(events.NewMessage(pattern="^/exit"))
async def _(e):
if not e.sender_id in admin:
return
stop_interval()
await disc_all()
sys.exit()
####################################################################
######################### RUN-BOT ##################################
####################################################################
with bot:
print("Bot Started")
bot.run_until_disconnected()