-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path__init__.py
158 lines (92 loc) · 4.02 KB
/
__init__.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
#!/usr/bin/python3
from telethon import TelegramClient, events
from telethon.sessions import StringSession
from telethon import functions, types
from telethon import errors
import telethon.sync
import os
# Remember to use your own values from my.telegram.org[api_id,api_hash,string_session]!
api_id = os.environ.get("APP_ID", 6)
api_hash = os.environ.get("API_HASH", "eb06d4abfb49dc3eeb1aeb98ae0f581e")
string = os.environ.get("STRING_SESSION", None)
client = TelegramClient(StringSession(string), api_id, api_hash)
whitelist = set()
myIDXYZ_COOKY = None
onHold = False # Managing bot Live/Dead
# Refresh contact list
async def refresh_contacts():
global whitelist
result = await client(functions.contacts.GetContactsRequest(
hash=0
))
for i in range(len(result.contacts)):
whitelist.add(result.contacts[i].user_id)
return 0
async def manual():
await client.send_message('me', '**Usage :** \n'
'```.start``` or ```.restart``` -> Restart the bot [Default - on start]\n\n'
'```.stop``` or ```.hold``` -> Stops the bot [Use ```.start``` or ```.restart``` to enable]\n\n'
'```.ap``` or ```.approve```-> Adds the user in whitelist when you reply\n\n'
'```.dis``` or ```.disapprove``` -> Removes the user from whitelist when you reply\n\n'
'```.re``` or ```.refresh``` -> Refresh the contacts to the bot\n\n'
'```.man``` or ```.manual``` or ```.h``` or ```.help``` -> For manual page\n\n'
'**Note : **\n'
'1. approve or disapprove can be used in groups and channels. Remaining in private\n'
'2. Users in whitelist can\'t be banned automatically')
return 0
# Starting function
async def main():
global whitelist
global myIDXYZ_COOKY
myIDXYZ_COOKY = await client.get_peer_id('me') # User INT ID
whitelist.add(myIDXYZ_COOKY)
await refresh_contacts()
async for dialog in client.iter_dialogs():
whitelist.add(dialog.id)
await manual()
# Handling events
@client.on(events.NewMessage)
async def my_event_handler(event):
global onHold
raw_text = event.raw_text
if not onHold:
global whitelist
sender_id = event.sender_id
is_reply_condition = event.is_reply and sender_id == myIDXYZ_COOKY and event.is_private == False
if is_reply_condition and raw_text in ['.ap', '.approve']:
replied = await event.get_reply_message()
whitelist.add(replied.from_id)
await event.delete()
elif is_reply_condition and raw_text in ['.dis', '.disapprove']:
replied = await event.get_reply_message()
whitelist.discard(replied.from_id)
await event.delete()
elif event.is_private:
chat_id = event.chat_id
if sender_id == myIDXYZ_COOKY:
whitelist.add(chat_id)
elif chat_id not in whitelist:
await client.get_dialogs()
await client(functions.messages.ReportSpamRequest(
peer=chat_id
))
await client(functions.contacts.BlockRequest(id=chat_id))
await client.delete_dialog(chat_id)
if raw_text in ['.re', '.refresh']:
await refresh_contacts()
await event.delete()
if raw_text in ['.h', '.help', '.man', '.manual']:
await manual()
await event.delete()
if raw_text in ['.stop', '.hold']:
onHold = True
await client.send_message('me', 'Stopped!')
elif onHold and raw_text in ['.start', '.restart']:
onHold = False
await client.send_message('me', 'Bot Started!')
# main function
with client:
client.loop.run_until_complete(main())
# event initialization
client.start()
client.run_until_disconnected()