Skip to content

Commit

Permalink
Синхронизация Названия и Фото чата при связке
Browse files Browse the repository at this point in the history
  • Loading branch information
Delitel-WEB committed Sep 19, 2023
1 parent 3f057f0 commit cb89af8
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 3 deletions.
25 changes: 23 additions & 2 deletions sync/tg/handlers/bind_chats.py
Original file line number Diff line number Diff line change
Expand Up @@ -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="*")
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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,
)
)
)
Expand All @@ -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("Чат уже к чему-то подключен!")

Expand Down
2 changes: 2 additions & 0 deletions sync/tg/utils/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
from .chats import update_chat_info
from .photos import get_photo_from_url
15 changes: 15 additions & 0 deletions sync/tg/utils/chats.py
Original file line number Diff line number Diff line change
@@ -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)
3 changes: 2 additions & 1 deletion sync/tg/utils/photos.py
Original file line number Diff line number Diff line change
@@ -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:
Expand All @@ -11,4 +12,4 @@ async def get_photo_from_url(photo_url):
else:
return None
except Exception as e:
return None
return None

0 comments on commit cb89af8

Please sign in to comment.