| import os |
| import requests |
| from fastapi import FastAPI, Request |
| from aiogram import Bot, Dispatcher, types |
| from scoring import analyze_full_submission |
| from database import Database |
|
|
| BOT_TOKEN = os.environ.get("BOT_TOKEN") |
| if not BOT_TOKEN: |
| raise SystemExit("BOT_TOKEN is missing! Add it in Space → Settings → Variables") |
|
|
| WEBHOOK_URL = os.environ.get("WEBHOOK_URL") |
|
|
| app = FastAPI() |
| bot = Bot(token=BOT_TOKEN) |
| dp = Dispatcher() |
| db = Database("cefr.db") |
|
|
|
|
| @app.get("/") |
| def home(): |
| return {"status": "ok", "message": "CEFR Bot Running"} |
|
|
|
|
| @dp.message() |
| async def main_handler(message: types.Message): |
| text = message.text or "" |
| submission = {"task1_1": text, "task1_2": "", "task2": ""} |
| result = await analyze_full_submission(submission) |
|
|
| await message.answer( |
| f"Total score: {result['total']}/75\n" |
| f"Level: {result['level']}\n\n" |
| f"Feedback:\n{result['feedback']}" |
| ) |
|
|
|
|
| @app.post("/webhook") |
| async def webhook(request: Request): |
| update = types.Update(**await request.json()) |
| await dp.feed_update(bot, update) |
| return {"ok": True} |
|
|
|
|
| @app.on_event("startup") |
| async def on_startup(): |
| if WEBHOOK_URL: |
| try: |
| url = f"https://api.telegram.org/bot{BOT_TOKEN}/setWebhook?url={WEBHOOK_URL}" |
| r = requests.get(url) |
| print("Webhook response:", r.text) |
| except Exception as e: |
| print("Webhook error:", e) |
| else: |
| print("WEBHOOK_URL is missing!") |
|
|