/
a-services
/
echo-bot
Обзор
Документация
Войти
/
a-services
/
echo-bot
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
dep
src/main.py
58 строк
1 KB
ad0x
first_commit
04 янв 2026, 14:32
04 янв 2026, 14:32
c649a7a
Код
Авторство
О чём код?
from pathlib import Path from dotenv import load_dotenv import os from fastapi import FastAPI, Request from telegram import Update from telegram.ext import Application, MessageHandler, ContextTypes, filters # ---- load .env ---- BASE_DIR = Path(__file__).resolve().parent.parent load_dotenv(BASE_DIR / ".env", override=True) BOT_TOKEN = os.getenv("BOT_TOKEN") APP_HOST = os.getenv("APP_HOST", "0.0.0.0") APP_PORT = int(os.getenv("APP_PORT", "8000")) if not BOT_TOKEN: raise RuntimeError("BOT_TOKEN is not set") # ---- telegram bot ---- tg_app = Application.builder().token(BOT_TOKEN).build() async def echo(update: Update, context: ContextTypes.DEFAULT_TYPE): if update.message: await update.message.reply_text(update.message.text) tg_app.add_handler(MessageHandler(filters.TEXT & ~filters.COMMAND, echo)) # ---- fastapi ---- api = FastAPI() @api.on_event("startup") async def startup(): await tg_app.initialize() await tg_app.bot.set_webhook( url=f"https://{os.getenv('PUBLIC_DOMAIN')}/webhook" ) @api.post("/webhook") async def telegram_webhook(request: Request): data = await request.json() update = Update.de_json(data, tg_app.bot) await tg_app.process_update(update) return {"ok": True} def application() -> FastAPI: return api if __name__ == "__main__": import uvicorn uvicorn.run( "src.main:application", host=APP_HOST, port=APP_PORT, factory=True, )