import telebot

from PIL import Image, ImageDraw, ImageFont

import random

import string

from io import BytesIO

import time


TOKEN = 'আপনার টেলিগ্রাম বট টোকেন'

bot = telebot.TeleBot(TOKEN)


# webhook মুছে ফেলা

bot.remove_webhook()


# ক্যাপচা টেক্সট জেনারেট করার ফাংশন

def generate_captcha_text(length=6):

    letters = string.ascii_uppercase + string.digits

    return ''.join(random.choice(letters) for i in range(length))


# ক্যাপচা ইমেজ তৈরি করার ফাংশন

def generate_captcha_image(captcha_text):

    width, height = 200, 80

    image = Image.new('RGB', (width, height), color=(255, 255, 255))

    draw = ImageDraw.Draw(image)

    font = ImageFont.load_default()

    

    # পরিবর্তিত অংশ: textsize() → textbbox()

    text_bbox = draw.textbbox((0, 0), captcha_text, font=font)

    text_width = text_bbox[2] - text_bbox[0]

    text_height = text_bbox[3] - text_bbox[1]

    

    position = ((width - text_width) // 2, (height - text_height) // 2)

    draw.text(position, captcha_text, fill=(0, 0, 0), font=font)

    return image


# টাইমআউট সেট করা (৫ সেকেন্ড)

CAPTCHA_TIMEOUT = 5


# ব্যবহারকারীকে ক্যাপচা পাঠানোর হ্যান্ডলার

@bot.message_handler(commands=['start'])

def send_captcha(message):

    captcha_text = generate_captcha_text()

    captcha_image = generate_captcha_image(captcha_text)

    

    byte_io = BytesIO()

    captcha_image.save(byte_io, 'PNG')

    byte_io.seek(0)

    

    bot.send_photo(message.chat.id, byte_io)

    bot.send_message(message.chat.id, "এই ক্যাপচা টেক্সট লিখুন:")

    

    bot.register_next_step_handler(message, verify_captcha, captcha_text)


# ক্যাপচা যাচাই করার ফাংশন

def verify_captcha(message, captcha_text):

    user_input = message.text

    if time.time() - message.date > CAPTCHA_TIMEOUT:

        bot.reply_to(message, "সময় শেষ হয়ে গেছে! ক্যাপচা স্কিপ করা হচ্ছে।")

        return


    if user_input == captcha_text:

        bot.reply_to(message, "আপনি সঠিকভাবে ক্যাপচা প্রবেশ করেছেন!")

    else:

        bot.reply_to(message, "ভুল ক্যাপচা। আবার চেষ্টা করুন!")


# বট চালু করার জন্য try-except ব্লক

try:

    print("Starting bot polling...") # Debugging print

    bot.

polling()

except Exception as e:

    print(f"Error occurred: {e}")