-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathutils.py
102 lines (83 loc) · 2.94 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
from threading import Thread
import time
from app import process_list, clients_list
import logging
from uuid import uuid4
from telethon import TelegramClient
from database import Storage
import time
logging.basicConfig(level=logging.DEBUG)
def make_request(phone):
clients_list[phone].connect()
clients_list[phone].send_code_request(phone)
clients_list[phone].disconnect()
def request_sign_in(phone):
# request code in another thread, because sometimes it got stuck for no reason
p = Thread(target=make_request, args=(phone, ))
p.start()
def send_msg(client, user, message):
res = False
try:
client.send_message(user, message)
print ('Message sent to {}'.format(user))
res = True
except Exception as e:
print ('Trouble while sending message to {}'.format(user))
print (e)
finally:
return res
def start_spam(accounts, user_list, interval, message):
s = Storage()
keys = s.get_api_keys()
# get all available accounts
clients = [TelegramClient(acc['session_id'], keys['api_id'], keys['api_hash']) for acc in accounts]
# iterate over all accounts
# save only working clients (they may be banned for some reasond)
on_clients = []
for idx, client in enumerate(clients):
try:
#try connecting account
client.connect()
on_clients.append(client)
print ('Client {} connected.'.format(idx))
print ('Client authorized: {}'.format(client.is_user_authorized()))
except Exception as e:
print ("Client {} can't connect: ".format(idx))
print (e)
# try one more time
try:
client.connect()
on_clients.append(client)
print ('Client {} connected.'.format(idx))
print ('Client authorized: {}'.format(client.is_user_authorized()))
except Exception as e:
# if
print ("Client {} can't connect: ".format(idx))
for idx, user in enumerate(user_list):
not_edited = user
if user[0] == '@':
# check if it is username, not phone number
user = user[1:]
try:
# send message from client N
if send_msg(on_clients[idx % len(accounts)], user, message):
s.user_invoiced(message, not_edited)
except Exception as e:
print (e) # set logger later
time.sleep(interval)
generate_report()
for client in on_clients:
try:
client.disconnect()
except Exception as e:
print (e) # set logger later
def generate_report():
now = time.strftime("%c")
s = Storage()
data = next(s.get_spam_jobs())
print (data)
filename = './tmp/{}.txt'.format(now)
with open(filename, 'w') as f:
for user, delivered in data['delivery'].items():
f.write('{} - {}\n'.format(user, delivered))
return now