Botisbot / app.py
Akwbw's picture
Update app.py
110bc66 verified
import os
import subprocess
import time
# Display set karna
os.environ["DISPLAY"] = ":99"
import base64
from flask import Flask, request, jsonify
from flask_cors import CORS
import pyautogui
# mss hata diya hai, hum direct system command use karenge (failsafe)
app = Flask(__name__)
CORS(app)
SCREEN_WIDTH = 1024
SCREEN_HEIGHT = 768
pyautogui.FAILSAFE = False
@app.route('/')
def home():
return "Botisbot Cloud PC (Fluxbox Edition) Ready!"
@app.route('/snapshot', methods=['GET'])
def get_snapshot():
try:
# METHOD: Direct System Command (Sabse reliable method)
# Hum 'scrot' use karke image temp file mein save karenge aur fir read karenge
filename = "/tmp/screen.jpg"
# Scrot command: -z (silent), -o (overwrite), -q 50 (quality 50%)
subprocess.run(["scrot", "-z", "-o", "-q", "50", filename], check=True)
# Image read karo
with open(filename, "rb") as image_file:
b64 = base64.b64encode(image_file.read()).decode('utf-8')
return jsonify({"screenshot": b64})
except Exception as e:
print(f"Snapshot Error: {e}")
# Agar error aaye to user ko batao
return jsonify({"error": str(e)}), 500
@app.route('/interact', methods=['POST'])
def interact():
data = request.json
action = data.get('action')
try:
if action == 'tap':
x = int(data.get('x') * SCREEN_WIDTH)
y = int(data.get('y') * SCREEN_HEIGHT)
pyautogui.click(x, y)
elif action == 'type':
pyautogui.write(data.get('text'))
elif action == 'key':
k = data.get('key').lower()
if k == 'enter': pyautogui.press('enter')
elif k == 'backspace': pyautogui.press('backspace')
elif action == 'scroll':
pyautogui.scroll(-5)
elif action == 'open_terminal':
subprocess.Popen(["xterm", "&"])
elif action == 'open_browser':
subprocess.Popen(["chromium-browser", "--no-sandbox"])
return jsonify({"status": "ok"})
except Exception as e:
return jsonify({"error": str(e)}), 500
if __name__ == '__main__':
app.run(host='0.0.0.0', port=7860, threaded=True)