Skip to content

Commit

Permalink
Проверка связки чатов сделана в виде декоратора
Browse files Browse the repository at this point in the history
  • Loading branch information
Delitel-WEB committed Sep 23, 2023
1 parent 82e6a46 commit 8caffee
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 115 deletions.
31 changes: 12 additions & 19 deletions sync/vk/handlers/messages.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,18 @@
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 create_vk_link, check_bundle


@bot.on.message()
async def on_message(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)
await tg.send_message(
bundle.tg_id,
f"<a href='{create_vk_link(message.from_id)}'>{user_info[0].first_name} {user_info[0].last_name}</a>\n"
+ "‾" * 10
+ f"\n{message.text}",
parse_mode="html",
disable_web_page_preview=True,
)
@check_bundle
async def on_message(message: Message, bundle):
user_info = await bot.api.users.get(message.from_id)
await tg.send_message(
bundle.tg_id,
f"<a href='{create_vk_link(message.from_id)}'>{user_info[0].first_name} {user_info[0].last_name}</a>\n"
+ "‾" * 10
+ f"\n{message.text}",
parse_mode="html",
disable_web_page_preview=True,
)
49 changes: 21 additions & 28 deletions sync/vk/handlers/photos.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
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 create_vk_link, check_bundle
from ...utils import get_file_from_url
from vkbottle.dispatch.rules import ABCRule
from aiogram.types import InputMediaPhoto
Expand All @@ -27,31 +24,27 @@ async def check(self, event: Message):


@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"<a href='{create_vk_link(message.from_id)}'>{user_info[0].first_name} {user_info[0].last_name}</a>\n"
if message.text:
text += "‾" * 10
text += f"\n{message.text}"
@check_bundle
async def on_photo(message: Message, bundle):
user_info = await bot.api.users.get(message.from_id)
text = f"<a href='{create_vk_link(message.from_id)}'>{user_info[0].first_name} {user_info[0].last_name}</a>\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)
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):
Expand Down
33 changes: 13 additions & 20 deletions sync/vk/handlers/stickers.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
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 create_vk_link, check_bundle
from ...utils import get_file_from_url
from vkbottle.dispatch.rules import ABCRule

Expand All @@ -26,19 +23,15 @@ async def check(self, event: Message):


@bot.on.message(StrickersRule())
async def on_stickers(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)
sticker = await get_file_from_url(
message.attachments[0].sticker.images_with_background[-1].url
)
sticker_message = await tg.send_sticker(bundle.tg_id, sticker=sticker)
await sticker_message.reply(
f"<a href='{create_vk_link(message.from_id)}'>{user_info[0].first_name} {user_info[0].last_name}</a>\n",
parse_mode="html",
disable_web_page_preview=True,
)
@check_bundle
async def on_stickers(message: Message, bundle):
user_info = await bot.api.users.get(message.from_id)
sticker = await get_file_from_url(
message.attachments[0].sticker.images_with_background[-1].url
)
sticker_message = await tg.send_sticker(bundle.tg_id, sticker=sticker)
await sticker_message.reply(
f"<a href='{create_vk_link(message.from_id)}'>{user_info[0].first_name} {user_info[0].last_name}</a>\n",
parse_mode="html",
disable_web_page_preview=True,
)
47 changes: 20 additions & 27 deletions sync/vk/handlers/videos.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
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 create_vk_link, check_bundle
from ...utils import get_file_from_url
from vkbottle.dispatch.rules import ABCRule
from aiogram.types import InputMediaVideo
Expand All @@ -27,31 +24,27 @@ async def check(self, event: Message):


@bot.on.message(VideoRule())
async def on_video(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"<a href='{create_vk_link(message.from_id)}'>{user_info[0].first_name} {user_info[0].last_name}</a>\n"
if message.text:
text += "‾" * 10
text += f"\n{message.text}"
@check_bundle
async def on_video(message: Message, bundle):
user_info = await bot.api.users.get(message.from_id)
text = f"<a href='{create_vk_link(message.from_id)}'>{user_info[0].first_name} {user_info[0].last_name}</a>\n"
if message.text:
text += "‾" * 10
text += f"\n{message.text}"

videos = []
for index, attachment in enumerate(message.attachments):
if attachment and attachment.video:
videos.append(
InputMediaVideo(
await get_file_from_url(
await get_best_quality_video(attachment.video.files)
),
parse_mode="html",
caption=text if index == 0 else None,
)
videos = []
for index, attachment in enumerate(message.attachments):
if attachment and attachment.video:
videos.append(
InputMediaVideo(
await get_file_from_url(
await get_best_quality_video(attachment.video.files)
),
parse_mode="html",
caption=text if index == 0 else None,
)
await tg.send_media_group(bundle.tg_id, videos)
)
await tg.send_media_group(bundle.tg_id, videos)


async def get_best_quality_video(files):
Expand Down
33 changes: 12 additions & 21 deletions sync/vk/handlers/voices.py
Original file line number Diff line number Diff line change
@@ -1,29 +1,20 @@
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 create_vk_link, check_bundle
from ...utils import get_file_from_url


@bot.on.message(attachment=["audio_message"])
async def on_audio_message(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)
voice_file = await get_file_from_url(
message.attachments[0].audio_message.link_ogg
)
@check_bundle
async def on_audio_message(message: Message, bundle):
user_info = await bot.api.users.get(message.from_id)
voice_file = await get_file_from_url(message.attachments[0].audio_message.link_ogg)

await tg.send_voice(
bundle.tg_id,
voice_file,
f"<a href='{create_vk_link(message.from_id)}'>{user_info[0].first_name} {user_info[0].last_name}</a>\n",
parse_mode="html",
disable_notification=True,
)
await tg.send_voice(
bundle.tg_id,
voice_file,
f"<a href='{create_vk_link(message.from_id)}'>{user_info[0].first_name} {user_info[0].last_name}</a>\n",
parse_mode="html",
disable_notification=True,
)
1 change: 1 addition & 0 deletions sync/vk/utils/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
from .links import create_vk_link
from .bundle import check_bundle
18 changes: 18 additions & 0 deletions sync/vk/utils/bundle.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
from ...db import Sessions
from ...db.models import Conversations
from sqlalchemy import select
from functools import wraps
from vkbottle.user import Message


def check_bundle(func):
@wraps(func)
async def wrapper(message: Message, *args):
async with Sessions() as session:
bundle = await session.scalar(
select(Conversations).where(Conversations.vk_id == message.peer_id)
)
if bundle:
await func(message, bundle, *args)

return wrapper

0 comments on commit 8caffee

Please sign in to comment.