diff --git a/bot.py b/bot.py index c8dcf22..b88b4d1 100644 --- a/bot.py +++ b/bot.py @@ -7,7 +7,7 @@ from tgbot.shit.fed import build_fediverse_inline_results, extract_content from tgbot.config import TOKEN, PROMPTING_USERS from tgbot.shit.handlers import handle_xitter, handle_red_ebalo, handle_hentai, handle_cute_button from tgbot.shit.prompting import gen_image -from tgbot.shit.render import render_text_on_image, image_to_string +from tgbot.shit.render import render_text_on_image, image_to_string, encode_text, decode_text from io import BytesIO logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', @@ -24,6 +24,16 @@ async def start(update: Update, context: ContextTypes.DEFAULT_TYPE): async def handle_text(update: Update, context: ContextTypes.DEFAULT_TYPE): + if update.message.text.startswith("encode"): + text = update.message.text.replace("encode", "") + await context.bot.send_message(chat_id=update.effective_chat.id, text=encode_text(text)) + return + + if update.message.text.startswith("decode"): + text = update.message.text.replace("decode", "") + await context.bot.send_message(chat_id=update.effective_chat.id, text=decode_text(text)) + return + if not update.message.text.startswith("prompting") or update.message.from_user["username"] not in PROMPTING_USERS: file = render_text_on_image("tgbot/assets/red_ebalo.png", update.message.text) await context.bot.send_sticker(chat_id=update.effective_chat.id, sticker=file) diff --git a/tgbot/shit/render.py b/tgbot/shit/render.py index ee69d67..a86a57b 100644 --- a/tgbot/shit/render.py +++ b/tgbot/shit/render.py @@ -98,3 +98,20 @@ def image_to_string(image_file): pixel_string += " " # New line for each row return pixel_string + + +def encode_text(text: str) -> str: + binary = ''.join(format(byte, '08b') for byte in text.encode('utf-8')) + encoded = ' '.join("человек" if bit == '0' else "яйца" for bit in binary) + return encoded + + +def decode_text(encoded: str) -> str: + words = encoded.split() + binary = ''.join('0' if word == "человек" else '1' for word in words) + try: + bytes_list = [binary[i:i + 8] for i in range(0, len(binary), 8)] + decoded_bytes = bytes(int(b, 2) for b in bytes_list if len(b) == 8) + return decoded_bytes.decode('utf-8') + except (ValueError, UnicodeDecodeError): + return "Decode error :<" \ No newline at end of file