import os import asyncio import re from flask import Flask, render_template, request, jsonify from telethon import TelegramClient from telethon.sessions import StringSession app = Flask(__name__) # --- CONFIG (AS IT IS) --- API_ID = int(os.environ.get("API_ID")) API_HASH = os.environ.get("API_HASH") SESSION_STR = os.environ.get("SESSION_STR") BOT_USERNAME = '@Osint2_info_bot' # --- TELETHON SETUP --- loop = asyncio.new_event_loop() asyncio.set_event_loop(loop) client = TelegramClient(StringSession(SESSION_STR), API_ID, API_HASH, loop=loop) last_msg_obj = None current_instruction = "" # ============================== # ๐Ÿ”ฅ SMART STRUCTURAL CLEANER # ============================== def clean_intel_text(text): if not text: return "" lines = text.split('\n') # --- STEP 1: Remove name between 'Time' and 'STATISTICS' (Lookup Case) --- time_idx = -1 stats_idx = -1 for i, line in enumerate(lines): if "Time:" in line or "โฐ" in line: time_idx = i if "STATISTICS" in line or "๐Ÿ“Š" in line: stats_idx = i break # Pehla Statistics milte hi ruk jao # Agar dono mil gaye, toh beech ki lines uda do if time_idx != -1 and stats_idx != -1 and stats_idx > time_idx + 1: lines = lines[:time_idx + 1] + lines[stats_idx:] # --- STEP 2: Remove line before 'Auto-delete' --- final_lines = [] auto_del_found = False # Hum piche se check karenge taaki last wali line uda sakein for i in range(len(lines)): line = lines[i] if "Auto-delete" in line or "โณ" in line: # Isse pehle wali line agar exist karti hai toh usey remove karo if final_lines: final_lines.pop() # Auto-delete wali line ko bhi skip karo kyunki wo terminal pe nahi chahiye continue final_lines.append(line) cleaned = "\n".join(final_lines) # --- STEP 3: BRANDING REPLACEMENT (AS IT IS) --- # Ye part wahi hai jo hamesha work karta hai cleaned = re.sub(r'(?i)owner:.*', '๐Ÿ‘‘ Owner: @beast_harry', cleaned) cleaned = re.sub(r'@(?!beast_harry)\w+', '@beast_harry', cleaned) cleaned = re.sub(r'(?i)\b(surya|hacker|developer|dev|osint|intel|bot|team)\b', 'Pardhan Ji', cleaned) # Extra design cleanup cleaned = re.sub(r'[โคใ€Žใ€โ—กโ€Œโƒใ…คโ€Œ]+', '', cleaned) return cleaned.strip() # ============================== # ๐Ÿ”ฅ SMART RESPONSE FILTER (AS IT IS) # ============================== async def get_final_response(mode="extraction"): global current_instruction status_indicators = ["fetching", "processing", "please wait", "loading", "checking", "searching", "sending", "requesting", "collecting"] for _ in range(12): await asyncio.sleep(1.5) messages = await client.get_messages(BOT_USERNAME, limit=3) for msg in messages: if not msg.text: continue text_raw = msg.text.strip() text_lower = text_raw.lower() if mode == "instruction": if len(text_raw) > 10 and not any(s in text_lower for s in status_indicators): current_instruction = text_raw return text_raw if mode == "extraction": if any(s in text_lower for s in status_indicators): continue if text_raw == current_instruction: continue if len(text_raw) > 50 or "result" in text_lower or "found" in text_lower: return text_raw return "โŒ System Timeout or No Data Found." # ============================== # ROUTES (NO CHANGES) # ============================== @app.route('/') def index(): return render_template('index.html') @app.route('/initialize', methods=['POST']) def initialize(): global last_msg_obj try: asyncio.set_event_loop(loop) if not client.is_connected(): loop.run_until_complete(client.connect()) async def get_start(): global last_msg_obj await client.send_message(BOT_USERNAME, '/start') for _ in range(10): await asyncio.sleep(1.5) messages = await client.get_messages(BOT_USERNAME, limit=1) if messages and messages[0].text: response = messages[0] if "/start" not in response.text: last_msg_obj = response btn_list = [btn.text for row in response.buttons for btn in row] if response.buttons else [] return {"status": "ready", "buttons": btn_list, "text": clean_intel_text(response.text)} return {"status": "error", "message": "Bot initialization timeout"} return jsonify(loop.run_until_complete(get_start())) except Exception as e: return jsonify({"status": "error", "message": str(e)}) @app.route('/click_module', methods=['POST']) def click_module(): global last_msg_obj btn_text = request.form.get('btn_text') try: asyncio.set_event_loop(loop) async def click_logic(): await last_msg_obj.click(text=btn_text) text = await get_final_response(mode="instruction") return {"status": "waiting_input", "msg": clean_intel_text(text)} return jsonify(loop.run_until_complete(click_logic())) except Exception as e: return jsonify({"status": "error", "message": str(e)}) @app.route('/extract_data', methods=['POST']) def extract_data(): user_data = request.form.get('data') try: asyncio.set_event_loop(loop) async def input_logic(): await client.send_message(BOT_USERNAME, user_data) text = await get_final_response(mode="extraction") return {"status": "success", "result": clean_intel_text(text)} return jsonify(loop.run_until_complete(input_logic())) except Exception as e: return jsonify({"status": "error", "message": str(e)}) if __name__ == '__main__': port = int(os.environ.get('PORT', 8080)) app.run(host='0.0.0.0', port=port)