From c7497acafada63eda7fd48cd7a231a034d29a337 Mon Sep 17 00:00:00 2001 From: bakatrouble Date: Tue, 2 Jun 2020 15:50:33 +0300 Subject: [PATCH] add send avatar on start --- bots/modules/overlay.py | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/bots/modules/overlay.py b/bots/modules/overlay.py index 5a8fa71..1e425ba 100644 --- a/bots/modules/overlay.py +++ b/bots/modules/overlay.py @@ -6,7 +6,7 @@ from PIL import Image from django.db import models from django import forms from django.forms import modelformset_factory -from telegram import Update +from telegram import Update, UserProfilePhotos from telegram.ext import Dispatcher, CallbackContext, MessageHandler, Filters, CommandHandler from bots.models import TelegramBotModuleConfig @@ -16,11 +16,22 @@ class OverlayBotModuleConfig(TelegramBotModuleConfig): start_text = models.TextField(null=True, blank=True) type_error_text = models.TextField(null=True, blank=True) comment = models.TextField(blank=True, null=True) + send_avatar_on_start = models.BooleanField(default=False) MODULE_NAME = 'Overlay' def start_handler(self, update: Update, ctx: CallbackContext): - if self.start_text: + if self.send_avatar_on_start: + with TemporaryDirectory() as d: + src = os.path.join(d, 'src.jpg') + photos = update.effective_user.get_profile_photos(limit=1) # type: UserProfilePhotos + if photos.photos: + q = photos.photos[0][-1].get_file().download(src) + im = Image.open(q) + self.create_image(update, im) + elif self.type_error_text: + update.effective_message.reply_text(self.type_error_text) + elif self.start_text: update.effective_message.reply_text(self.start_text) def message_handler(self, update: Update, ctx: CallbackContext): @@ -30,9 +41,14 @@ class OverlayBotModuleConfig(TelegramBotModuleConfig): def photo_handler(self, update: Update, ctx: CallbackContext): with TemporaryDirectory() as d: src = os.path.join(d, 'src.jpg') - out = os.path.join(d, 'out.png') q = update.effective_message.photo[-1].get_file().download(src) - im = Image.open(q).convert('RGBA') # type: Image.Image + im = Image.open(q) + self.create_image(update, im) + + def create_image(self, update: Update, im: Image.Image): + with TemporaryDirectory() as d: + out = os.path.join(d, 'out.png') + im = im.convert('RGBA') # type: Image.Image overlay = Image.open(self.images.order_by('?').first().image.path).convert('RGBA') # type: Image.Image w, h = im.size min_side = min(w, h)