import asyncio import httpx from playwright.async_api import async_playwright, TimeoutError as PlaywrightTimeoutError SCREENSHOT_PATH = "/tmp/screenshot.png" TELEGRAM_BOT_TOKEN = "8865512961:AAG4VVJk7S58oAF5Ps00xgwfoPjXobbcm1E" TELEGRAM_CHAT_ID = "-1004385538803" TELEGRAM_API_URL = f"https://api.telegram.org/bot{TELEGRAM_BOT_TOKEN}/sendPhoto" TARGET_URL = "https://www.google.com" async def capture_screenshot() -> bool: """Launch headless Chromium, navigate to target URL, and capture full-page screenshot.""" print(f"[*] Launching headless Chromium browser...") try: async with async_playwright() as p: browser = await p.chromium.launch(headless=True) page = await browser.new_page() print(f"[*] Navigating to {TARGET_URL}...") # Wait until network is idle to ensure full page load await page.goto(TARGET_URL, wait_until="networkidle", timeout=30000) print(f"[*] Capturing full-page screenshot to {SCREENSHOT_PATH}...") await page.screenshot(path=SCREENSHOT_PATH, full_page=True) await browser.close() print(f"[+] Screenshot saved successfully to {SCREENSHOT_PATH}") return True except PlaywrightTimeoutError: print(f"[-] Timeout error: Could not load {TARGET_URL} within the allowed time.") return False except Exception as e: print(f"[-] Unexpected error during screenshot capture: {e}") return False def send_screenshot_via_telegram() -> bool: """Send the captured screenshot to Telegram chat using sendPhoto API.""" print(f"[*] Sending screenshot to Telegram chat {TELEGRAM_CHAT_ID}...") try: with open(SCREENSHOT_PATH, "rb") as photo_file: with httpx.Client(timeout=30.0) as client: response = client.post( TELEGRAM_API_URL, data={"chat_id": TELEGRAM_CHAT_ID, "caption": f"Screenshot of {TARGET_URL}"}, files={"photo": ("screenshot.png", photo_file, "image/png")}, ) if response.status_code == 200: result = response.json() if result.get("ok"): print(f"[+] Screenshot sent successfully to Telegram!") return True else: print(f"[-] Telegram API error: {result.get('description', 'Unknown error')}") return False else: print(f"[-] HTTP error from Telegram API: {response.status_code} - {response.text}") return False except httpx.TimeoutException: print(f"[-] Network timeout: Failed to reach Telegram API within the allowed time.") return False except httpx.NetworkError as e: print(f"[-] Network error while sending to Telegram: {e}") return False except FileNotFoundError: print(f"[-] Screenshot file not found at {SCREENSHOT_PATH}. Was the capture step successful?") return False except Exception as e: print(f"[-] Unexpected error while sending screenshot: {e}") return False async def main(): print("=== Screenshot Scout Starting ===") # Step 1: Capture the screenshot screenshot_ok = await capture_screenshot() if not screenshot_ok: print("[!] Aborting: Screenshot capture failed.") return # Step 2: Send screenshot via Telegram send_ok = send_screenshot_via_telegram() if send_ok: print("=== Screenshot Scout Completed Successfully ===") else: print("=== Screenshot Scout Finished With Errors ===") if __name__ == "__main__": asyncio.run(main())