feat: backend servis iskeletini ve yönetim uçlarını ekle

This commit is contained in:
2026-03-21 11:53:04 +03:00
parent df1924b772
commit 62add37d9d
29 changed files with 953 additions and 0 deletions

View File

@@ -0,0 +1 @@

View File

@@ -0,0 +1,8 @@
from sqlalchemy.orm import Session
from app.db import AuthorizedUserORM
def is_authorized(session: Session, telegram_user_id: int) -> bool:
record = session.get(AuthorizedUserORM, telegram_user_id)
return bool(record and record.is_active)

View File

@@ -0,0 +1,51 @@
from typing import Any
from telegram import Update
from telegram.ext import Application, CommandHandler, ContextTypes, MessageHandler, filters
from app.orchestrator import WiseClawOrchestrator
class TelegramBotService:
def __init__(self, token: str, orchestrator_factory: Any) -> None:
self.token = token
self.orchestrator_factory = orchestrator_factory
self.application: Application | None = None
async def process_message(self, telegram_user_id: int, text: str) -> str:
with self.orchestrator_factory() as session:
orchestrator = WiseClawOrchestrator(session)
return orchestrator.handle_text_message(telegram_user_id=telegram_user_id, text=text)
async def start(self) -> None:
if not self.token:
return
self.application = Application.builder().token(self.token).build()
self.application.add_handler(CommandHandler("start", self._on_start))
self.application.add_handler(MessageHandler(filters.TEXT & ~filters.COMMAND, self._on_text))
await self.application.initialize()
await self.application.start()
await self.application.updater.start_polling(drop_pending_updates=True)
async def stop(self) -> None:
if self.application is None:
return
await self.application.updater.stop()
await self.application.stop()
await self.application.shutdown()
self.application = None
async def _on_start(self, update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
del context
if update.message is None or update.effective_user is None:
return
await update.message.reply_text(
"WiseClaw is online. If your Telegram user is whitelisted, send a message to start."
)
async def _on_text(self, update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
del context
if update.message is None or update.effective_user is None or update.message.text is None:
return
reply = await self.process_message(update.effective_user.id, update.message.text)
await update.message.reply_text(reply)