-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauto_group_control.py
105 lines (82 loc) · 3.22 KB
/
auto_group_control.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
from telethon import TelegramClient, types, functions
from telethon.errors.rpcerrorlist import SessionPasswordNeededError
from time import sleep
# pip install telethon
# get api id and api hash from https://my.telegram.org/apps after creating an app there
api_id = ''
api_hash = ''
group_title = 'group name that you want to control'
phone_number = 'place phone number used in telegram account'
# Initialize the client
client = TelegramClient('mysession', api_id, api_hash)
group_id = None
async def disable_group(group_id):
await client.connect()
if not await client.is_user_authorized():
print("User not authorized. Requesting code...")
await client.send_code_request(phone_number)
code = input('Enter the code: ')
try:
await client.sign_in(phone_number, code)
except SessionPasswordNeededError:
password = input('Two-step verification password: ')
await client.sign_in(password=password)
print("Authenticated")
await client(functions.messages.EditChatDefaultBannedRightsRequest(
peer=group_id,
banned_rights=types.ChatBannedRights(
until_date=None,
send_messages=True
)
))
print("Group disabled successfully.")
await client.disconnect()
async def reopen_group(group_id):
await client.connect()
if not await client.is_user_authorized():
print("User not authorized. Requesting code...")
await client.send_code_request(phone_number)
code = input('Enter the code: ')
try:
await client.sign_in(phone_number, code)
except SessionPasswordNeededError:
password = input('Two-step verification password: ')
await client.sign_in(password=password)
print("Authenticated")
await client(functions.messages.EditChatDefaultBannedRightsRequest(
peer=group_id,
banned_rights=types.ChatBannedRights(
until_date=None,
send_messages=False # Allow sending messages
)
))
await client.disconnect()
async def get_private_group_id(group_title):
global group_id
await client.connect()
if not await client.is_user_authorized():
print("User not authorized. Requesting code...")
phone_number = '+8801877524080'
await client.send_code_request(phone_number)
code = input('Enter the code: ')
try:
await client.sign_in(phone_number, code)
except SessionPasswordNeededError:
password = input('Two-step verification password: ')
await client.sign_in(password=password)
print("Authenticated")
async for dialog in client.iter_dialogs():
if dialog.is_group and dialog.title == group_title:
group_id = dialog.id
print(f"Group ID found: {dialog.id}")
break
else:
print("Group not found.")
await client.disconnect()
client.loop.run_until_complete(get_private_group_id(group_title))
sleep(2)
if group_id:
# uncomment below link if you want to disable group messages
# client.loop.run_until_complete(disable_group(group_id))
# below line will reopen group messages
client.loop.run_until_complete(reopen_group(group_id))