Skip to content

Commit

Permalink
Обработка голосовых сообщений из вк в телеграм
Browse files Browse the repository at this point in the history
  • Loading branch information
Delitel-WEB committed Sep 20, 2023
1 parent 0dd67f5 commit 28d0fcf
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 4 deletions.
2 changes: 1 addition & 1 deletion sync/tg/utils/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
from .chats import update_chat_info
from .chats import update_chat_info
2 changes: 1 addition & 1 deletion sync/utils/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
from .files import get_file_from_url
from .files import get_file_from_url
1 change: 1 addition & 0 deletions sync/vk/handlers/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
from . import voices
from . import messages
4 changes: 2 additions & 2 deletions sync/vk/handlers/messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ async def on_message(message: Message):
select(Conversations).where(Conversations.vk_id == message.peer_id)
)
if bundle:
users_info = await bot.api.users.get(message.from_id)
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)}'>{users_info[0].first_name} {users_info[0].last_name}</a>\n"
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",
Expand Down
28 changes: 28 additions & 0 deletions sync/vk/handlers/voices.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
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


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

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",
)

0 comments on commit 28d0fcf

Please sign in to comment.