Files
kwork/app/telegram.py
2026-07-17 12:21:53 +07:00

49 lines
1.5 KiB
Python

from __future__ import annotations
from aiogram import Bot
from aiogram.types import InlineKeyboardButton, InlineKeyboardMarkup, Message
from app.models import Offer
class TelegramPublisher:
def __init__(self, bot: Bot, channel_id: str) -> None:
self._bot = bot
self._channel_id = channel_id
async def publish_offer(self, offer: Offer) -> Message:
return await self._bot.send_message(
chat_id=self._channel_id,
text=_format_offer_message(offer),
disable_web_page_preview=True,
reply_markup=_build_offer_markup(),
)
def _format_offer_message(offer: Offer) -> str:
seller_line = f"Исполнитель: @{offer.seller_username}\n" if offer.seller_username else ""
description = offer.description.strip()
if len(description) > 1000:
description = f"{description[:997].rstrip()}..."
return (
f"Новый оффер: {offer.title}\n"
f"Источник: #{offer.source}\n"
f"Категория: {offer.category_id}\n"
f"Бюджет: {offer.price}\n"
f"Опубликовано: {offer.published_at}\n"
f"{seller_line}"
f"\n"
f"{description}\n\n"
f"Ссылка: {offer.url}"
)
def _build_offer_markup() -> InlineKeyboardMarkup:
return InlineKeyboardMarkup(
inline_keyboard=[
[InlineKeyboardButton(text="Принять", callback_data="accept_offer")],
[InlineKeyboardButton(text="❌", callback_data="delete_post")],
]
)