kopyl

Untitled

Dec 10th, 2024
23
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.08 KB | None | 0 0
  1. from telegram import Update
  2. from telegram.ext import ApplicationBuilder, ContextTypes, CommandHandler, MessageHandler, filters
  3. from telegram import constants
  4. from telegram.helpers import escape_markdown
  5.  
  6. from PIL import Image
  7. from io import BytesIO
  8.  
  9.  
  10. BOT_TOKEN = "..."
  11.  
  12. WELCOME_MESSAGE = escape_markdown(
  13.     "Send me a prompt and I will generate a phot of Oleh Kopyl\n"
  14.     "All prompts should include `kopyl` keyword\n\n"
  15.     "For example: `Photo of kopyl riding on a huge cock`"
  16. )
  17. NO_KOPYL_MENTION_IN_PROMPT_MESSAGE = (
  18.     "You need to include `kopyl` keyword somewhere in your prompt, otherwise i can't generate anything\n\n"
  19.     "For example: `Photo of kopyl riding on a huge cock`"
  20. )
  21. USER_INVALID_REQUEST_INFO_MESSAGE = (
  22.     "A user requested an image with an invalid prompt (no 'kopyl' keyword in it)\n\n"
  23.     "{user_info}\n\n"
  24.     "{image_info}"
  25. )
  26. USER_RESPONSE_INFO_MESSAGE = (
  27.     "{user_info}\n\n"
  28.     "{image_info}"
  29. )
  30.  
  31. FRIENDS_GROUP_CHAT_ID = -100xxx
  32. ADMIN_CHAT_ID = xxx
  33. REPORTING_CHAT_ID = FRIENDS_GROUP_CHAT_ID
  34.  
  35.  
  36. def construct_user_info(update):
  37.     user_id = update.effective_user.id
  38.     username = update.effective_user.username
  39.     first_name = update.effective_user.first_name
  40.     last_name = update.effective_user.last_name
  41.     info = (
  42.         (
  43.             f"`{user_id}` \n"
  44.             f"@{username}\n" if username else ""
  45.         ) +
  46.         (f"{first_name} " if  first_name else "") +
  47.         f"{last_name} " if  last_name else ""
  48.     )
  49.     return info
  50.  
  51.  
  52. def construct_image_request_info(update):
  53.     return f"Prompt: {escape_markdown(update.message.text)}"
  54.  
  55.  
  56. def construct_image_response_info(update, seed):
  57.     return (
  58.         f"Prompt: `{escape_markdown(update.message.text)}`\n"
  59.         f"Seed: `{seed}`"
  60.     )
  61.  
  62.  
  63. def prompt_is_not_valid(prompt):
  64.     if "kopyl" in prompt.lower():
  65.         return
  66.     return True
  67.  
  68.  
  69. def generate_seed() -> int:
  70.     ...
  71.  
  72.  
  73. def convert_PIL_image_to_sendable_photo(pil_image: Image.Image):
  74.     stream = BytesIO()
  75.     pil_image.save(stream, "PNG")
  76.     stream.seek(0)
  77.     return stream
  78.  
  79.  
  80. def generate_image(prompt: str) -> [Image.Image, int]:
  81.     ...
  82.  
  83.  
  84. async def start_bot_command(update: Update, context: ContextTypes.DEFAULT_TYPE):
  85.     await update.message.reply_markdown(WELCOME_MESSAGE)
  86.  
  87.  
  88. async def process_image_generation_request(update: Update, context: ContextTypes.DEFAULT_TYPE):
  89.     if prompt_is_not_valid(update.message.text):
  90.         await context.bot.send_message(
  91.             REPORTING_CHAT_ID,
  92.             parse_mode=constants.ParseMode.MARKDOWN,
  93.             disable_notification=True,
  94.             text=USER_INVALID_REQUEST_INFO_MESSAGE.format(
  95.                 user_info=construct_user_info(update),
  96.                 image_info=construct_image_request_info(update),
  97.             ),
  98.         )
  99.         return await update.message.reply_markdown(
  100.             NO_KOPYL_MENTION_IN_PROMPT_MESSAGE
  101.         )
  102.    
  103.     await update.message.reply_text("Generating...")
  104.  
  105.     image, seed = generate_image(update.message.text)
  106.     image_bytes = convert_PIL_image_to_sendable_photo(image)
  107.     sent_photo = await context.bot.send_photo(
  108.         update.effective_chat.id, image_bytes,
  109.         parse_mode=constants.ParseMode.MARKDOWN,
  110.         caption=construct_image_response_info(update, seed)
  111.     )
  112.    
  113.     await context.bot.copy_message(
  114.         REPORTING_CHAT_ID,
  115.         update.effective_chat.id,
  116.         sent_photo.message_id,
  117.         parse_mode=constants.ParseMode.MARKDOWN,
  118.         caption=USER_RESPONSE_INFO_MESSAGE.format(
  119.             user_info=construct_user_info(update),
  120.             image_info=construct_image_response_info(update, seed)
  121.         )
  122.     )
  123.  
  124.  
  125. application = ApplicationBuilder().token(BOT_TOKEN).build()
  126.  
  127. start_handler = CommandHandler('start', start_bot_command)
  128. application.add_handler(start_handler)
  129.  
  130. message_handler = MessageHandler(
  131.     filters.TEXT &
  132.     (~filters.COMMAND) &
  133.     (~filters.UpdateType.EDITED_MESSAGE) &
  134.     (~filters.ChatType.SUPERGROUP),
  135.     process_image_generation_request
  136. )
  137. application.add_handler(message_handler)
  138.  
  139. application.run_polling()
Add Comment
Please, Sign In to add comment