This repository has been archived by the owner on Jul 2, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
msgbot.py
79 lines (72 loc) · 2.33 KB
/
msgbot.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
from telethon.sync import TelegramClient
from telethon.tl.types import InputPeerUser
from telethon.errors.rpcerrorlist import PeerFloodError
import sys
import csv
import random
import time
auth = []
with open(user_auth.csv, encoding='UTF-8') as f:
rows = csv.reader(f,delimiter=",",lineterminator="\n")
next(rows, None)
for row in rows:
auth_lst= {}
auth_lst['api_id'] = int(row[0])
auth_lst['api_hash'] = row[1]
auth_lst['phone'] = row[2]
auth.append(auth_lst)
api_id = auth['api_id']
api_hash = auth_lst['api_hash']
phone = auth_lst['phone']
client.start(phone)
SLEEP_TIME = int(input("Enter Delay Timing For Per Message Sending : "))
os.system('clear')
client = TelegramClient('anon', api_id, api_hash)
client.connect()
if not client.is_user_authorized():
client.send_code_request(phone)
client.sign_in(phone, input('Enter the code: '))
input_file = sys.argv[1]
users = []
with open(input_file, encoding='UTF-8') as f:
rows = csv.reader(f,delimiter=",",lineterminator="\n")
next(rows, None)
for row in rows:
user = {}
user['username'] = row[0]
user['id'] = int(row[1])
user['access_hash'] = int(row[2])
user['name'] = row[3]
users.append(user)
mode = int(input("Enter 1 to send by user ID or 2 to send by username: "))
messages= ""
with open('message.txt') as f:
messages = messages
messages = "".join(f.readlines())
for user in users:
if mode == 2:
if user['username'] == "":
continue
receiver = client.get_input_entity(user['username'])
elif mode == 1:
receiver = InputPeerUser(user['id'],user['access_hash'])
else:
print("Invalid Mode. Exiting.")
client.disconnect()
sys.exit()
message = messages
try:
print("Sending Message to:", user['name'])
client.send_message(receiver, message.format(user['name']))
print("Waiting {} seconds".format(SLEEP_TIME))
time.sleep(SLEEP_TIME)
except PeerFloodError:
print("Getting Flood Error from telegram. Script is stopping now. Please try again after some time.")
client.disconnect()
sys.exit()
except Exception as e:
print("Error:", e)
print("Trying to continue...")
continue
client.disconnect()
print("Done. Message sent to all users.")