red ebalo

This commit is contained in:
owl 2024-10-25 23:45:07 +07:00
parent a2fa597921
commit 023fd3207d
8 changed files with 150 additions and 0 deletions

1
.dockerignore Normal file
View File

@ -0,0 +1 @@
.env

7
Dockerfile Normal file
View File

@ -0,0 +1,7 @@
FROM python:3.11
COPY . .
RUN pip install -r requirements.txt
CMD python3 bot.py

Binary file not shown.

57
bot.py Normal file
View File

@ -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()

6
docker-compose.yml Normal file
View File

@ -0,0 +1,6 @@
services:
owlrandomshitbot:
image: owlrandomshitbot
build:
context: .
env_file: ".env"

BIN
red_ebalo.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 198 KiB

70
render.py Normal file
View File

@ -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")

9
requirements.txt Normal file
View File

@ -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