add send avatar on start

This commit is contained in:
bakatrouble 2020-06-02 15:50:33 +03:00
parent 29e9265483
commit c7497acafa

View File

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