from telegram import Update
from telegram.ext import Application, CommandHandler, MessageHandler, filters, ContextTypes
from PIL import Image, ImageDraw, ImageFont
TOKEN = "7830309255:AAHzpZfL62dW8haX41-x5lbUac6oEU1rke4"
def generator_mem(img_file, top_text, bottom_text):
image = Image.open(img_file)
width, height = image.size
draw = ImageDraw.Draw(image)
font = ImageFont.truetype("v_CCTimSale_v1.11.ttf", size=60)
# Расчет позиции для верхнего текста
text_bbox = draw.textbbox((0, 0), top_text, font=font)
text_width = text_bbox[2] - text_bbox[0]
draw.text(((width - text_width) // 2, 10), top_text, font=font, fill="black")
# Расчет позиции для нижнего текста
text_bbox = draw.textbbox((0, 0), bottom_text, font=font)
text_width = text_bbox[2] - text_bbox[0]
text_height = text_bbox[3] - text_bbox[1]
draw.text(((width - text_width) // 2, height - text_height - 50), bottom_text, font=font, fill="black")
image.save("mem.jpg")
async def start(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
await update.message.reply_text("Привет, генератор мемов запущен! Введите верхний текст:")
context.user_data["state"] = "awaiting_top_text"
async def handle_text(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
user_text = update.message.text
state = context.user_data.get("state")
if state == "awaiting_top_text":
context.user_data["top_text"] = user_text
await update.message.reply_text("Теперь введите нижний текст: ")
context.user_data["state"] = "awaiting_bottom_text"
elif state == "awaiting_bottom_text":
context.user_data["bottom_text"] = user_text
await update.message.reply_text("Выбери картинку: 1:Бу испугался 2:Смешной кот 3:Черная рамка мем 4:Джокер")
context.user_data["state"] = "awaiting_image_choice"
elif state == "awaiting_image_choice":
if user_text in ["1", "2", "3", "4"]:
# Сохраняем оригинальную конструкцию выбора
if user_text == "1":
img_file = "cat.jpg"
elif user_text == "2":
img_file = "dog.jpg"
elif user_text == "3":
img_file = "rabbit.jpeg"
elif user_text == "4":
img_file = "winner.jpeg"
top_text = context.user_data['top_text']
bottom_text = context.user_data['bottom_text']
generator_mem(img_file, top_text, bottom_text)
await update.message.reply_photo(photo=open("mem.jpg", 'rb'))
await update.message.reply_text("Мем готов! Используй /start чтобы сделать новый мем")
context.user_data.clear()
else:
await update.message.reply_text("Пожалуйста, сначала введите /start")
def main():
application = Application.builder().token(TOKEN).build()
application.add_handler(CommandHandler("start", start))
application.add_handler(MessageHandler(filters.TEXT & ~filters.COMMAND, handle_text))
application.run_polling()
if __name__ == "__main__":
main()