-
Notifications
You must be signed in to change notification settings - Fork 0
/
telegram_track_sender.py
77 lines (64 loc) · 2.63 KB
/
telegram_track_sender.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import logging
from datetime import timedelta
from aiogram import Bot
from aiogram.types import InlineKeyboardButton, InlineKeyboardMarkup, ParseMode
from models.track_source import TrackSource
from telegram_track_sender.track_formatter import format_track_markdown
_logger = logging.getLogger(__name__)
def create_reply_source_url_keyboard(sources: list[TrackSource]):
if not sources:
return
keyboard = InlineKeyboardMarkup(row_width=1)
for button in [
InlineKeyboardButton(
text=source.name,
url=source.url
)
for source in sources
]:
keyboard.add(button)
return keyboard
class TelegramTrackSender:
bot: Bot
def __init__(self, token: str, chat_id: int):
self.bot = Bot(token=token)
self.chat_id = chat_id
async def send_track(self,
title: str,
description: str,
podcast: str,
duration: timedelta,
track_sources: list[TrackSource],
tags: list[str]):
"""
Отправить новое сообщение и вернуть его ID
:param title: Заголовок трека
:param description: Описание трека
:param podcast: Название подкаста
:param duration: Длительность трека
:param track_sources: Источники, где можно послушать
:param tags: Тэги трека/подкаста
:return: ID отправленного сообщения
"""
formatted_message = format_track_markdown(title, description, podcast, duration, tags)
keyboard = create_reply_source_url_keyboard(track_sources)
message = await self.bot.send_message(
self.chat_id,
formatted_message,
parse_mode=ParseMode.HTML,
reply_markup=keyboard
)
return message.message_id
async def update_track_sources(self, message_id: int, track_sources: list[TrackSource]):
"""
Обновить источники трека, у существующего сообщения.
Все источники заменяются полностью!
:param message_id: ID сообщения
:param track_sources: Источники трека
"""
keyboard = create_reply_source_url_keyboard(track_sources)
await self.bot.edit_message_reply_markup(
chat_id=self.chat_id,
message_id=message_id,
reply_markup=keyboard
)