-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTelegramController.py
More file actions
1160 lines (1028 loc) · 53.1 KB
/
TelegramController.py
File metadata and controls
1160 lines (1028 loc) · 53.1 KB
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
Telegram PC Controller - All in One
By Nothing-dot-exe (https://github.com/Nothing-dot-exe)
Contains both the GUI Manager and the Telegram Bot backend in a single file.
Configuration is stored in config.json (managed via GUI).
Setup:
1. pip install -r requirements.txt
2. python TelegramController.py
3. Enter Bot Token and Chat ID in the GUI, then click Start.
"""
import os
import sys
import io
import socket
import asyncio
import subprocess
import threading
import platform
import logging
import winreg
from datetime import datetime
import psutil
import pyautogui
import requests
import tkinter as tk
from tkinter import font as tkfont
from telegram import Update, InlineKeyboardButton, InlineKeyboardMarkup
from telegram.ext import (
ApplicationBuilder,
CommandHandler,
CallbackQueryHandler,
ContextTypes,
)
# ╔══════════════════════════════════════════════════════════════╗
# ║ CONFIGURATION ║
# ╚══════════════════════════════════════════════════════════════╝
import json
if getattr(sys, 'frozen', False):
SCRIPT_DIR = os.path.dirname(sys.executable)
else:
SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__))
CONFIG_FILE = os.path.join(SCRIPT_DIR, "config.json")
def load_config():
if os.path.exists(CONFIG_FILE):
try:
with open(CONFIG_FILE, "r") as f:
return json.load(f)
except: pass
return {"BOT_TOKEN": "", "AUTHORIZED_USER_ID": ""}
def save_config(token, user_id):
with open(CONFIG_FILE, "w") as f:
json.dump({
"_README": "Telegram PC Controller Configuration",
"_BOT_TOKEN_help": "Get this from @BotFather on Telegram (/newbot)",
"BOT_TOKEN": token,
"_AUTHORIZED_USER_ID_help": "Your Telegram User ID (get it from @RawDataBot or /myid command)",
"AUTHORIZED_USER_ID": user_id,
"_author": "Nothing-dot-exe (https://github.com/Nothing-dot-exe)"
}, f, indent=4)
cfg = load_config()
BOT_TOKEN = cfg.get("BOT_TOKEN", "").strip()
AUTHORIZED_USER_ID = cfg.get("AUTHORIZED_USER_ID", "")
try: AUTHORIZED_USER_ID = int(AUTHORIZED_USER_ID)
except: AUTHORIZED_USER_ID = 0
# ══════════════════════════════════════════════════════════════
THIS_SCRIPT = os.path.abspath(__file__)
APP_NAME = "TelegramPCController"
# Force python.exe instead of pythonw.exe for subprocess stability
PYTHON_EXE = sys.executable.replace("pythonw.exe", "python.exe")
logging.basicConfig(
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s",
level=logging.INFO,
)
logger = logging.getLogger(__name__)
pending_actions = {}
# =====================================================================
# BOT BACKEND LOGIC
# =====================================================================
def authorized(func):
async def wrapper(update: Update, context: ContextTypes.DEFAULT_TYPE):
user_id = update.effective_user.id
if user_id != AUTHORIZED_USER_ID:
await update.message.reply_text("⛔ Unauthorized. Access denied.")
logger.warning(f"Unauthorized access attempt by {user_id}")
return
return await func(update, context)
return wrapper
async def cmd_myid(update: Update, context: ContextTypes.DEFAULT_TYPE):
user = update.effective_user
await update.message.reply_text(
f"👤 *Your Telegram Info*\n"
f"━━━━━━━━━━━━━━━━━━━━━━\n"
f"🆔 *User ID:* `{user.id}`\n"
f"📛 *Name:* {user.first_name}\n"
f"━━━━━━━━━━━━━━━━━━━━━━\n\n"
f"📋 Copy the User ID above and set it as\n"
f"`AUTHORIZED_USER_ID` in the bot script.",
parse_mode="Markdown",
)
@authorized
async def cmd_start(update: Update, context: ContextTypes.DEFAULT_TYPE):
help_text = """
🖥️ *Telegram PC Controller*
━━━━━━━━━━━━━━━━━━━━━━
🔧 *System Control*
/screenshot — Capture screen
/webcam — Capture webcam photo
/lock — Lock the PC
/shutdown — Shutdown (30s cancel)
/restart — Restart (30s cancel)
/sleep — Put PC to sleep
/logoff — Log off current user
/cancel — Cancel shutdown/restart
📊 *Monitoring*
/status — CPU, RAM, Disk, Battery
/processes — Top processes by CPU
/ip — Local & public IP
/drives — List all drives
/uptime — System uptime
/wifi — Current WiFi info
📁 *Apps & Files*
/tasklist — Running apps
/kill `<name>` — Kill a process
/open `<path>` — Open file/app
/cmd `<command>` — Run shell command
/download `<url>` — Download file to PC
/sendfile `<path>` — Send file to Telegram
/browse `<url>` — Open URL in browser
🔊 *Media & Display*
/volume `<0-100>` — Set volume
/mute — Toggle mute
/brightness `<0-100>` — Set brightness
/media `<play/pause/next/prev>` — Media keys
/say `<text>` — Text-to-speech on PC
💬 *Utilities*
/alert `<msg>` — Popup on PC
/clipboard — Get clipboard
/setclip `<text>` — Set clipboard
/note `<text>` — Save note to Desktop
/myid — Show your Telegram ID
💻 *Author*
Made by Nothing-dot-exe
GitHub: [Nothing-dot-exe](https://github.com/Nothing-dot-exe)
━━━━━━━━━━━━━━━━━━━━━━
🟢 Bot is online and ready!
"""
await update.message.reply_text(help_text, parse_mode="Markdown")
@authorized
async def cmd_screenshot(update: Update, context: ContextTypes.DEFAULT_TYPE):
await update.message.reply_text("📸 Capturing screen...")
try:
screenshot = pyautogui.screenshot()
buf = io.BytesIO()
screenshot.save(buf, format="PNG")
buf.seek(0)
await update.message.reply_photo(photo=buf, caption=f"🖥️ Screenshot — {datetime.now().strftime('%H:%M:%S')}")
except Exception as e:
await update.message.reply_text(f"❌ Screenshot failed: {e}")
@authorized
async def cmd_webcam(update: Update, context: ContextTypes.DEFAULT_TYPE):
await update.message.reply_text("📷 Capturing webcam...")
try:
import cv2
cap = cv2.VideoCapture(0)
if not cap.isOpened():
await update.message.reply_text("❌ No webcam found.")
return
ret, frame = cap.read()
cap.release()
if not ret:
await update.message.reply_text("❌ Failed to capture webcam.")
return
_, buf_arr = cv2.imencode(".jpg", frame)
buf = io.BytesIO(buf_arr.tobytes())
buf.seek(0)
await update.message.reply_photo(photo=buf, caption=f"📷 Webcam — {datetime.now().strftime('%H:%M:%S')}")
except ImportError:
await update.message.reply_text("❌ opencv-python not installed.\nRun: `pip install opencv-python`", parse_mode="Markdown")
except Exception as e:
await update.message.reply_text(f"❌ Webcam error: {e}")
@authorized
async def cmd_lock(update: Update, context: ContextTypes.DEFAULT_TYPE):
await update.message.reply_text("🔒 Locking PC...")
try:
subprocess.Popen("rundll32.exe user32.dll,LockWorkStation")
await update.message.reply_text("✅ PC locked.")
except Exception as e:
await update.message.reply_text(f"❌ Failed to lock: {e}")
@authorized
async def cmd_shutdown(update: Update, context: ContextTypes.DEFAULT_TYPE):
keyboard = [[InlineKeyboardButton("❌ Cancel Shutdown", callback_data="cancel_shutdown")]]
reply_markup = InlineKeyboardMarkup(keyboard)
msg = await update.message.reply_text(
"⚠️ *PC will shutdown in 30 seconds!*\nPress the button below to cancel.",
parse_mode="Markdown", reply_markup=reply_markup
)
job = context.application.job_queue.run_once(execute_shutdown, 30, data=msg.message_id, name="shutdown_job")
pending_actions["shutdown"] = job
async def execute_shutdown(context: ContextTypes.DEFAULT_TYPE):
pending_actions.pop("shutdown", None)
subprocess.Popen("shutdown /s /t 0", shell=True)
@authorized
async def cmd_restart(update: Update, context: ContextTypes.DEFAULT_TYPE):
keyboard = [[InlineKeyboardButton("❌ Cancel Restart", callback_data="cancel_restart")]]
reply_markup = InlineKeyboardMarkup(keyboard)
msg = await update.message.reply_text(
"⚠️ *PC will restart in 30 seconds!*\nPress the button below to cancel.",
parse_mode="Markdown", reply_markup=reply_markup
)
job = context.application.job_queue.run_once(execute_restart, 30, data=msg.message_id, name="restart_job")
pending_actions["restart"] = job
async def execute_restart(context: ContextTypes.DEFAULT_TYPE):
pending_actions.pop("restart", None)
subprocess.Popen("shutdown /r /t 0", shell=True)
@authorized
async def cmd_logoff(update: Update, context: ContextTypes.DEFAULT_TYPE):
await update.message.reply_text("🚪 Logging off...")
try:
subprocess.Popen("shutdown /l", shell=True)
except Exception as e:
await update.message.reply_text(f"❌ Failed: {e}")
async def cancel_callback(update: Update, context: ContextTypes.DEFAULT_TYPE):
query = update.callback_query
if query.from_user.id != AUTHORIZED_USER_ID:
await query.answer("⛔ Unauthorized.")
return
await query.answer()
if query.data == "cancel_shutdown" and "shutdown" in pending_actions:
pending_actions["shutdown"].schedule_removal()
pending_actions.pop("shutdown")
await query.edit_message_text("✅ Shutdown cancelled.")
elif query.data == "cancel_restart" and "restart" in pending_actions:
pending_actions["restart"].schedule_removal()
pending_actions.pop("restart")
await query.edit_message_text("✅ Restart cancelled.")
else:
await query.edit_message_text("ℹ️ No pending action to cancel.")
@authorized
async def cmd_cancel(update: Update, context: ContextTypes.DEFAULT_TYPE):
cancelled = []
for key in list(pending_actions.keys()):
pending_actions[key].schedule_removal()
pending_actions.pop(key)
cancelled.append(key)
if cancelled:
await update.message.reply_text(f"✅ Cancelled: {', '.join(cancelled)}")
else:
await update.message.reply_text("ℹ️ No pending actions to cancel.")
@authorized
async def cmd_sleep(update: Update, context: ContextTypes.DEFAULT_TYPE):
await update.message.reply_text("😴 Putting PC to sleep...")
try:
subprocess.Popen("rundll32.exe powrprof.dll,SetSuspendState 0,1,0")
except Exception as e:
await update.message.reply_text(f"❌ Failed: {e}")
@authorized
async def cmd_status(update: Update, context: ContextTypes.DEFAULT_TYPE):
try:
cpu = psutil.cpu_percent(interval=1)
mem = psutil.virtual_memory()
disk = psutil.disk_usage("/")
b_time = datetime.fromtimestamp(psutil.boot_time())
up = datetime.now() - b_time
bat = psutil.sensors_battery()
bat_info = "🔌 No battery (Desktop)"
if bat:
plug = "🔌 Plugged in" if bat.power_plugged else "🔋 On battery"
bat_info = f"{plug} — {bat.percent}%"
h, rem = divmod(int(up.total_seconds()), 3600)
m, _ = divmod(rem, 60)
text = f"📊 *System Status*\n━━━━━━━━━━━━━━━━━━━━━━\n🖥️ *PC:* {platform.node()}\n💻 *OS:* {platform.system()} {platform.release()}\n⚡ *CPU:* {cpu}%\n🧠 *RAM:* {mem.percent}% ({mem.used // (1024**3)}/{mem.total // (1024**3)} GB)\n💾 *Disk:* {disk.percent}% ({disk.used // (1024**3)}/{disk.total // (1024**3)} GB)\n{bat_info}\n⏱️ *Uptime:* {h}h {m}m\n━━━━━━━━━━━━━━━━━━━━━━"
await update.message.reply_text(text, parse_mode="Markdown")
except Exception as e:
await update.message.reply_text(f"❌ Error: {e}")
@authorized
async def cmd_processes(update: Update, context: ContextTypes.DEFAULT_TYPE):
try:
procs = []
for p in psutil.process_iter(["pid", "name", "cpu_percent", "memory_percent"]):
try:
if p.info: procs.append(p.info)
except: pass
procs.sort(key=lambda x: x.get("cpu_percent", 0) or 0, reverse=True)
lines = ["📊 *Top Processes (by CPU)*", "━━━━━━━━━━━━━━━━━━━━━━"]
for p in procs[:15]:
n = (p["name"] or "Unknown")[:20]
c = p.get("cpu_percent", 0) or 0
m = p.get("memory_percent", 0) or 0
lines.append(f"`{n:<20}` CPU: {c:.1f}% | RAM: {m:.1f}%")
await update.message.reply_text("\n".join(lines), parse_mode="Markdown")
except Exception as e:
await update.message.reply_text(f"❌ Error: {e}")
@authorized
async def cmd_ip(update: Update, context: ContextTypes.DEFAULT_TYPE):
try:
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.connect(("8.8.8.8", 80))
lip = s.getsockname()[0]
s.close()
try: pip = requests.get("https://api.ipify.org", timeout=5).text
except: pip = "Could not determine"
text = f"🌐 *Network Info*\n━━━━━━━━━━━━━━━━━━━━━━\n🏠 *Local IP:* `{lip}`\n🌍 *Public IP:* `{pip}`\n💻 *Hostname:* `{socket.gethostname()}`\n━━━━━━━━━━━━━━━━━━━━━━"
await update.message.reply_text(text, parse_mode="Markdown")
except Exception as e:
await update.message.reply_text(f"❌ Error: {e}")
@authorized
async def cmd_drives(update: Update, context: ContextTypes.DEFAULT_TYPE):
try:
lines = ["💾 *Drives*", "━━━━━━━━━━━━━━━━━━━━━━"]
for p in psutil.disk_partitions():
try:
u = psutil.disk_usage(p.mountpoint)
lines.append(f"📀 `{p.device}` ({p.fstype})\n Used: {u.used//(1024**3)}GB / {u.total//(1024**3)}GB ({u.percent}%) | Free: {u.free//(1024**3)}GB")
except:
lines.append(f"📀 `{p.device}` — Access denied")
await update.message.reply_text("\n".join(lines), parse_mode="Markdown")
except Exception as e:
await update.message.reply_text(f"❌ Error: {e}")
@authorized
async def cmd_uptime(update: Update, context: ContextTypes.DEFAULT_TYPE):
try:
bt = datetime.fromtimestamp(psutil.boot_time())
up = datetime.now() - bt
d = up.days
h, r = divmod(up.seconds, 3600)
m, s = divmod(r, 60)
text = f"⏱️ *System Uptime*\n━━━━━━━━━━━━━━━━━━━━━━\n🕐 *Up for:* {d}d {h}h {m}m {s}s\n🖥️ *Boot time:* `{bt.strftime('%Y-%m-%d %H:%M:%S')}`\n━━━━━━━━━━━━━━━━━━━━━━"
await update.message.reply_text(text, parse_mode="Markdown")
except Exception as e:
await update.message.reply_text(f"❌ Error: {e}")
@authorized
async def cmd_wifi(update: Update, context: ContextTypes.DEFAULT_TYPE):
try:
r = subprocess.run(["netsh", "wlan", "show", "interfaces"], capture_output=True, text=True, timeout=10)
out = r.stdout.strip()
if not out or "not running" in out.lower():
await update.message.reply_text("📶 WiFi adapter not found or disabled.")
return
lines = ["📶 *WiFi Info*", "━━━━━━━━━━━━━━━━━━━━━━"]
for l in out.split("\n"):
l = l.strip()
for k in ["SSID", "Signal", "State", "Radio type", "Authentication", "Channel"]:
if l.startswith(k) and "BSSID" not in l:
lines.append(f"• `{l}`")
break
await update.message.reply_text("\n".join(lines), parse_mode="Markdown")
except Exception as e:
await update.message.reply_text(f"❌ Error: {e}")
@authorized
async def cmd_tasklist(update: Update, context: ContextTypes.DEFAULT_TYPE):
try:
t = list(set([p.info["name"] for p in psutil.process_iter(["name"]) if p.info["name"] and not p.info["name"].startswith("svchost")]))
t.sort()
tl = "\n".join([f"• `{x}`" for x in t[:50]])
text = f"📋 *Running Apps ({len(t)})*\n━━━━━━━━━━━━━━━━━━━━━━\n{tl}"
if len(t) > 50: text += f"\n\n_...and {len(t) - 50} more_"
await update.message.reply_text(text, parse_mode="Markdown")
except Exception as e:
await update.message.reply_text(f"❌ Error: {e}")
@authorized
async def cmd_kill(update: Update, context: ContextTypes.DEFAULT_TYPE):
if not context.args: return await update.message.reply_text("Usage: `/kill <name>`", parse_mode="Markdown")
t, k = " ".join(context.args).lower(), 0
try:
for p in psutil.process_iter(["name"]):
try:
if p.info["name"] and t in p.info["name"].lower():
p.kill()
k += 1
except: pass
await update.message.reply_text(f"💀 Killed {k} process(es) matching `{context.args[0]}`" if k else f"⚠️ No process matching `{context.args[0]}` found", parse_mode="Markdown")
except Exception as e:
await update.message.reply_text(f"❌ Error: {e}")
@authorized
async def cmd_open(update: Update, context: ContextTypes.DEFAULT_TYPE):
if not context.args: return await update.message.reply_text("Usage: `/open <path>`", parse_mode="Markdown")
t = " ".join(context.args)
try:
os.startfile(t)
await update.message.reply_text(f"✅ Opened: `{t}`", parse_mode="Markdown")
except Exception as e:
await update.message.reply_text(f"❌ Failed to open: {e}")
@authorized
async def cmd_shell(update: Update, context: ContextTypes.DEFAULT_TYPE):
if not context.args: return await update.message.reply_text("Usage: `/cmd <command>`", parse_mode="Markdown")
c = " ".join(context.args)
await update.message.reply_text(f"⚙️ Running: `{c}`", parse_mode="Markdown")
try:
r = subprocess.run(c, shell=True, capture_output=True, text=True, timeout=30)
o = r.stdout or r.stderr or "(No output)"
if len(o) > 3900: o = o[:3900] + "\n\n... (truncated)"
await update.message.reply_text(f"```\n{o}\n```", parse_mode="Markdown")
except subprocess.TimeoutExpired:
await update.message.reply_text("⏰ Command timed out (30s).")
except Exception as e:
await update.message.reply_text(f"❌ Error: {e}")
@authorized
async def cmd_download(update: Update, context: ContextTypes.DEFAULT_TYPE):
if not context.args: return await update.message.reply_text("Usage: `/download <url>`", parse_mode="Markdown")
url = context.args[0]
await update.message.reply_text(f"⬇️ Downloading...")
try:
res = requests.get(url, stream=True, timeout=60)
fn = url.split("/")[-1].split("?")[0] or "download"
fp = os.path.join(os.path.expanduser("~"), "Desktop", fn)
with open(fp, "wb") as f:
for c in res.iter_content(chunk_size=8192): f.write(c)
await update.message.reply_text(f"✅ Downloaded!\n📁 *File:* `{fn}`\n📂 *Saved to:* Desktop", parse_mode="Markdown")
except Exception as e:
await update.message.reply_text(f"❌ Download failed: {e}")
@authorized
async def cmd_sendfile(update: Update, context: ContextTypes.DEFAULT_TYPE):
if not context.args: return await update.message.reply_text("Usage: `/sendfile <path>`", parse_mode="Markdown")
fp = " ".join(context.args)
try:
if not os.path.exists(fp): return await update.message.reply_text(f"❌ File not found: `{fp}`", parse_mode="Markdown")
smb = os.path.getsize(fp) / (1024 * 1024)
if smb > 50: return await update.message.reply_text(f"❌ File too large ({smb:.1f}MB). Telegram limit is 50MB.")
await update.message.reply_text(f"📤 Sending `{os.path.basename(fp)}` ({smb:.2f}MB)...", parse_mode="Markdown")
with open(fp, "rb") as f: await update.message.reply_document(document=f, filename=os.path.basename(fp))
except Exception as e:
await update.message.reply_text(f"❌ Error: {e}")
@authorized
async def cmd_browse(update: Update, context: ContextTypes.DEFAULT_TYPE):
if not context.args: return await update.message.reply_text("Usage: `/browse <url>`", parse_mode="Markdown")
import webbrowser
u = context.args[0]
if not u.startswith("http"): u = "https://" + u
webbrowser.open(u)
await update.message.reply_text(f"🌐 Opened: `{u}`", parse_mode="Markdown")
@authorized
async def cmd_volume(update: Update, context: ContextTypes.DEFAULT_TYPE):
if not context.args: return await update.message.reply_text("Usage: `/volume <0-100>`", parse_mode="Markdown")
try:
l = int(context.args[0])
if not 0 <= l <= 100: return await update.message.reply_text("⚠️ Volume must be between 0 and 100.")
from ctypes import cast, POINTER
from comtypes import CLSCTX_ALL
from pycaw.pycaw import AudioUtilities, IAudioEndpointVolume
devs = AudioUtilities.GetSpeakers()
face = devs.Activate(IAudioEndpointVolume._iid_, CLSCTX_ALL, None)
vol = cast(face, POINTER(IAudioEndpointVolume))
vol.SetMasterVolumeLevelScalar(l / 100.0, None)
b = "█" * (l // 10) + "░" * (10 - l // 10)
await update.message.reply_text(f"🔊 Volume: {l}%\n[{b}]")
except Exception as e:
await update.message.reply_text(f"❌ Error: {e}")
@authorized
async def cmd_mute(update: Update, context: ContextTypes.DEFAULT_TYPE):
try:
from ctypes import cast, POINTER
from comtypes import CLSCTX_ALL
from pycaw.pycaw import AudioUtilities, IAudioEndpointVolume
devs = AudioUtilities.GetSpeakers()
face = devs.Activate(IAudioEndpointVolume._iid_, CLSCTX_ALL, None)
vol = cast(face, POINTER(IAudioEndpointVolume))
m = vol.GetMute()
vol.SetMute(not m, None)
await update.message.reply_text("🔊 Unmuted!" if m else "🔇 Muted!")
except Exception as e:
await update.message.reply_text(f"❌ Error: {e}")
@authorized
async def cmd_brightness(update: Update, context: ContextTypes.DEFAULT_TYPE):
if not context.args: return await update.message.reply_text("Usage: `/brightness <0-100>`", parse_mode="Markdown")
try:
l = int(context.args[0])
r = subprocess.run(["powershell", "-Command", f"(Get-WmiObject -Namespace root/WMI -Class WmiMonitorBrightnessMethods).WmiSetBrightness(1,{l})"], capture_output=True, text=True, timeout=10)
if r.returncode == 0: await update.message.reply_text(f"🔆 Brightness: {l}%\n[" + "☀" * (l // 10) + "░" * (10 - l // 10) + "]")
else: await update.message.reply_text(f"❌ Failed:\n`{r.stderr.strip()}`", parse_mode="Markdown")
except Exception as e:
await update.message.reply_text(f"❌ Error: {e}")
@authorized
async def cmd_media(update: Update, context: ContextTypes.DEFAULT_TYPE):
if not context.args: return await update.message.reply_text("Usage: `/media <play/pause/next/prev/stop>`", parse_mode="Markdown")
a = context.args[0].lower()
vk = {"play": "0xB3", "pause": "0xB3", "next": "0xB0", "prev": "0xB1", "stop": "0xB2"}
if a not in vk: return await update.message.reply_text("❌ Unknown action", parse_mode="Markdown")
try:
subprocess.run(["powershell", "-Command", f'Add-Type -TypeDefinition "using System; using System.Runtime.InteropServices; public class MediaKey {{ [DllImport(\\"user32.dll\\")] public static extern void keybd_event(byte bVk, byte bScan, uint dwFlags, UIntPtr dwExtraInfo); }}"; [MediaKey]::keybd_event({vk[a]}, 0, 0, [UIntPtr]::Zero); [MediaKey]::keybd_event({vk[a]}, 0, 2, [UIntPtr]::Zero)'], timeout=5)
await update.message.reply_text(f"🎵 Media: {a}")
except Exception as e:
await update.message.reply_text(f"❌ Error: {e}")
@authorized
async def cmd_say(update: Update, context: ContextTypes.DEFAULT_TYPE):
if not context.args: return await update.message.reply_text("Usage: `/say <text>`", parse_mode="Markdown")
t = " ".join(context.args).replace("'", "''")
try:
subprocess.Popen(["powershell", "-Command", f"Add-Type -AssemblyName System.Speech; (New-Object System.Speech.Synthesis.SpeechSynthesizer).Speak('{t}')"])
await update.message.reply_text(f"🗣️ Speaking: \"{' '.join(context.args)}\"")
except Exception as e:
await update.message.reply_text(f"❌ Error: {e}")
@authorized
async def cmd_alert(update: Update, context: ContextTypes.DEFAULT_TYPE):
if not context.args: return await update.message.reply_text("Usage: `/alert <msg>`", parse_mode="Markdown")
m = " ".join(context.args).replace("'", "''")
try:
subprocess.Popen(f'''powershell -Command "Add-Type -AssemblyName PresentationFramework; [System.Windows.MessageBox]::Show('{m}', 'Telegram Alert', 'OK', 'Information')"''', shell=True)
await update.message.reply_text(f"✅ Alert shown on PC: `{m}`", parse_mode="Markdown")
except Exception as e:
await update.message.reply_text(f"❌ Error: {e}")
@authorized
async def cmd_clipboard(update: Update, context: ContextTypes.DEFAULT_TYPE):
try:
r = subprocess.run(["powershell", "-Command", "Get-Clipboard"], capture_output=True, text=True, timeout=5)
c = r.stdout.strip() or "(Clipboard is empty)"
if len(c) > 3900: c = c[:3900] + "\n\n... (truncated)"
await update.message.reply_text(f"📋 *Clipboard:*\n```\n{c}\n```", parse_mode="Markdown")
except Exception as e:
await update.message.reply_text(f"❌ Error: {e}")
@authorized
async def cmd_setclip(update: Update, context: ContextTypes.DEFAULT_TYPE):
if not context.args: return await update.message.reply_text("Usage: `/setclip <text>`", parse_mode="Markdown")
t = " ".join(context.args)
try:
subprocess.run(["powershell", "-Command", f"Set-Clipboard -Value '{t}'"], timeout=5)
await update.message.reply_text(f"✅ Clipboard set to: `{t}`", parse_mode="Markdown")
except Exception as e:
await update.message.reply_text(f"❌ Error: {e}")
@authorized
async def cmd_note(update: Update, context: ContextTypes.DEFAULT_TYPE):
if not context.args: return await update.message.reply_text("Usage: `/note <text>`", parse_mode="Markdown")
t = " ".join(context.args)
try:
d = os.path.join(os.path.expanduser("~"), "Desktop")
ts = datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
fp = os.path.join(d, f"TelegramNote_{ts}.txt")
with open(fp, "w", encoding="utf-8") as f:
f.write(f"📝 Telegram Note\nDate: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}\n{'='*40}\n\n{t}")
await update.message.reply_text(f"📝 Note saved!\n📂 `{os.path.basename(fp)}`\n📁 Location: Desktop", parse_mode="Markdown")
except Exception as e:
await update.message.reply_text(f"❌ Error: {e}")
def run_bot():
# Reload config from file so subprocess gets latest GUI values
fresh_cfg = load_config()
global BOT_TOKEN, AUTHORIZED_USER_ID
BOT_TOKEN = fresh_cfg.get("BOT_TOKEN", "").strip()
AUTHORIZED_USER_ID = fresh_cfg.get("AUTHORIZED_USER_ID", "")
try: AUTHORIZED_USER_ID = int(AUTHORIZED_USER_ID)
except: AUTHORIZED_USER_ID = 0
if not BOT_TOKEN or not AUTHORIZED_USER_ID:
print("Error: Missing Configuration. Please set Bot Token and Chat ID in the GUI.")
sys.exit(1)
# Check if another instance is running via lock file
import tempfile
lock_file = os.path.join(tempfile.gettempdir(), 'telegram_pc_controller.lock')
try:
if os.path.exists(lock_file):
pidstr = open(lock_file, "r").read().strip()
if pidstr:
try:
pid_int = int(pidstr)
if psutil.pid_exists(pid_int):
proc = psutil.Process(pid_int)
if '--run-bot' in ' '.join(proc.cmdline()):
print(f"Error: Another instance is already running (PID {pidstr})")
sys.exit(1)
except (psutil.NoSuchProcess, psutil.AccessDenied, ValueError):
pass # Stale lock, overwrite it
open(lock_file, "w").write(str(os.getpid()))
except Exception:
pass
print("=" * 60)
print(" [PC] Telegram PC Controller Bot")
print("=" * 60)
print(f" Bot Token: ...{BOT_TOKEN[-8:]}")
print(f" Auth User: {AUTHORIZED_USER_ID}")
print(f" Started: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}")
print("=" * 60)
print(" [OK] Bot is running! Send /start on Telegram.")
app = ApplicationBuilder().token(BOT_TOKEN).build()
cmds = [
("start", cmd_start), ("help", cmd_start), ("myid", cmd_myid), ("screenshot", cmd_screenshot),
("webcam", cmd_webcam), ("lock", cmd_lock), ("shutdown", cmd_shutdown), ("restart", cmd_restart),
("logoff", cmd_logoff), ("cancel", cmd_cancel), ("sleep", cmd_sleep), ("status", cmd_status),
("processes", cmd_processes), ("ip", cmd_ip), ("drives", cmd_drives), ("uptime", cmd_uptime),
("wifi", cmd_wifi), ("tasklist", cmd_tasklist), ("kill", cmd_kill), ("open", cmd_open),
("cmd", cmd_shell), ("download", cmd_download), ("sendfile", cmd_sendfile), ("browse", cmd_browse),
("volume", cmd_volume), ("mute", cmd_mute), ("brightness", cmd_brightness), ("media", cmd_media),
("say", cmd_say), ("alert", cmd_alert), ("clipboard", cmd_clipboard), ("setclip", cmd_setclip),
("note", cmd_note)
]
for c, f in cmds:
app.add_handler(CommandHandler(c, f))
app.add_handler(CallbackQueryHandler(cancel_callback))
# We remove the lock file on exit (if possible safely)
try:
app.run_polling(drop_pending_updates=True)
finally:
try:
if os.path.exists(lock_file):
os.remove(lock_file)
except Exception:
pass
# =====================================================================
# GUI MANAGER LOGIC
# =====================================================================
class Theme:
BG = "#0d0d12"
SURFACE = "#161620"
SURFACE_2 = "#1c1c2a"
BORDER = "#2a2a3d"
GREEN = "#22c55e"
GREEN_DIM = "#166534"
RED = "#ef4444"
BLUE = "#3b82f6"
CYAN = "#06b6d4"
AMBER = "#f59e0b"
TEXT = "#f0f0f5"
TEXT_2 = "#a0a0b8"
TEXT_3 = "#606078"
TEXT_DIM = "#3d3d55"
class BotManagerGUI:
def __init__(self):
self.root = tk.Tk()
self.root.title("Telegram PC Controller")
# Load custom icon
ico_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), "app_icon.ico")
if getattr(sys, 'frozen', False):
ico_path = os.path.join(sys._MEIPASS, "app_icon.ico")
try: self.root.iconbitmap(ico_path)
except: pass
self.root.geometry("480x860")
self.root.resizable(False, False)
self.root.configure(bg=Theme.BG)
self.root.attributes('-alpha', 0.98)
self.bot_process = None
self.is_running = False
self.minimize_on_close = False
self.cmd_count = 0
self.start_time = None
self.fn = {
"title": tkfont.Font(family="Segoe UI", size=16, weight="bold"),
"subtitle": tkfont.Font(family="Segoe UI", size=9),
"label": tkfont.Font(family="Segoe UI", size=10, weight="bold"),
"small": tkfont.Font(family="Segoe UI", size=8),
"tiny": tkfont.Font(family="Segoe UI", size=7),
"status": tkfont.Font(family="Segoe UI", size=20, weight="bold"),
"status_sub": tkfont.Font(family="Segoe UI", size=9),
"log": tkfont.Font(family="Cascadia Code", size=8),
"stat_num": tkfont.Font(family="Segoe UI", size=14, weight="bold"),
"stat_lbl": tkfont.Font(family="Segoe UI", size=7),
}
self._build_ui()
self.root.protocol("WM_DELETE_WINDOW", self._on_close)
def _build_ui(self):
main = tk.Frame(self.root, bg=Theme.BG)
main.pack(fill="both", expand=True, padx=20, pady=(14, 10))
# Title Row
title_row = tk.Frame(main, bg=Theme.BG)
title_row.pack(fill="x")
tk.Label(title_row, text="⚡", font=("Segoe UI", 16), bg=Theme.BG, fg=Theme.CYAN).pack(side="left", padx=(0, 6))
title_col = tk.Frame(title_row, bg=Theme.BG)
title_col.pack(side="left")
tk.Label(title_col, text="PC Controller", font=self.fn["title"], fg=Theme.TEXT, bg=Theme.BG).pack(anchor="w")
tk.Label(title_col, text="Telegram Remote Access", font=self.fn["tiny"], fg=Theme.TEXT_3, bg=Theme.BG).pack(anchor="w")
# Power Section
power_section = tk.Frame(main, bg=Theme.BG)
power_section.pack(fill="x", pady=(10, 0))
self.power_canvas = tk.Canvas(power_section, width=110, height=110, bg=Theme.BG, highlightthickness=0)
self.power_canvas.pack()
self._draw_power_off()
self.power_canvas.bind("<Button-1>", self._toggle_bot)
self.power_canvas.configure(cursor="hand2")
self.status_text = tk.Label(power_section, text="OFFLINE", font=self.fn["status"], fg=Theme.RED, bg=Theme.BG)
self.status_text.pack(pady=(4, 0))
self.status_sub = tk.Label(power_section, text="Tap the button to start", font=self.fn["tiny"], fg=Theme.TEXT_3, bg=Theme.BG)
self.status_sub.pack()
# Stats Row
stats_frame = tk.Frame(main, bg=Theme.SURFACE, highlightbackground=Theme.BORDER, highlightthickness=1)
stats_frame.pack(fill="x", pady=(8, 0))
for i, (icon, label, val_attr) in enumerate([("📡", "STATUS", "_stat_status"), ("⏱️", "UPTIME", "_stat_uptime"), ("📨", "CMDS", "_stat_cmds"), ("🤖", "SERVERS", "_stat_zombies")]):
cell = tk.Frame(stats_frame, bg=Theme.SURFACE)
cell.pack(side="left", fill="both", expand=True, padx=1)
inner = tk.Frame(cell, bg=Theme.SURFACE)
inner.pack(pady=8)
tk.Label(inner, text=icon, font=("Segoe UI", 10), bg=Theme.SURFACE, fg=Theme.TEXT).pack()
val_label = tk.Label(inner, text="--", font=self.fn["label"], fg=Theme.TEXT, bg=Theme.SURFACE)
val_label.pack()
setattr(self, val_attr, val_label)
tk.Label(inner, text=label, font=self.fn["stat_lbl"], fg=Theme.TEXT_3, bg=Theme.SURFACE).pack()
if i < 3:
div = tk.Canvas(stats_frame, width=1, height=45, bg=Theme.SURFACE, highlightthickness=0)
div.pack(side="left", fill="y", pady=8)
div.create_line(0, 0, 0, 45, fill=Theme.BORDER)
self._stat_status.configure(text="OFF", fg=Theme.RED)
self._stat_uptime.configure(text="0m", fg=Theme.TEXT_2)
self._stat_cmds.configure(text="0", fg=Theme.TEXT_2)
self._stat_zombies.configure(text="0", fg=Theme.TEXT_2)
self._update_zombies()
# Configuration
config_label = tk.Label(main, text="CONFIGURATION", font=self.fn["tiny"], fg=Theme.TEXT_3, bg=Theme.BG)
config_label.pack(anchor="w", pady=(10, 3))
cfg_frame = tk.Frame(main, bg=Theme.SURFACE, highlightbackground=Theme.BORDER, highlightthickness=1)
cfg_frame.pack(fill="x")
cfg_inner = tk.Frame(cfg_frame, bg=Theme.SURFACE)
cfg_inner.pack(fill="x", padx=12, pady=8)
tk.Label(cfg_inner, text="🔑 Bot Token:", font=self.fn["small"], fg=Theme.TEXT_2, bg=Theme.SURFACE).grid(row=0, column=0, sticky="w", pady=2)
self.entry_token = tk.Entry(cfg_inner, font=self.fn["small"], bg=Theme.BG, fg=Theme.TEXT, insertbackground=Theme.TEXT, relief="flat", width=26, show="*")
self.entry_token.grid(row=0, column=1, padx=4, pady=2, sticky="ew")
self.entry_token.insert(0, BOT_TOKEN)
paste_tok = tk.Label(cfg_inner, text="📋", font=("Segoe UI", 9), bg=Theme.SURFACE, fg=Theme.TEXT_2, cursor="hand2")
paste_tok.grid(row=0, column=2, padx=2)
paste_tok.bind("<Button-1>", lambda e: self._paste_to(self.entry_token))
tk.Label(cfg_inner, text="👤 Chat ID:", font=self.fn["small"], fg=Theme.TEXT_2, bg=Theme.SURFACE).grid(row=1, column=0, sticky="w", pady=2)
self.entry_id = tk.Entry(cfg_inner, font=self.fn["small"], bg=Theme.BG, fg=Theme.TEXT, insertbackground=Theme.TEXT, relief="flat", width=26)
self.entry_id.grid(row=1, column=1, padx=4, pady=2, sticky="ew")
self.entry_id.insert(0, str(AUTHORIZED_USER_ID) if AUTHORIZED_USER_ID else "")
paste_id = tk.Label(cfg_inner, text="📋", font=("Segoe UI", 9), bg=Theme.SURFACE, fg=Theme.TEXT_2, cursor="hand2")
paste_id.grid(row=1, column=2, padx=2)
paste_id.bind("<Button-1>", lambda e: self._paste_to(self.entry_id))
save_btn = tk.Label(cfg_inner, text=" SAVE ", font=self.fn["small"], bg=Theme.BLUE, fg="#ffffff", padx=8, pady=2, cursor="hand2")
save_btn.grid(row=0, column=3, rowspan=2, padx=(4, 0))
save_btn.bind("<Button-1>", lambda e: self._save_config())
cfg_inner.columnconfigure(1, weight=1)
# Settings
settings_label = tk.Label(main, text="SETTINGS", font=self.fn["tiny"], fg=Theme.TEXT_3, bg=Theme.BG)
settings_label.pack(anchor="w", pady=(10, 3))
self._make_setting_row(main, "🚀", "Auto-Start with Windows", "Launch bot silently on login", self._check_autostart(), self._toggle_autostart)
self._make_setting_row(main, "🔽", "Minimize on Close", "Keep bot running in background", False, self._toggle_minimize)
self._make_action_row(main, "💀", "Kill All Servers", "Stop ANY running background bot", "KILL ALL", self._kill_all_zombies)
# Log
log_header = tk.Frame(main, bg=Theme.BG)
log_header.pack(fill="x", pady=(10, 3))
tk.Label(log_header, text="ACTIVITY LOG", font=self.fn["tiny"], fg=Theme.TEXT_3, bg=Theme.BG).pack(side="left")
clear_lbl = tk.Label(log_header, text="Clear", font=self.fn["tiny"], fg=Theme.BLUE, bg=Theme.BG, cursor="hand2")
clear_lbl.pack(side="right")
clear_lbl.bind("<Button-1>", lambda e: self._clear_log())
log_frame = tk.Frame(main, bg=Theme.SURFACE, highlightbackground=Theme.BORDER, highlightthickness=1)
log_frame.pack(fill="both", expand=True)
self.log_text = tk.Text(log_frame, bg=Theme.SURFACE, fg=Theme.TEXT_2, font=self.fn["log"], relief="flat", padx=10, pady=6, wrap="word", state="disabled", height=4, borderwidth=0)
self.log_text.pack(fill="both", expand=True)
self.log_text.tag_configure("time", foreground=Theme.TEXT_DIM)
self.log_text.tag_configure("info", foreground=Theme.BLUE)
self.log_text.tag_configure("success", foreground=Theme.GREEN)
self.log_text.tag_configure("error", foreground=Theme.RED)
self.log_text.tag_configure("warn", foreground=Theme.AMBER)
self._log("Ready. Enter config and tap power.", "info")
# Author Footer
footer = tk.Frame(main, bg=Theme.BG)
footer.pack(fill="x", pady=(6, 0))
author_lbl = tk.Label(footer, text="Made with \u2764\ufe0f by Nothing-dot-exe", font=self.fn["tiny"], fg=Theme.TEXT_3, bg=Theme.BG)
author_lbl.pack(side="left")
gh_btn = tk.Label(footer, text=" \U0001f310 GitHub", font=self.fn["tiny"], fg=Theme.CYAN, bg=Theme.BG, cursor="hand2")
gh_btn.pack(side="right")
gh_btn.bind("<Button-1>", lambda e: __import__('webbrowser').open("https://github.com/Nothing-dot-exe"))
def _paste_to(self, entry):
try:
clip = self.root.clipboard_get()
entry.delete(0, "end")
entry.insert(0, clip.strip())
self._log("Pasted from clipboard.", "info")
except tk.TclError:
self._log("Clipboard is empty.", "warn")
def _save_config(self):
tok = self.entry_token.get().strip()
uid = self.entry_id.get().strip()
try: uid = int(uid) if uid else 0
except: uid = 0
save_config(tok, uid)
self._log("Config saved! Restart bot to apply changes.", "success")
def _make_action_row(self, parent, icon, title, desc, btn_text, callback):
row = tk.Frame(parent, bg=Theme.SURFACE, highlightbackground=Theme.BORDER, highlightthickness=1)
row.pack(fill="x", pady=(0, 4))
inner = tk.Frame(row, bg=Theme.SURFACE)
inner.pack(fill="x", padx=14, pady=10)
tk.Label(inner, text=icon, font=("Segoe UI", 13), bg=Theme.SURFACE, fg=Theme.TEXT).pack(side="left", padx=(0, 10))
text_col = tk.Frame(inner, bg=Theme.SURFACE)
text_col.pack(side="left", fill="x", expand=True)
tk.Label(text_col, text=title, font=self.fn["label"], fg=Theme.TEXT, bg=Theme.SURFACE).pack(anchor="w")
tk.Label(text_col, text=desc, font=self.fn["tiny"], fg=Theme.TEXT_3, bg=Theme.SURFACE).pack(anchor="w")
btn = tk.Label(inner, text=btn_text, font=self.fn["small"], bg=Theme.RED, fg="#ffffff", padx=8, pady=4, cursor="hand2")
btn.pack(side="right")
btn.bind("<Button-1>", lambda e: callback())
def _update_zombies(self):
try:
count = sum(1 for p in psutil.process_iter(['cmdline']) if p.info.get('cmdline') and '--run-bot' in p.info['cmdline'])
self._stat_zombies.configure(text=str(count), fg=Theme.RED if count > 0 else Theme.TEXT_2)
except: pass
self.root.after(2000, self._update_zombies)
def _kill_all_zombies(self):
self._log("Killing all background bot servers...", "warn")
killed = 0
for p in psutil.process_iter(['cmdline']):
try:
cmd = p.info.get('cmdline')
if cmd and '--run-bot' in cmd:
p.kill()
killed += 1
except: pass
import tempfile
lock_file = os.path.join(tempfile.gettempdir(), 'telegram_pc_controller.lock')
if os.path.exists(lock_file):
try: os.remove(lock_file)
except: pass
self.is_running = False
self.bot_process = None
self._update_ui_stopped()
self._log(f"Killed {killed} background server(s).", "success")
self._update_zombies()
def _make_setting_row(self, parent, icon, title, desc, initial, callback):
row = tk.Frame(parent, bg=Theme.SURFACE, highlightbackground=Theme.BORDER, highlightthickness=1)
row.pack(fill="x", pady=(0, 4))
inner = tk.Frame(row, bg=Theme.SURFACE)
inner.pack(fill="x", padx=14, pady=10)
tk.Label(inner, text=icon, font=("Segoe UI", 13), bg=Theme.SURFACE, fg=Theme.TEXT).pack(side="left", padx=(0, 10))
text_col = tk.Frame(inner, bg=Theme.SURFACE)
text_col.pack(side="left", fill="x", expand=True)
tk.Label(text_col, text=title, font=self.fn["label"], fg=Theme.TEXT, bg=Theme.SURFACE).pack(anchor="w")
tk.Label(text_col, text=desc, font=self.fn["tiny"], fg=Theme.TEXT_3, bg=Theme.SURFACE).pack(anchor="w")
toggle = tk.Canvas(inner, width=44, height=24, bg=Theme.SURFACE, highlightthickness=0, cursor="hand2")
toggle.pack(side="right")
toggle._is_on = initial
toggle._callback = callback
self._draw_toggle(toggle, initial)
toggle.bind("<Button-1>", lambda e, t=toggle: self._click_toggle(t))
def _draw_toggle(self, canvas, is_on):
canvas.delete("all")
w, h, r = 44, 24, 12
if is_on: track_color, knob_x = Theme.GREEN, w - r
else: track_color, knob_x = Theme.TEXT_DIM, r
canvas.create_oval(1, 1, h-1, h-1, fill=track_color, outline="")
canvas.create_oval(w-h+1, 1, w-1, h-1, fill=track_color, outline="")
canvas.create_rectangle(r, 1, w-r, h-1, fill=track_color, outline="")
knob_r = r - 3
canvas.create_oval(knob_x - knob_r, r - knob_r, knob_x + knob_r, r + knob_r, fill="#ffffff", outline="")
def _click_toggle(self, toggle):
toggle._is_on = not toggle._is_on
self._draw_toggle(toggle, toggle._is_on)
if toggle._callback: toggle._callback(toggle._is_on)
def _draw_power_off(self):
c = self.power_canvas
c.delete("all")
cx, cy = 55, 55
c.create_oval(8, 8, 102, 102, fill=Theme.SURFACE, outline=Theme.BORDER, width=2)
c.create_oval(14, 14, 96, 96, fill=Theme.SURFACE_2, outline="")
icon_r = 18
c.create_arc(cx-icon_r, cy-icon_r+4, cx+icon_r, cy+icon_r+4, start=50, extent=260, style="arc", outline=Theme.RED, width=3)
c.create_line(cx, cy-icon_r-1, cx, cy+2, fill=Theme.RED, width=3)
def _draw_power_on(self):
c = self.power_canvas
c.delete("all")
cx, cy = 55, 55
for i in range(2):
offset = (i + 1) * 3
c.create_oval(8-offset, 8-offset, 102+offset, 102+offset, fill="", outline=Theme.GREEN_DIM, width=1)
c.create_oval(8, 8, 102, 102, fill=Theme.SURFACE, outline=Theme.GREEN, width=2)
c.create_oval(14, 14, 96, 96, fill="#0d1a0d", outline="")
icon_r = 18
c.create_arc(cx-icon_r, cy-icon_r+4, cx+icon_r, cy+icon_r+4, start=50, extent=260, style="arc", outline=Theme.GREEN, width=3)
c.create_line(cx, cy-icon_r-1, cx, cy+2, fill=Theme.GREEN, width=3)
def _toggle_bot(self, event=None):
if self.is_running: self._stop_bot()
else: self._start_bot()
def _start_bot(self):
# Auto-save config from GUI fields before launching
tok = self.entry_token.get().strip()
uid = self.entry_id.get().strip()
if not tok or not uid:
self._log("Missing Bot Token or Chat ID! Fill in Configuration first.", "error")
return
try:
uid_int = int(uid)
except ValueError:
self._log("Chat ID must be a number!", "error")
return
save_config(tok, uid_int)
self._log("Starting bot...", "info")
try:
# RUN ITSELF WITH THE `--run-bot` FLAG
if getattr(sys, 'frozen', False):
cmd = [sys.executable, "--run-bot"]
else:
cmd = [PYTHON_EXE, THIS_SCRIPT, "--run-bot"]
self.bot_process = subprocess.Popen(
cmd,
cwd=SCRIPT_DIR,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
creationflags=subprocess.CREATE_NO_WINDOW,
env=dict(os.environ, PYTHONUNBUFFERED="1", PYTHONIOENCODING="utf-8")
)
self.is_running = True
self.start_time = datetime.now()
self.cmd_count = 0
self._update_ui_running()
self._log("Bot is online!", "success")
t = threading.Thread(target=self._read_output, daemon=True)
t.start()