Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from telegram import Update
- from telegram.ext import ApplicationBuilder, ContextTypes, CommandHandler, MessageHandler, filters
- from telegram import constants
- from telegram.helpers import escape_markdown
- from PIL import Image
- from io import BytesIO
- BOT_TOKEN = "..."
- WELCOME_MESSAGE = escape_markdown(
- "Send me a prompt and I will generate a phot of Oleh Kopyl\n"
- "All prompts should include `kopyl` keyword\n\n"
- "For example: `Photo of kopyl riding on a huge cock`"
- )
- NO_KOPYL_MENTION_IN_PROMPT_MESSAGE = (
- "You need to include `kopyl` keyword somewhere in your prompt, otherwise i can't generate anything\n\n"
- "For example: `Photo of kopyl riding on a huge cock`"
- )
- USER_INVALID_REQUEST_INFO_MESSAGE = (
- "A user requested an image with an invalid prompt (no 'kopyl' keyword in it)\n\n"
- "{user_info}\n\n"
- "{image_info}"
- )
- USER_RESPONSE_INFO_MESSAGE = (
- "{user_info}\n\n"
- "{image_info}"
- )
- FRIENDS_GROUP_CHAT_ID = -100xxx
- ADMIN_CHAT_ID = xxx
- REPORTING_CHAT_ID = FRIENDS_GROUP_CHAT_ID
- def construct_user_info(update):
- user_id = update.effective_user.id
- username = update.effective_user.username
- first_name = update.effective_user.first_name
- last_name = update.effective_user.last_name
- info = (
- (
- f"`{user_id}` \n"
- f"@{username}\n" if username else ""
- ) +
- (f"{first_name} " if first_name else "") +
- f"{last_name} " if last_name else ""
- )
- return info
- def construct_image_request_info(update):
- return f"Prompt: {escape_markdown(update.message.text)}"
- def construct_image_response_info(update, seed):
- return (
- f"Prompt: `{escape_markdown(update.message.text)}`\n"
- f"Seed: `{seed}`"
- )
- def prompt_is_not_valid(prompt):
- if "kopyl" in prompt.lower():
- return
- return True
- def generate_seed() -> int:
- ...
- def convert_PIL_image_to_sendable_photo(pil_image: Image.Image):
- stream = BytesIO()
- pil_image.save(stream, "PNG")
- stream.seek(0)
- return stream
- def generate_image(prompt: str) -> [Image.Image, int]:
- ...
- async def start_bot_command(update: Update, context: ContextTypes.DEFAULT_TYPE):
- await update.message.reply_markdown(WELCOME_MESSAGE)
- async def process_image_generation_request(update: Update, context: ContextTypes.DEFAULT_TYPE):
- if prompt_is_not_valid(update.message.text):
- await context.bot.send_message(
- REPORTING_CHAT_ID,
- parse_mode=constants.ParseMode.MARKDOWN,
- disable_notification=True,
- text=USER_INVALID_REQUEST_INFO_MESSAGE.format(
- user_info=construct_user_info(update),
- image_info=construct_image_request_info(update),
- ),
- )
- return await update.message.reply_markdown(
- NO_KOPYL_MENTION_IN_PROMPT_MESSAGE
- )
- await update.message.reply_text("Generating...")
- image, seed = generate_image(update.message.text)
- image_bytes = convert_PIL_image_to_sendable_photo(image)
- sent_photo = await context.bot.send_photo(
- update.effective_chat.id, image_bytes,
- parse_mode=constants.ParseMode.MARKDOWN,
- caption=construct_image_response_info(update, seed)
- )
- await context.bot.copy_message(
- REPORTING_CHAT_ID,
- update.effective_chat.id,
- sent_photo.message_id,
- parse_mode=constants.ParseMode.MARKDOWN,
- caption=USER_RESPONSE_INFO_MESSAGE.format(
- user_info=construct_user_info(update),
- image_info=construct_image_response_info(update, seed)
- )
- )
- application = ApplicationBuilder().token(BOT_TOKEN).build()
- start_handler = CommandHandler('start', start_bot_command)
- application.add_handler(start_handler)
- message_handler = MessageHandler(
- filters.TEXT &
- (~filters.COMMAND) &
- (~filters.UpdateType.EDITED_MESSAGE) &
- (~filters.ChatType.SUPERGROUP),
- process_image_generation_request
- )
- application.add_handler(message_handler)
- application.run_polling()
Add Comment
Please, Sign In to add comment