| import os | |
| import subprocess | |
| from datetime import datetime | |
| from config import OS_COMMANDS, TEMP_DIR | |
| def execute_command(command_type: str, command_text: str) -> str: | |
| try: | |
| if command_type == "open_app": | |
| for app, cmd in OS_COMMANDS.get("open", {}).items(): | |
| if app in command_text.lower(): | |
| subprocess.Popen(cmd.split()) | |
| return f"β Opening {app.title()}..." | |
| elif command_type == "close_app": | |
| for app, cmd in OS_COMMANDS.get("close", {}).items(): | |
| if app in command_text.lower(): | |
| os.system(cmd) | |
| return f"π Closing {app.title()}..." | |
| elif command_type == "screenshot": | |
| screenshot_path = os.path.join(TEMP_DIR, f"screenshot_{datetime.now().strftime('%Y%m%d_%H%M%S')}.png") | |
| cmd = OS_COMMANDS["screenshot"]["default"] | |
| cmd = cmd.replace("screenshot.png", screenshot_path) | |
| os.system(cmd) | |
| return f"πΈ Screenshot saved to: {screenshot_path}" | |
| elif command_type == "create_file": | |
| filename = "new_file.txt" | |
| with open(filename, "w") as f: | |
| f.write("Hello from your AI assistant.") | |
| return f"β File '{filename}' created." | |
| elif command_type == "shutdown": | |
| os.system(OS_COMMANDS["shutdown"]) | |
| return "π Shutting down..." | |
| elif command_type == "restart": | |
| os.system(OS_COMMANDS["restart"]) | |
| return "π Restarting system..." | |
| elif command_type == "lock": | |
| os.system(OS_COMMANDS["lock"]) | |
| return "π System locked." | |
| except Exception as e: | |
| return f"β Error while executing command: {e}" | |
| return "β οΈ Command not recognized or not supported yet." | |