diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..2eea525 --- /dev/null +++ b/.dockerignore @@ -0,0 +1 @@ +.env \ No newline at end of file diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..ce759d0 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,7 @@ +FROM python:3.11 + +COPY . . + +RUN pip install -r requirements.txt + +CMD python3 bot.py diff --git a/arial-unicode-ms-regular.ttf b/arial-unicode-ms-regular.ttf new file mode 100644 index 0000000..51a18bc Binary files /dev/null and b/arial-unicode-ms-regular.ttf differ diff --git a/bot.py b/bot.py new file mode 100644 index 0000000..7cc702e --- /dev/null +++ b/bot.py @@ -0,0 +1,57 @@ + +from telegram import Update, InlineKeyboardMarkup, InlineKeyboardButton +from telegram.ext import ApplicationBuilder, ContextTypes, CommandHandler, MessageHandler, filters, InlineQueryHandler +import logging +from render import render_text_on_image + +import os + + +logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', + level=logging.INFO) + +logger = logging.getLogger(__name__) + +TOKEN = os.getenv("TOKEN") + +async def start(update: Update, context: ContextTypes.DEFAULT_TYPE): + await context.bot.send_message(chat_id=update.effective_chat.id, text='Hi! I respond with a picture to any message.') + +async def respond_with_picture(update: Update, context: ContextTypes.DEFAULT_TYPE): + file = render_text_on_image("red_ebalo.png", update.message.text) + await context.bot.send_sticker(chat_id=update.effective_chat.id, sticker=file) + +async def inline_respond_with_sticker(update: Update, context: ContextTypes.DEFAULT_TYPE): + + query = update.inline_query.query + if not query: + return + + bot_info = await context.bot.get_me() + + file = render_text_on_image("red_ebalo.png", query) + + file_id = (await context.bot.send_sticker(chat_id=-1002471390283, sticker=file)).sticker.file_id + results = [ + { + "type": "sticker", + "id": "1", + "sticker_file_id": file_id + } + ] + await context.bot.answer_inline_query(update.inline_query.id, results) + +def main(): + application = ApplicationBuilder().token(TOKEN).build() + + start_handler = CommandHandler('start', start) + picture_handler = MessageHandler(filters.TEXT & ~filters.COMMAND, respond_with_picture) + + application.add_handler(start_handler) + application.add_handler(picture_handler) + application.add_handler(InlineQueryHandler(inline_respond_with_sticker)) + + application.run_polling() + +if __name__ == '__main__': + main() diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..a3c7484 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,6 @@ +services: + owlrandomshitbot: + image: owlrandomshitbot + build: + context: . + env_file: ".env" \ No newline at end of file diff --git a/red_ebalo.png b/red_ebalo.png new file mode 100644 index 0000000..a1d9edd Binary files /dev/null and b/red_ebalo.png differ diff --git a/render.py b/render.py new file mode 100644 index 0000000..a51eff5 --- /dev/null +++ b/render.py @@ -0,0 +1,70 @@ +from PIL import Image, ImageDraw, ImageFont +import sys +import io + + +def render_text_on_image(input_image_path, text_to_draw, output_image_path=None, font_path="arial-unicode-ms-regular.ttf", font_size=52): + image = Image.open(input_image_path) + + draw = ImageDraw.Draw(image) + + font = ImageFont.truetype(font_path, font_size) + + text_color = (255, 255, 255) + + image_width, image_height = image.size + + def wrap_text(text, draw, font, max_width): + words = text.split() + lines = [] + current_line = [] + current_line_width = 0 + + for word in words: + word_width, word_height = draw.textbbox((0, 0), word, font=font)[2:4] + space_width = draw.textbbox((0, 0), ' ', font=font)[2] + + if current_line and (current_line_width + word_width + space_width) > max_width: + lines.append(' '.join(current_line)) + current_line = [word] + current_line_width = word_width + else: + current_line.append(word) + current_line_width += word_width + space_width + + if current_line: + lines.append(' '.join(current_line)) + + return lines + + lines = wrap_text(text_to_draw, draw, font, image_width - 20) + + total_text_height = sum([draw.textbbox((0, 0), line, font=font)[3] for line in lines]) + + current_y = 10 + + for line in lines: + draw.text((10, current_y), line, font=font, fill=text_color) + + current_y += draw.textbbox((0, 0), line, font=font)[3] + 5 + + if current_y > image_height: + break + + if output_image_path: + image.save(output_image_path) + + else: + file = io.BytesIO() + + image.thumbnail((512, 512)) + + image.save(file, format="WEBP") + + file.seek(0) + + return file + +if __name__ == "__main__": + + render_text_on_image("red_ebalo.png", " ".join(sys.argv[1:]), output_image_path="red_ebalo_text.png") diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..38a86a4 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,9 @@ +anyio==4.6.2.post1 +certifi==2024.8.30 +h11==0.14.0 +httpcore==1.0.6 +httpx==0.27.2 +idna==3.10 +pillow==11.0.0 +python-telegram-bot==21.6 +sniffio==1.3.1