File size: 5,206 Bytes
c83166f c52c85f c83166f |
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 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 |
import os
import json
import string
import random
import subprocess
import threading
import time
import requests
from flask import Flask
import telebot
from telebot import types
# === CONFIG ===
BOT_TOKEN = "8018538301:AAGRSngj4Bj5nqizPsU3Zux48Yr6fpaZ7To" # π Replace this with your Telegram Bot Token
SCRIPT_DIR = "scripts"
DB_FILE = "scripts.json"
PORT = int(os.environ.get('PORT', 7860))
PING_URL = "https://dexsecon-yobot.hf.space/ping" # β
Auto-ping URL (your space)
# === INIT ===
os.makedirs(SCRIPT_DIR, exist_ok=True)
app = Flask(__name__)
bot = telebot.TeleBot(BOT_TOKEN)
file_upload_mode = {}
status_map = {}
# === UTILITIES ===
def generate_filename():
return ''.join(random.choices(string.ascii_letters + string.digits, k=6)) + ".py"
def load_db():
if not os.path.exists(DB_FILE):
with open(DB_FILE, "w") as f:
json.dump({}, f)
with open(DB_FILE, "r") as f:
return json.load(f)
def save_db(data):
with open(DB_FILE, "w") as f:
json.dump(data, f, indent=4)
def update_status(filename, proc):
stdout, stderr = proc.communicate()
if proc.returncode == 0:
status_map[filename] = 'sleep'
else:
status_map[filename] = 'error'
def auto_ping():
while True:
try:
requests.get(PING_URL)
print("β
Ping sent to HF")
except Exception as e:
print(f"β Ping failed: {e}")
time.sleep(300) # 5 minutes
# === TELEGRAM COMMANDS ===
@bot.message_handler(commands=['start'])
def start_message(message):
markup = types.ReplyKeyboardMarkup(resize_keyboard=True)
markup.add('π₯ Upload Script', 'π My Scripts')
markup.add('π Status', 'π Refresh')
bot.send_message(message.chat.id, "π Welcome to XOOM Bot!", reply_markup=markup)
@bot.message_handler(commands=['help'])
def help_message(message):
bot.send_message(message.chat.id, "Available:\n/start β Menu\n/help β Help info\n/status β Status of your scripts\n/myscripts β Show your scripts")
@bot.message_handler(commands=['status'])
def status_all(message):
chat_id = str(message.chat.id)
db = load_db()
scripts = db.get(chat_id, [])
counts = {"running": 0, "error": 0, "sleep": 0}
for s in scripts:
stat = status_map.get(s, 'unknown')
if stat in counts:
counts[stat] += 1
msg = f"π Status:\nRunning: {counts['running']} π\nDone: {counts['sleep']} β
\nError: {counts['error']} β"
bot.send_message(message.chat.id, msg)
@bot.message_handler(commands=['myscripts'])
def my_scripts(message):
chat_id = str(message.chat.id)
db = load_db()
scripts = db.get(chat_id, [])
if not scripts:
bot.send_message(message.chat.id, "π No scripts found.")
return
msg = "π Your Scripts:\n"
for s in scripts:
stat = status_map.get(s, 'unknown')
icon = {'running': 'π', 'sleep': 'β
', 'error': 'β'}.get(stat, 'β')
msg += f"{icon} {s} - {stat}\n"
bot.send_message(message.chat.id, msg)
# === BUTTON HANDLERS ===
@bot.message_handler(func=lambda m: m.text == 'π₯ Upload Script')
def upload_script_btn(message):
bot.send_message(message.chat.id, "π Send your .py file now.")
file_upload_mode[message.chat.id] = True
@bot.message_handler(func=lambda m: m.text == 'π My Scripts')
def my_scripts_btn(message):
my_scripts(message)
@bot.message_handler(func=lambda m: m.text == 'π Status')
def status_btn(message):
status_all(message)
@bot.message_handler(func=lambda m: m.text == 'π Refresh')
def refresh_btn(message):
start_message(message)
# === FILE HANDLER ===
@bot.message_handler(content_types=['document'])
def handle_file(message):
if message.chat.id not in file_upload_mode:
return
file_info = bot.get_file(message.document.file_id)
if not file_info.file_path.endswith(".py"):
bot.send_message(message.chat.id, "β Only .py files allowed.")
return
file_data = bot.download_file(file_info.file_path)
filename = generate_filename()
filepath = os.path.join(SCRIPT_DIR, filename)
with open(filepath, "wb") as f:
f.write(file_data)
chat_id = str(message.chat.id)
db = load_db()
db.setdefault(chat_id, []).append(filename)
save_db(db)
try:
proc = subprocess.Popen(["python3", filepath], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
status_map[filename] = 'running'
bot.send_message(message.chat.id, f"β
Script `{filename}` is running.")
threading.Thread(target=lambda: update_status(filename, proc)).start()
except Exception as e:
status_map[filename] = 'error'
bot.send_message(message.chat.id, f"β Failed: {e}")
file_upload_mode.pop(message.chat.id, None)
# === FLASK ROUTES ===
@app.route('/')
def home():
return "β
XOOM Bot Running - dexsecon/xoomhost"
@app.route('/ping')
def ping():
return "β
Ping OK"
# === START EVERYTHING ===
threading.Thread(target=lambda: bot.infinity_polling(), daemon=True).start()
threading.Thread(target=auto_ping, daemon=True).start()
if __name__ == '__main__':
app.run(host='0.0.0.0', port=PORT) |