File size: 1,594 Bytes
0eddad1
a1ecef6
 
0eddad1
 
 
5a494ed
a1ecef6
 
 
5a494ed
 
 
 
a1ecef6
5a494ed
 
 
 
 
 
 
 
 
0eddad1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a1ecef6
 
0eddad1
 
 
5a494ed
 
0eddad1
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
from aiogram import Bot, Dispatcher
from aiogram.enums import ParseMode
from aiogram.client.default import DefaultBotProperties
from aiogram.types import Update
from aiogram.filters import Command
from aiogram.types import WebAppInfo, InlineKeyboardButton, InlineKeyboardMarkup, ReplyKeyboardMarkup, KeyboardButton
from aiohttp import web
from config import config
from database import connect_db, close_db
from handlers import user, admin

WEBHOOK_HOST = "https://ibrohm-textilestore.hf.space"
WEBHOOK_PATH = "/webhook"
WEBAPP_URL = "https://ibrohm-textile-shop.hf.space"

bot = Bot(
    token=config.BOT_TOKEN,
    default=DefaultBotProperties(parse_mode=ParseMode.HTML)
)

dp = Dispatcher()

dp.include_routers(user.router, admin.router)

async def on_startup():
    await connect_db()
    await bot.set_webhook(f"{WEBHOOK_HOST}{WEBHOOK_PATH}")

async def on_shutdown():
    await bot.delete_webhook()
    await close_db()

async def handle_update(request):
    try:
        data = await request.json()
        update = Update.model_validate(data)
        await dp.feed_update(bot, update)
        return web.Response(text="ok")
    except Exception as e:
        print(f"Update error: {e}")
        return web.Response(text="error")

async def health_check(request):
    return web.Response(text="ok")

if __name__ == "__main__":
    dp.startup.register(on_startup)
    dp.shutdown.register(on_shutdown)
    
    app = web.Application()
    
    app.router.add_post(WEBHOOK_PATH, handle_update)
    app.router.add_get("/", health_check)
    
    web.run_app(app, host="0.0.0.0", port=7860)