-
Notifications
You must be signed in to change notification settings - Fork 3
/
add.py
65 lines (54 loc) · 2.46 KB
/
add.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
from telethon.sync import TelegramClient
from telethon.tl.functions.channels import InviteToChannelRequest
from telethon.tl.functions.messages import GetDialogsRequest
from telethon.tl.types import InputPeerEmpty
import time
# Replace 'API_ID' and 'API_HASH' with your actual API ID and API Hash
api_id = 'Your-API-ID'
api_hash = 'You-API-hash'
phone = 'Phone-Number' # Include country code, e.g., +123456789
client = TelegramClient(phone, api_id, api_hash)
async def main():
await client.start()
# Get dialogs
dialogs = await client(GetDialogsRequest(
offset_date=None,
offset_id=0,
offset_peer=InputPeerEmpty(),
limit=200,
hash=0
))
# Print out all dialogs
for i, chat in enumerate(dialogs.chats):
print(f"{i}. {chat.title} ({chat.id})")
# Choose source group
source_group_index = int(input("Enter the number of the source group: "))
source_group = dialogs.chats[source_group_index]
# Choose destination group
destination_group_index = int(input("Enter the number of the destination group: "))
destination_group = dialogs.chats[destination_group_index]
# Get all participants from the source group
source_participants = await client.get_participants(source_group)
# Get all participants from the destination group
destination_participants = await client.get_participants(destination_group)
destination_participant_ids = {user.id for user in destination_participants}
# Add participants to the destination group
for user in source_participants:
if user.id not in destination_participant_ids:
retries = 0
while retries < 5: # Limit the number of retries
try:
await client(InviteToChannelRequest(destination_group, [user]))
print(f"Added {user.id} to {destination_group.title}")
time.sleep(5) # Avoid hitting limits
break
except Exception as e:
print(f"Failed to add {user.id}: {e}")
retries += 1
backoff_time = 5 * (2 ** retries) # Exponential backoff
print(f"Retrying in {backoff_time} seconds...")
time.sleep(backoff_time)
else:
print(f"User {user.id} is already in {destination_group.title}")
with client:
client.loop.run_until_complete(main())