File size: 4,329 Bytes
1e0c9d6
 
ba34f84
 
1e0c9d6
 
479d580
1e0c9d6
 
7ce64c9
2142971
479d580
 
1e0c9d6
 
 
 
 
2142971
1e0c9d6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2142971
7d376c9
2142971
 
1e0c9d6
 
eceb86e
1e0c9d6
479d580
1e0c9d6
 
 
b00d2c6
1e0c9d6
 
 
 
 
 
2142971
eceb86e
1e0c9d6
b00d2c6
1e0c9d6
 
2142971
 
 
 
e77b868
1e0c9d6
 
eceb86e
e77b868
1e0c9d6
 
 
 
 
 
 
 
 
 
 
 
 
2142971
1e0c9d6
 
2142971
1e0c9d6
2142971
 
 
1e0c9d6
2142971
 
1e0c9d6
 
2142971
 
479d580
2142971
1e0c9d6
 
 
 
2142971
1e0c9d6
 
 
 
 
2142971
 
479d580
ba34f84
 
2142971
 
 
1e0c9d6
 
2142971
 
1e0c9d6
2142971
1e0c9d6
2142971
 
1e0c9d6
2142971
1e0c9d6
 
 
 
 
 
 
ba34f84
1e0c9d6
 
ba34f84
2142971
479d580
 
 
2142971
ba34f84
eece125
ba34f84
 
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
import gradio as gr
import subprocess
import asyncio
import threading
from queue import Queue, Empty
import os
import time
from telegram import Update
from telegram.ext import ApplicationBuilder, CommandHandler, ContextTypes

BOT_TOKEN = os.environ.get("BOT_TOKEN", "YOUR_BOT_TOKEN")

bot_app = None
bot_thread = None
bot_running = False
log_queue = Queue()
telegram_cmd_logs = []

# Telegram bot handler
async def bash_command(update: Update, context: ContextTypes.DEFAULT_TYPE):
    cmd = ' '.join(context.args)
    if not cmd:
        await update.message.reply_text("Usage: /bash <command>")
        return

    log_queue.put(f"[Telegram] Received command: {cmd}")

    try:
        proc = await asyncio.create_subprocess_shell(
            cmd,
            stdout=asyncio.subprocess.PIPE,
            stderr=asyncio.subprocess.STDOUT,
        )
        stdout, _ = await proc.communicate()
        output = stdout.decode().strip()
        if len(output) > 4000:
            output = output[:4000] + "\n...[truncated]"
    except Exception as e:
        output = f"Error: {e}"

    await update.message.reply_text(f"$ {cmd}\n{output}")
    telegram_cmd_logs.append(f"$ {cmd}\n{output}")
    if len(telegram_cmd_logs) > 50:
        telegram_cmd_logs.pop(0)

def start_bot():
    global bot_app, bot_running, bot_thread
    if bot_running:
        log_queue.put("[Bot] Already running.")
        return

    async def run_bot():
        global bot_app, bot_running
        bot_app = ApplicationBuilder().token(BOT_TOKEN).build()
        bot_app.add_handler(CommandHandler("bash", bash_command))
        bot_running = True
        log_queue.put("[Bot] Bot started.")
        await bot_app.run_polling()

    loop = asyncio.new_event_loop()

    def runner():
        asyncio.set_event_loop(loop)
        try:
            loop.run_until_complete(run_bot())
        except Exception as e:
            log_queue.put(f"[Bot Error] {e}")

    bot_thread = threading.Thread(target=runner, daemon=True)
    bot_thread.start()

def stop_bot():
    global bot_app, bot_running
    if not bot_running:
        log_queue.put("[Bot] Bot is not running.")
        return

    async def shutdown():
        await bot_app.shutdown()
        await bot_app.stop()
        log_queue.put("[Bot] Bot stopped.")

    asyncio.run(shutdown())
    bot_running = False

# Real live terminal generator
def live_terminal(cmd):
    if not cmd.strip():
        yield gr.TextArea.update(value="$ ")
        return
    yield gr.TextArea.update(value=f"$ {cmd}\n")
    process = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True)
    output = f"$ {cmd}\n"
    for line in iter(process.stdout.readline, ''):
        output += line
        yield gr.TextArea.update(value=output)
    process.stdout.close()
    process.wait()
    output += "$ "
    yield gr.TextArea.update(value=output)

# Update log output
def update_log_box():
    logs = []
    try:
        while True:
            logs.append(log_queue.get_nowait())
    except Empty:
        pass
    logs.extend(telegram_cmd_logs[-20:])
    return "\n".join(logs[-20:])

# UI
with gr.Blocks(title="πŸ“š TheMovieProviderBot Interface") as demo:
    gr.Markdown("## πŸ–₯️ Live Terminal + Telegram Bot Interface")

    with gr.Row():
        terminal_output = gr.TextArea(label="πŸ“Ÿ Live Terminal", lines=20)
        cmd_input = gr.Textbox(label="⌨️ Enter Command")
        run_btn = gr.Button("▢️ Run")

    with gr.Row():
        start_btn = gr.Button("πŸ€– Start Telegram Bot")
        stop_btn = gr.Button("πŸ›‘ Stop Telegram Bot")

    log_output = gr.Textbox(label="πŸ“œ Bot Logs", lines=15, interactive=False)

    # Stream terminal output
    run_btn.stream(live_terminal, inputs=cmd_input, outputs=terminal_output)

    # Start/Stop handlers
    def start_click():
        start_bot()
        return update_log_box()

    def stop_click():
        stop_bot()
        return update_log_box()

    start_btn.click(start_click, outputs=log_output)
    stop_btn.click(stop_click, outputs=log_output)

    # Background log auto-updater
    def background_log_updater():
        while True:
            time.sleep(3)
            yield gr.Textbox.update(value=update_log_box())

    demo.load(background_log_updater, outputs=log_output)

demo.launch()