Advertisement
OtsoSilver

Untitled

Feb 20th, 2022
995
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.52 KB | None | 0 0
  1. from telegram import Bot, Poll
  2. from telegram.ext import Updater, CommandHandler, MessageHandler, Filters, ConversationHandler, CallbackContext
  3. from credits import bot_token
  4. from PIL import Image, ImageFont, ImageDraw
  5.  
  6. bot = Bot(token=bot_token)
  7. updater = Updater(token=bot_token)
  8. dispatcher = updater.dispatcher
  9.  
  10.  
  11. def set_meme_text(username, ts, bs):
  12.     img = Image.open(f'{username}_photo.jpg')
  13.     rgb_img = img.convert("RGB")
  14.     title_font = ImageFont.truetype("Nautilus.otf", 60)
  15.     width, height = rgb_img.size
  16.     meme = ImageDraw.Draw(rgb_img)
  17.     meme.text(((width / 2 - width / 4), height / 4), ts, (0, 0, 0), font=title_font)
  18.     meme.text(((width / 2 - width / 4), height - height / 4), bs, (0, 0, 0), font=title_font)
  19.     rgb_img.save(f"{username}_meme.jpg")
  20.  
  21.  
  22. PHOTO = 1
  23. TOP_STRING = 1
  24. BOT_STRING = 1
  25. VIDEO = 1
  26. ts = ''
  27.  
  28. def start(update, context):
  29.     update.message.reply_text('Добро пожаловать в генератор ботов!'
  30.                               ' Отправьте фото, чтобы начать с ним работать!')
  31.     return PHOTO
  32.  
  33.  
  34. def photo(update, context):
  35.     user = str(update.message.from_user['username'])
  36.     photo_file = update.message.document.get_file()
  37.     photo_file.download(f'{user}_photo.jpg')
  38.     update.message.reply_text('Отлично! Теперь добавь верхнюю надпись'
  39.                               'или отправь /skip если хочешь пропустить этот шаг')
  40.     return TOP_STRING
  41. def top_string(update,context):
  42.     global ts
  43.     update.message.reply_text("Смешно =) А теперь вводи нижнюю надпись или отправь;"
  44.                               " /skip если хочешь пропустить этот шаг")
  45.     ts = update.message.text
  46.     return BOT_STRING
  47. def skip_top_string(update,context):
  48.     update.message.reply_text('Тогда пиши что должно быть внизу или отправь'
  49.                               ' /skip если хочешь пропустить этот шаг')
  50.     return BOT_STRING
  51. def bottom_string(update,context):
  52.     user = str(update.message.from_user['username'])
  53.     update.message.reply_text('Лови результат')
  54.     bs = update.message.text
  55.     set_meme_text(user, ts,bs)
  56.     sending_img = open(user+ "_meme.jpg", 'rb')
  57.     context.bot.send_document(update.effective_chat.id, sending_img)
  58.     return ConversationHandler.END
  59. bottom_string_handler = MessageHandler(Filters.text & ~Filters.command,bottom_string)
  60. top_string_handler = MessageHandler(Filters.text & ~Filters.command,top_string)
  61. skip_top_string_handler = CommandHandler("skip", skip_top_string)
  62. start_handler = CommandHandler("start",start)
  63. photo_handler = MessageHandler(Filters.document.category("image"), photo)
  64. conv_handler = ConversationHandler(entry_points=start_handler,
  65.                                    states={
  66.                                        PHOTO: [photo_handler],
  67.                                        TOP_STRING: [top_string_handler,skip_top_string_handler],
  68.                                        BOT_STRING: [bottom_string_handler]
  69.                                    },fallbacks=[bottom_string_handler])
  70.  
  71. dispatcher.add_handler(start_handler)
  72. dispatcher.add_handler(conv_handler)
  73. dispatcher.add_handler(bottom_string_handler)
  74. dispatcher.add_handler(top_string_handler)
  75. dispatcher.add_handler(skip_top_string_handler)
  76. dispatcher.add_handler(photo_handler)
  77. updater.start_polling()
  78. updater.idle()
  79.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement