Update main.py
Browse files
main.py
CHANGED
|
@@ -6,9 +6,7 @@ from telethon import TelegramClient, events
|
|
| 6 |
# ---------------- CONFIG ----------------
|
| 7 |
API_ID = 22138159
|
| 8 |
API_HASH = "3fe4592e4cad72f366b6c564505f2d57"
|
| 9 |
-
|
| 10 |
MASTER_BOT_SESSION = "master.session"
|
| 11 |
-
SUPER_ADMIN_ID = 7205748624
|
| 12 |
LOG_CHANNEL_ID = -1002524453831
|
| 13 |
# ---------------------------------------
|
| 14 |
|
|
@@ -17,105 +15,81 @@ logger = logging.getLogger(__name__)
|
|
| 17 |
|
| 18 |
master = TelegramClient(MASTER_BOT_SESSION, API_ID, API_HASH)
|
| 19 |
|
| 20 |
-
# ---------- REGEX ----------
|
| 21 |
LINK_WITH_ID = r"https?://t\.me/([a-zA-Z0-9_]+)/(\d+)"
|
| 22 |
-
LINK_ONLY = r"https?://t\.me/([a-zA-Z0-9_]+)"
|
| 23 |
USER_WITH_ID = r"@?([a-zA-Z0-9_]+)\s+(\d+)"
|
|
|
|
| 24 |
USER_ONLY = r"^@?([a-zA-Z0-9_]{5,})$"
|
| 25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
|
| 27 |
|
| 28 |
@master.on(events.NewMessage)
|
| 29 |
async def convert_links(event):
|
| 30 |
text = event.raw_text.strip()
|
| 31 |
-
matches = []
|
| 32 |
-
|
| 33 |
-
matches += re.findall(LINK_WITH_ID, text)
|
| 34 |
-
matches += re.findall(USER_WITH_ID, text)
|
| 35 |
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
no_id_users.add(u)
|
| 39 |
-
for u in re.findall(USER_ONLY, text):
|
| 40 |
-
no_id_users.add(u)
|
| 41 |
|
| 42 |
-
if not
|
| 43 |
return
|
| 44 |
|
| 45 |
processing = None
|
| 46 |
-
if len(
|
| 47 |
-
processing = await event.reply("β³ Processing
|
| 48 |
|
| 49 |
-
|
| 50 |
|
| 51 |
-
# ---
|
| 52 |
-
for username, msg_id in
|
| 53 |
try:
|
| 54 |
entity = await master.get_entity(username)
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
private_id = abs(entity.id) - 1000000000000
|
| 58 |
-
private_link = f"https://t.me/c/{private_id}/{msg_id}"
|
| 59 |
|
| 60 |
-
|
| 61 |
-
f"π **{
|
| 62 |
-
f"π `{
|
| 63 |
)
|
| 64 |
except Exception:
|
| 65 |
-
|
| 66 |
|
| 67 |
-
# ---
|
| 68 |
-
for username in
|
| 69 |
try:
|
| 70 |
entity = await master.get_entity(username)
|
| 71 |
-
|
|
|
|
|
|
|
| 72 |
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
results.append(
|
| 82 |
-
f"π **{chat_name}** (last post)\n"
|
| 83 |
-
f"π `{private_link}`\n"
|
| 84 |
)
|
| 85 |
except Exception:
|
| 86 |
-
|
| 87 |
|
| 88 |
-
|
| 89 |
|
| 90 |
if processing:
|
| 91 |
-
await processing.edit(
|
| 92 |
else:
|
| 93 |
-
await event.reply(
|
| 94 |
-
|
| 95 |
-
await master.send_message(
|
| 96 |
-
LOG_CHANNEL_ID,
|
| 97 |
-
"π **Link Conversion Log**\n\n" + "\n".join(results)
|
| 98 |
-
)
|
| 99 |
-
|
| 100 |
|
| 101 |
-
|
| 102 |
-
async def load_registered_bots():
|
| 103 |
-
while True:
|
| 104 |
-
await asyncio.sleep(60)
|
| 105 |
-
|
| 106 |
-
async def cleanup_idle_clients():
|
| 107 |
-
while True:
|
| 108 |
-
await asyncio.sleep(120)
|
| 109 |
-
# --------------------------------------------
|
| 110 |
|
| 111 |
|
| 112 |
async def main():
|
| 113 |
await master.start()
|
| 114 |
logger.info("β
Master started")
|
| 115 |
-
|
| 116 |
-
asyncio.create_task(load_registered_bots())
|
| 117 |
-
asyncio.create_task(cleanup_idle_clients())
|
| 118 |
-
|
| 119 |
await master.run_until_disconnected()
|
| 120 |
|
| 121 |
|
|
|
|
| 6 |
# ---------------- CONFIG ----------------
|
| 7 |
API_ID = 22138159
|
| 8 |
API_HASH = "3fe4592e4cad72f366b6c564505f2d57"
|
|
|
|
| 9 |
MASTER_BOT_SESSION = "master.session"
|
|
|
|
| 10 |
LOG_CHANNEL_ID = -1002524453831
|
| 11 |
# ---------------------------------------
|
| 12 |
|
|
|
|
| 15 |
|
| 16 |
master = TelegramClient(MASTER_BOT_SESSION, API_ID, API_HASH)
|
| 17 |
|
|
|
|
| 18 |
LINK_WITH_ID = r"https?://t\.me/([a-zA-Z0-9_]+)/(\d+)"
|
|
|
|
| 19 |
USER_WITH_ID = r"@?([a-zA-Z0-9_]+)\s+(\d+)"
|
| 20 |
+
LINK_ONLY = r"https?://t\.me/([a-zA-Z0-9_]+)"
|
| 21 |
USER_ONLY = r"^@?([a-zA-Z0-9_]{5,})$"
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
def get_private_id(chat_id: int) -> int:
|
| 25 |
+
chat_id = abs(chat_id)
|
| 26 |
+
if chat_id > 10**12:
|
| 27 |
+
return chat_id - 1000000000000
|
| 28 |
+
return chat_id
|
| 29 |
|
| 30 |
|
| 31 |
@master.on(events.NewMessage)
|
| 32 |
async def convert_links(event):
|
| 33 |
text = event.raw_text.strip()
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
|
| 35 |
+
with_id = re.findall(LINK_WITH_ID, text) + re.findall(USER_WITH_ID, text)
|
| 36 |
+
only_user = set(re.findall(LINK_ONLY, text) + re.findall(USER_ONLY, text))
|
|
|
|
|
|
|
|
|
|
| 37 |
|
| 38 |
+
if not with_id and not only_user:
|
| 39 |
return
|
| 40 |
|
| 41 |
processing = None
|
| 42 |
+
if len(with_id) + len(only_user) > 1:
|
| 43 |
+
processing = await event.reply("β³ Processing...")
|
| 44 |
|
| 45 |
+
out = []
|
| 46 |
|
| 47 |
+
# --- Provided message ID ---
|
| 48 |
+
for username, msg_id in with_id:
|
| 49 |
try:
|
| 50 |
entity = await master.get_entity(username)
|
| 51 |
+
private_id = get_private_id(entity.id)
|
| 52 |
+
name = getattr(entity, "title", None) or f"@{entity.username}"
|
|
|
|
|
|
|
| 53 |
|
| 54 |
+
out.append(
|
| 55 |
+
f"π **{name}**\n"
|
| 56 |
+
f"π `https://t.me/c/{private_id}/{msg_id}`\n"
|
| 57 |
)
|
| 58 |
except Exception:
|
| 59 |
+
out.append(f"β Failed: `{username} {msg_id}`")
|
| 60 |
|
| 61 |
+
# --- Auto last post ---
|
| 62 |
+
for username in only_user:
|
| 63 |
try:
|
| 64 |
entity = await master.get_entity(username)
|
| 65 |
+
last = await master.get_messages(entity, limit=1)
|
| 66 |
+
if not last:
|
| 67 |
+
raise Exception
|
| 68 |
|
| 69 |
+
msg_id = last[0].id
|
| 70 |
+
private_id = get_private_id(entity.id)
|
| 71 |
+
name = getattr(entity, "title", None) or f"@{entity.username}"
|
| 72 |
|
| 73 |
+
out.append(
|
| 74 |
+
f"π **{name}** (last post)\n"
|
| 75 |
+
f"π `https://t.me/c/{private_id}/{msg_id}`\n"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
)
|
| 77 |
except Exception:
|
| 78 |
+
out.append(f"β Failed last post: `{username}`")
|
| 79 |
|
| 80 |
+
result = "π **Private Links**\n\n" + "\n".join(out)
|
| 81 |
|
| 82 |
if processing:
|
| 83 |
+
await processing.edit(result)
|
| 84 |
else:
|
| 85 |
+
await event.reply(result)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 86 |
|
| 87 |
+
await master.send_message(LOG_CHANNEL_ID, result)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 88 |
|
| 89 |
|
| 90 |
async def main():
|
| 91 |
await master.start()
|
| 92 |
logger.info("β
Master started")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 93 |
await master.run_until_disconnected()
|
| 94 |
|
| 95 |
|