Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pyautogui
- import telegram.error
- from win32 import win32gui
- from telegram.ext import Updater, CommandHandler, CallbackContext, Dispatcher, ConversationHandler, MessageHandler, \
- Filters
- from telegram import Update, InputMediaPhoto
- import subprocess
- import io
- from typing import Optional
- import threading
- import time
- from PIL import Image
- from pynput.keyboard import Controller
- TOKEN = 'XXXXXXXXXXXXXXXXXXXXXXXX'
- SNAKE_PATH = r"C:\Users\...\Snake\script.py"
- snake_process: Optional[subprocess.Popen] = None
- def screenshot(window_title: Optional[str] = None) -> Image:
- """Get a screenshot of the window with window_title"""
- if window_title:
- hwnd = win32gui.FindWindow(None, window_title)
- if hwnd:
- try:
- win32gui.SetForegroundWindow(hwnd)
- except Exception:
- pass
- x, y, x1, y1 = win32gui.GetClientRect(hwnd)
- x, y = win32gui.ClientToScreen(hwnd, (x, y))
- x1, y1 = win32gui.ClientToScreen(hwnd, (x1 - x, y1 - y))
- image = pyautogui.screenshot(region=(x, y, x1, y1))
- return image
- else:
- image = pyautogui.screenshot()
- return image
- def send_snake_images(update: Update, context: CallbackContext, process: subprocess.Popen) -> None:
- """Threaded function that sends and continuously edits a photo with screenshots"""
- first = True
- msg = None
- while process.poll() is None:
- if image := screenshot('Snake'): # Gets a screenshot of the window
- bytesio = io.BytesIO()
- image.save(bytesio, format='PNG')
- bytes_image = bytes(bytesio.getvalue()) # Convert to bytes object
- if first: # First time send a full message
- msg = context.bot.send_photo(update.message.chat_id, bytes_image)
- first = False
- else:
- try:
- msg.edit_media(media=InputMediaPhoto(media=bytes_image)) # Edit the message with the next photo
- except telegram.error.BadRequest:
- pass
- def snake(update: Update, context: CallbackContext) -> str:
- """Callback for /snake command- starts the game and screenshot sending"""
- global snake_process
- snake_process = subprocess.Popen(['python', SNAKE_PATH])
- time.sleep(2) # Wait for the window to open
- thread = threading.Thread(target=send_snake_images, args=(update, context, snake_process))
- thread.start() # Start thread that wil send photos
- return "StartReceivingDirections" # Move to next state
- def exit_snake(update: Update, context: CallbackContext) -> int:
- """Exit callback"""
- if snake_process is not None and snake_process.poll() is None:
- snake_process.terminate() # End the game process
- return -1 # End the conversation
- def get_directions(update: Update, context: CallbackContext) -> None:
- """Callback to text - will press the button"""
- keyboard = Controller()
- keyboard.press(update.message.text[0]) # press the first letter
- keyboard.release(update.message.text[0])
- def main():
- updater: Updater = Updater(TOKEN)
- entry_points = [CommandHandler('snake', snake)] # Snake game conversation
- states = {"StartReceivingDirections": [CommandHandler('exit', exit_snake),
- MessageHandler(Filters.text & (~Filters.command), get_directions)]}
- fallbacks = []
- updater.dispatcher.add_handler(ConversationHandler(entry_points, states, fallbacks))
- updater.start_polling()
- updater.idle()
- if __name__ == '__main__':
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement