From e30f774340da4ec3be7c64f0e9802196d53a5a05 Mon Sep 17 00:00:00 2001 From: Delitel Date: Thu, 21 Sep 2023 22:04:51 +0300 Subject: [PATCH 1/2] =?UTF-8?q?=D0=9E=D0=B1=D1=80=D0=B0=D0=B1=D0=BE=D1=82?= =?UTF-8?q?=D0=BA=D0=B0=20=D1=84=D0=BE=D1=82=D0=BE=20=D0=B8=D0=B7=20=D0=B2?= =?UTF-8?q?=D0=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 +- sync/vk/handlers/__init__.py | 1 + sync/vk/handlers/photos.py | 72 ++++++++++++++++++++++++++++++++++++ 3 files changed, 74 insertions(+), 1 deletion(-) create mode 100644 sync/vk/handlers/photos.py diff --git a/README.md b/README.md index f835ed7..75d3bf0 100644 --- a/README.md +++ b/README.md @@ -76,7 +76,7 @@ python main.py | Удаление сообщений | ❌ | ❌ | | Редактирование сообщений | ❌ | ❌ | | **Медиа** | | | -| Фото | ❌ | ❌ | +| Фото | ❌ | ✅ | | Видео | ❌ | ❌ | | Геолокация | ❌ | ❌ | | Гифки | ❌ | ❌ | diff --git a/sync/vk/handlers/__init__.py b/sync/vk/handlers/__init__.py index 000b6b2..72679ce 100644 --- a/sync/vk/handlers/__init__.py +++ b/sync/vk/handlers/__init__.py @@ -1,3 +1,4 @@ +from . import photos from . import stickers from . import voices from . import messages diff --git a/sync/vk/handlers/photos.py b/sync/vk/handlers/photos.py new file mode 100644 index 0000000..56e68e3 --- /dev/null +++ b/sync/vk/handlers/photos.py @@ -0,0 +1,72 @@ +from ..core import bot +from vkbottle.user import Message +from ...db import Sessions +from ...db.models import Conversations +from sqlalchemy import select +from ...tg.core import bot as tg +from ..utils import create_vk_link +from ...utils import get_file_from_url +from vkbottle.dispatch.rules import ABCRule +from aiogram.types import InputMediaPhoto + + +class PhotoRule(ABCRule[Message]): + """ + Кастомное правило которое будет срабатывать если + будет отправлено фото + """ + + async def check(self, event: Message): + if event.attachments: + if event.attachments[0].photo: + return True + else: + return False + else: + return False + + +@bot.on.message(PhotoRule()) +async def on_photo(message: Message): + async with Sessions() as session: + bundle = await session.scalar( + select(Conversations).where(Conversations.vk_id == message.peer_id) + ) + if bundle: + user_info = await bot.api.users.get(message.from_id) + text = f"{user_info[0].first_name} {user_info[0].last_name}\n" + if message.text: + text += "_" * 10 + text += f"\n{message.text}" + + photos = [] + for index, attachment in enumerate(message.attachments): + if attachment and attachment.photo: + photos.append( + InputMediaPhoto( + await get_file_from_url( + await get_best_quality_image(attachment.photo.sizes) + ), + parse_mode="html", + caption=text if index == 0 else None, + ) + ) + await tg.send_media_group(bundle.tg_id, photos) + + +async def get_best_quality_image(sizes: list): + max_quality = None + max_quality_url = None + + for image_info in sizes: + width = image_info.width + height = image_info.height + + if width > 0 and height > 0: + if max_quality is None or ( + width > max_quality.width and height > max_quality.height + ): + max_quality = image_info + max_quality_url = image_info.url + + return max_quality_url From 5bb3e3e496780772f04032d6d47fc4664fa84e66 Mon Sep 17 00:00:00 2001 From: Delitel Date: Thu, 21 Sep 2023 22:21:01 +0300 Subject: [PATCH 2/2] =?UTF-8?q?=D0=A1=D0=B8=D0=BC=D0=B2=D0=BE=D0=BB=20?= =?UTF-8?q?=D0=BE=D1=82=D0=B4=D0=B5=D0=BB=D1=8F=D1=8E=D1=89=D0=B8=D0=B9=20?= =?UTF-8?q?=D0=BA=D0=BE=D0=BD=D1=82=D0=B5=D0=BD=D1=82=20=D0=B8=D0=B7=D0=BC?= =?UTF-8?q?=D0=B5=D0=BD=D1=91=D0=BD=20=D0=BD=D0=B0=20`=E2=80=BE`?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- sync/vk/handlers/messages.py | 2 +- sync/vk/handlers/photos.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/sync/vk/handlers/messages.py b/sync/vk/handlers/messages.py index 2f705c7..3cee187 100644 --- a/sync/vk/handlers/messages.py +++ b/sync/vk/handlers/messages.py @@ -18,7 +18,7 @@ async def on_message(message: Message): await tg.send_message( bundle.tg_id, f"{user_info[0].first_name} {user_info[0].last_name}\n" - + "_" * 10 + + "‾" * 10 + f"\n{message.text}", parse_mode="html", ) diff --git a/sync/vk/handlers/photos.py b/sync/vk/handlers/photos.py index 56e68e3..1f68f65 100644 --- a/sync/vk/handlers/photos.py +++ b/sync/vk/handlers/photos.py @@ -36,7 +36,7 @@ async def on_photo(message: Message): user_info = await bot.api.users.get(message.from_id) text = f"{user_info[0].first_name} {user_info[0].last_name}\n" if message.text: - text += "_" * 10 + text += "‾" * 10 text += f"\n{message.text}" photos = []