From cb89af85c520609436b51f961e106821586fbd4b Mon Sep 17 00:00:00 2001 From: Delitel Date: Tue, 19 Sep 2023 23:03:44 +0300 Subject: [PATCH] =?UTF-8?q?=D0=A1=D0=B8=D0=BD=D1=85=D1=80=D0=BE=D0=BD?= =?UTF-8?q?=D0=B8=D0=B7=D0=B0=D1=86=D0=B8=D1=8F=20=D0=9D=D0=B0=D0=B7=D0=B2?= =?UTF-8?q?=D0=B0=D0=BD=D0=B8=D1=8F=20=D0=B8=20=D0=A4=D0=BE=D1=82=D0=BE=20?= =?UTF-8?q?=D1=87=D0=B0=D1=82=D0=B0=20=D0=BF=D1=80=D0=B8=20=D1=81=D0=B2?= =?UTF-8?q?=D1=8F=D0=B7=D0=BA=D0=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- sync/tg/handlers/bind_chats.py | 25 +++++++++++++++++++++++-- sync/tg/utils/__init__.py | 2 ++ sync/tg/utils/chats.py | 15 +++++++++++++++ sync/tg/utils/photos.py | 3 ++- 4 files changed, 42 insertions(+), 3 deletions(-) create mode 100644 sync/tg/utils/__init__.py create mode 100644 sync/tg/utils/chats.py diff --git a/sync/tg/handlers/bind_chats.py b/sync/tg/handlers/bind_chats.py index 97dd212..2fc0a10 100644 --- a/sync/tg/handlers/bind_chats.py +++ b/sync/tg/handlers/bind_chats.py @@ -8,6 +8,7 @@ from ...db import Sessions from ...db.models import Conversations from sqlalchemy import select, or_ +from ..utils import update_chat_info @dp.message_handler(commands=["bind_chat"], state="*") @@ -49,7 +50,7 @@ async def get_vk_id(message: Message, state: FSMContext): if data["chat_type"] == "DM": chat = await vk.api.users.get(message.text) if chat: - data["vk_chat_id"] = 2000000000 + int(message.text) + data["vk_chat_id"] = message.text await state.set_data(data) await BindChats.verify_choice_vk.set() @@ -139,7 +140,7 @@ async def get_tg_id(message: Message, state: FSMContext): select(Conversations).where( or_( Conversations.vk_id == data["vk_chat_id"], - Conversations.tg_id == message.text + Conversations.tg_id == message.text, ) ) ) @@ -153,6 +154,26 @@ async def get_tg_id(message: Message, state: FSMContext): await message.answer("Отлично!\n" "Чаты успешно связаны!") await state.finish() + + if data["chat_type"] == "GR": + chat = await vk.api.messages.get_conversations_by_id( + data["vk_chat_id"] + ) + await update_chat_info( + message.text, + chat.items[0].chat_settings.title, + preview=chat.items[0].chat_settings.photo.photo_200 + if chat.items[0].chat_settings.photo + else None, + ) + else: + chat = await vk.api.users.get( + data["vk_chat_id"], fields=["photo_max_orig"] + ) + full_name = f"{chat[0].first_name} {chat[0].last_name}" + await update_chat_info( + message.text, full_name, preview=chat[0].photo_max_orig + ) else: await message.answer("Чат уже к чему-то подключен!") diff --git a/sync/tg/utils/__init__.py b/sync/tg/utils/__init__.py new file mode 100644 index 0000000..f4a48be --- /dev/null +++ b/sync/tg/utils/__init__.py @@ -0,0 +1,2 @@ +from .chats import update_chat_info +from .photos import get_photo_from_url diff --git a/sync/tg/utils/chats.py b/sync/tg/utils/chats.py new file mode 100644 index 0000000..dd5320a --- /dev/null +++ b/sync/tg/utils/chats.py @@ -0,0 +1,15 @@ +from ..core import bot +from .photos import get_photo_from_url + + +async def update_chat_info(chat_id: int, title: str, preview: str): + """ + Обновление информации о группе. + Название, Фото + """ + + if preview: + photo = await get_photo_from_url(preview) + await bot.set_chat_photo(chat_id, photo) + + await bot.set_chat_title(chat_id, title) diff --git a/sync/tg/utils/photos.py b/sync/tg/utils/photos.py index 92bea48..af22099 100644 --- a/sync/tg/utils/photos.py +++ b/sync/tg/utils/photos.py @@ -1,6 +1,7 @@ import aiohttp from io import BytesIO + async def get_photo_from_url(photo_url): try: async with aiohttp.ClientSession() as session: @@ -11,4 +12,4 @@ async def get_photo_from_url(photo_url): else: return None except Exception as e: - return None \ No newline at end of file + return None