feat: Telegram bot — full functionality with inline keyboards, file upload, auth

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-04-03 22:46:20 +05:00
parent 397b999558
commit 922b1b26a6
8 changed files with 439 additions and 0 deletions
+59
View File
@@ -0,0 +1,59 @@
from aiogram import Bot, Dispatcher
from aiogram.webhook.aiohttp_server import SimpleRequestHandler
from fastapi import APIRouter, Request, Response
from app.config import settings
from app.messengers.base import MessengerService
from app.messengers.telegram.handlers import router as handlers_router
api_router = APIRouter()
class TelegramService(MessengerService):
def __init__(self):
self.bot = Bot(token=settings.telegram_bot_token)
self.dp = Dispatcher()
self.dp.include_router(handlers_router)
async def setup(self):
if settings.telegram_webhook_url:
await self.bot.set_webhook(
settings.telegram_webhook_url,
drop_pending_updates=True,
)
async def shutdown(self):
await self.bot.delete_webhook()
await self.bot.session.close()
async def send_message(self, chat_id: str, text: str, **kwargs):
await self.bot.send_message(int(chat_id), text, **kwargs)
async def send_file(self, chat_id: str, file_path: str, caption: str = ""):
from aiogram.types import FSInputFile
await self.bot.send_document(
int(chat_id),
FSInputFile(file_path),
caption=caption,
)
async def notify_progress(self, chat_id: str, status: str, progress: int | None = None):
text = f"⚙️ {status}"
if progress is not None:
bar = "" * (progress // 10) + "" * (10 - progress // 10)
text += f"\n{bar} {progress}%"
await self.send_message(chat_id, text)
async def feed_webhook(self, request: Request) -> Response:
update = await request.json()
from aiogram.types import Update
await self.dp.feed_update(self.bot, Update(**update))
return Response(status_code=200)
telegram_service = TelegramService()
@api_router.post("/api/telegram/webhook")
async def telegram_webhook(request: Request):
return await telegram_service.feed_webhook(request)