-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathbot.py
More file actions
119 lines (103 loc) · 4.82 KB
/
bot.py
File metadata and controls
119 lines (103 loc) · 4.82 KB
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
import asyncio
from getpass import getpass
from json import load
from asyncio import run
from os import _exit
from re import search
from telethon.sync import TelegramClient
from telethon.errors import (
SessionPasswordNeededError, UserBannedInChannelError, MediaEmptyError, ChatWriteForbiddenError
)
class Bot:
def __init__(self, phone_number: str, api_id: int, api_hash: str) -> None:
self.api_id: int = api_id
self.api_hash: str = api_hash
self.phone_number: str = phone_number
self.client: TelegramClient = TelegramClient(
self.phone_number,
api_id=self.api_id,
api_hash=self.api_hash
)
self.status: bool = True
self.messages_delay: int = 300 # delay between each message
self.groups_delay: int = 12000 # how often to send messages
def load_json_file(self, file_name: str) -> object:
try:
with open(f'assets/{file_name}', 'rb') as file:
return load(file)
except Exception:
print(f"[-] Error: File {file_name} not found.")
return {}
async def login(self) -> bool:
try:
await self.client.connect()
if not await self.client.is_user_authorized():
try:
await self.client.send_code_request(
str(self.phone_number)
)
await self.client.sign_in(
self.phone_number,
input("[?] Access code: ")
)
except SessionPasswordNeededError:
await self.client.sign_in(password=getpass())
return True
except Exception as e:
print(f"[-] Login failed due to {type(e).__name__}: {str(e)}")
return False
async def send_message(self, local_chat_id):
local_chat_entity = await self.client.get_entity(local_chat_id)
for message_id, channel_ids in self.data[local_chat_id].items():
try:
text = await self.client.get_messages(
local_chat_entity,
ids=int(message_id)
)
for channel_id in channel_ids:
try:
# check if you are meant to reply to a topic or not
match = search(r'https://t.me/[^/]+/(\d+)', channel_id)
if match:
channel_entity = await self.client.get_entity(channel_id.split('/' + match.group(1))[0])
await self.client.send_message(channel_entity, text, reply_to=int(match.group(1)))
else:
channel_entity = await self.client.get_entity(int(channel_id))
await self.client.send_message(channel_entity, text)
print(f"[+] Successfully forwarded {local_chat_id}{message_id} to {channel_id}!")
except ChatWriteForbiddenError as e:
print(f"[-] {channel_id} [ChatWriteForbiddenError]: You can't write in this chat.")
except UserBannedInChannelError as e:
print(f"[-] You have been banned from sending messages to groups/supergroups until {e.time}!")
return
except MediaEmptyError as e:
print(f"[-] MediaEmptyError: The provided media object is invalid or the account may not be able to send it.")
except Exception as e:
print(f"[-] {local_chat_id} [{type(e).__name__}]: {str(e)}")
pass
except KeyboardInterrupt:
return
except Exception as e:
print(f"[-] {local_chat_id} [{type(e).__name__}]: {str(e)}")
await asyncio.sleep(self.messages_delay)
async def main(self) -> None:
if not await self.login():
print(f"[-] Exiting the program...")
return
while self.status:
try:
self.data: object = self.load_json_file("data.json")
for chat_id in self.data:
await self.send_message(chat_id)
print(f"[+] Successfully forwarded all messages!\n\n")
await asyncio.sleep(self.groups_delay)
except KeyboardInterrupt:
self.status = False
break
except Exception as e:
print(f"An issue occurred [{type(e).__name__}]: {str(e)}")
continue
_exit(-3)
if __name__ == "__main__":
bot = Bot("+<phone_number>", 999999, "api_hash") # replace with your credentials
run(bot.main())