-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
263 lines (202 loc) · 7.79 KB
/
utils.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
from curses import reset_shell_mode
from telethon import client
from config import *
class TelegramApi:
def __init__(self) -> None:
self.api_id = API_ID
self.api_hash = API_HASH
self.receivers = []
self.client = None
@property
def get_proxy(self):
proxy = {
'proxy_type': socks.SOCKS5,
'addr': '127.0.0.1',
'port': 8100,
'username': 'username',
'password': 'password',
'rdns': False
}
return proxy
@property
def start(self):
self.loop = asyncio.new_event_loop()
self.client = TelegramClient(StringSession(), API_ID, API_HASH, loop=self.loop)
# self.client = TelegramClient(
# StringSession(),
# self.api_id,
# self.api_hash,
# connection=connection.ConnectionTcpMTProxyRandomizedIntermediate,
# proxy=('127.0.0.1', 3000, '00000000000000000000000000000000')
# )
def sign_in(self, session:str):
"Sign In A Session ID"
if self.client is not None:
self.stop
self.loop = asyncio.new_event_loop()
# print(session)
self.client = TelegramClient(StringSession(session), API_ID, API_HASH, loop=self.loop).start()
user = self.client.loop.run_until_complete(self.client.get_me())
print(f"Signed In As {user.first_name} ({user.id}) ({user.phone})")
@property
def session(self):
return self.client.session.save()
@property
def stop(self):
self.client.disconnect()
def write_to_json(self, session:str):
"Write Session to Database"
file = open(f'{cwd}/data.json')
data = json.load(file)
# add session
data['sessions'].append(session)
with open(f'{cwd}/data.json', 'w') as json_file:
json.dump(data, json_file, indent=4)
def check_receiver(self, user_id:int) -> bool:
"Checks the receivers file if user_id exists"
file = open(f'{cwd}/users.json')
data = json.load(file)
#check user_id
for user in data["users"]:
if user == user_id:
return True
else:
pass
return False
def update_receivers(self):
"Update the users.json file with newly sent message user Ids"
file = open(f'{cwd}/users.json')
data = json.load(file)
for each in self.receivers:
data["users"].append(each)
with open(f'{cwd}/users.json', 'w') as json_file:
json.dump(data, json_file, indent=4)
def add_about(self):
"Add Bio & User Picture to new Account"
# Add proxy
# import pdb; pdb.set_trace()
self.client.set_proxy(self.get_proxy)
self.client.loop.run_until_complete(
self.client(UpdateProfileRequest(
about="Crypto Investment Expert - www.archoptions.com"
))
)
group = self.client.loop.run_until_complete(
self.client.get_entity('@teleescrowtest')
)
self.client.loop.run_until_complete(
self.client(JoinChannelRequest(group))
)
# self.client.loop.run_until_complete(
# self.client(UploadProfilePhotoRequest(
# file=self.client.upload_file(f"{cwd}/photo.jpeg")
# ))
# )
# self.client.loop.run_until_complete(
# self.client(UploadProfilePhotoRequest(
# f"{cwd}/photo.jpeg"
# )
# )
# self.client.lo
return
def get_user_data(self):
"Return user object"
data = self.client.loop.run_until_complete(
self.client.get_me()
)
return data
def add_account(self):
"Verifies A New User Account"
session = self.session
self.write_to_json(session)
return {
"session" : session
}
async def send_messages(self, target:str, start:int):
"Fetch Participants from group to an array"
try:
group = await self.client.get_entity(target)
# entity = [ print(e) async for e in self.client.iter_participants(target) ]
# print(len(entity))
# self.client.loop.run_until_complete(
# asyncio.sleep(0.5)
# )
await asyncio.sleep(2)
await self.client(JoinChannelRequest(group))
index = 0
async for each in self.client.iter_participants(target):
# await asyncio.sleep(2)
if start > index:
index += 1
pass
else:
user = await self.client.get_entity(each.id)
isExisting = self.check_receiver(each.id)
if each.id not in self.receivers and isExisting == False:
self.receivers.append(each.id)
# # import pdb; pdb.set_trace()
# await self.client.send_file(each.id, f"{cwd}/nyoki.jpg")
for msg in data['message']:
# Write User Filter In Condition
status = str(each.status).split('(')[0]
if each.bot == False and status in ["UserStatusRecently", "UserStatusOnline"]:
if each.username != None:
text = msg.replace("$USERNAME", each.username)
else:
text = msg.replace("$USERNAME", "")
await self.client.send_message(user.id, text)
print(f"Sent To - {user.first_name}")
await asyncio.sleep(0.5)
else:
pass
else:
pass
self.update_receivers()
return True
except Exception as e:
print(e)
print("Failed attempt! You inputed invalid data for this script.")
self.update_receivers()
return False
async def text_spambot(self):
try:
await self.client.send_message(
'@SpamBot',
"/start"
)
except Exception as e:
print(e)
print("No Messaging")
async def request_handler(self, event):
print(event)
try:
sender = event.message.peer_id
message = event.message
print(f"New Message Alert - {message}")
# if event.out == False and sender in self.receivers:
if event.message.out == False:
# import pdb; pdb.set_trace()
await self.client.forward_messages(
'crytoexpert',
message.id,
sender
)
# await self.client.send_message(
# sender,
# "Please contact @codefred for more details and resources to help you get started."
# )
# self.receivers.remove(sender)
else:
pass
except AttributeError:
try:
print(event.message.message)
except AttributeError:
pass
except Exception as e:
print(e)
def run(self):
"Runs The Listening Handler"
self.client.add_event_handler(self.request_handler)
print(".....")
self.client.run_until_disconnected()