-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadd_users_to_group_by_phone.py
More file actions
53 lines (42 loc) · 1.87 KB
/
add_users_to_group_by_phone.py
File metadata and controls
53 lines (42 loc) · 1.87 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
from telethon import TelegramClient
from telethon.tl.functions.contacts import AddContactRequest
from telethon.tl.functions.messages import AddChatUserRequest
# Replace with your own API ID and API Hash
api_id = 'YOUR_API_ID' # Your API ID
api_hash = 'YOUR_API_HASH' # Your API Hash
group_id = 'YOUR_GROUP_ID' # Your group ID (can be fetched by username or chat ID)
# Read phone numbers from the text file
with open('phone_numbers.txt', 'r') as file:
phone_numbers = [line.strip() for line in file.readlines()]
# Create the client session
client = TelegramClient('session_name', api_id, api_hash)
async def main():
# Start the client
await client.start()
# Get the group entity (by username or chat ID)
group_entity = await client.get_entity(group_id)
# Loop through the list of phone numbers
for phone_number in phone_numbers:
try:
# Add contact by phone number
contact = await client(AddContactRequest(
phone_number=phone_number, # Phone number to add
first_name="New", # You can provide a first name
last_name="Contact" # You can provide a last name
))
# Now you can get the contact's user ID
user_entity = await client.get_entity(contact.user)
# Add user to the group
await client(AddChatUserRequest(
chat_id=group_entity.id, # The group ID
user_id=user_entity.id, # The user ID
fwd_limit=10 # Optional: limits forwarded messages (default: 10)
))
print(f"Successfully added {phone_number} to the group!")
except Exception as e:
print(f"Failed to add {phone_number}: {e}")
# Disconnect after completing the operation
await client.disconnect()
# Run the script
import asyncio
asyncio.run(main())