File size: 2,149 Bytes
e77b868
b00d2c6
e77b868
bea787a
e77b868
 
bea787a
7ce64c9
bea787a
 
e77b868
 
eceb86e
bea787a
e77b868
bea787a
e77b868
7ce64c9
bea787a
 
 
b00d2c6
bea787a
 
 
 
eceb86e
bea787a
 
 
b00d2c6
bea787a
 
e77b868
bea787a
 
 
e77b868
bea787a
 
 
e77b868
eceb86e
e77b868
bea787a
 
 
 
 
e77b868
bea787a
e77b868
bea787a
 
e77b868
 
bea787a
e77b868
 
 
bea787a
 
eceb86e
e77b868
 
bea787a
69a921f
e77b868
bea787a
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
import os
import gradio as gr
import logging
from multiprocessing import Process
from pyrogram import Client, filters
from io import StringIO
from time import sleep

# ---------- FIXED CONFIG ----------
API_ID = int(os.environ.get("API_ID", "6"))
API_HASH = os.environ.get("API_HASH", "eb06d4abfb49dc3eeb1aeb98ae0f581e")
BOT_TOKEN = os.environ.get("BOT_TOKEN", "7510817339:AAHFZoFzPkUO_-nAwfh9bjY_qHsWMprM2PI")

# ---------- LOGGING ----------
log_stream = StringIO()
logging.basicConfig(stream=log_stream, level=logging.INFO, format="%(asctime)s - %(message)s")
logger = logging.getLogger()

# ---------- BOT FUNCTION ----------
def run_bot():
    app = Client("my_bot", api_id=API_ID, api_hash=API_HASH, bot_token=BOT_TOKEN)

    @app.on_message(filters.command("start") & filters.private)
    async def start_cmd(client, message):
        await message.reply("hi")
        logger.info(f"Replied to /start from user ID: {message.from_user.id}")

    logger.info("Bot started.")
    app.run()
    logger.info("Bot stopped.")

# ---------- PROCESS CONTROL ----------
bot_process = None

def start_bot():
    global bot_process
    if bot_process and bot_process.is_alive():
        return "Bot is already running."
    bot_process = Process(target=run_bot)
    bot_process.start()
    sleep(2)
    return "βœ… Bot started."

def stop_bot():
    global bot_process
    if bot_process and bot_process.is_alive():
        bot_process.terminate()
        bot_process.join()
        logger.info("Bot manually stopped.")
        return "πŸ›‘ Bot stopped."
    return "Bot is not running."

def get_logs():
    return log_stream.getvalue()[-20000:]

# ---------- GRADIO UI ----------
with gr.Blocks() as ui:
    gr.Markdown("## πŸ€– Telegram Bot Control Panel")
    start_btn = gr.Button("πŸš€ Start Bot")
    stop_btn = gr.Button("πŸ›‘ Stop Bot")
    refresh_btn = gr.Button("πŸ”„ Refresh Logs")
    logs_box = gr.Textbox(label="πŸ“œ Bot Logs", lines=25)

    start_btn.click(fn=start_bot, outputs=logs_box)
    stop_btn.click(fn=stop_bot, outputs=logs_box)
    refresh_btn.click(fn=get_logs, outputs=logs_box)

if __name__ == "__main__":
    ui.launch()