ibrohm commited on
Commit
0eddad1
·
verified ·
1 Parent(s): 5a494ed

Upload main.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. main.py +31 -19
main.py CHANGED
@@ -1,25 +1,18 @@
1
- from aiogram import Bot, Dispatcher, types
2
  from aiogram.enums import ParseMode
3
  from aiogram.client.default import DefaultBotProperties
4
- from aiogram.webhook.aiohttp import AiohttpRequestHandler
5
- from aiogram.webhook.executor import WebhookExecutor
 
6
  from aiohttp import web
7
  from config import config
8
  from database import connect_db, close_db
9
  from handlers import user, admin
10
- import asyncio
11
 
12
  WEBHOOK_HOST = "https://ibrohm-textilestore.hf.space"
13
  WEBHOOK_PATH = "/webhook"
14
  WEBAPP_URL = "https://ibrohm-textile-shop.hf.space"
15
 
16
- async def on_startup():
17
- await connect_db()
18
- await bot.set_webhook(f"{WEBHOOK_HOST}{WEBHOOK_PATH}")
19
-
20
- async def on_shutdown():
21
- await bot.delete_webhook()
22
-
23
  bot = Bot(
24
  token=config.BOT_TOKEN,
25
  default=DefaultBotProperties(parse_mode=ParseMode.HTML)
@@ -27,17 +20,36 @@ bot = Bot(
27
 
28
  dp = Dispatcher()
29
 
30
- dp.startup.register(on_startup)
31
- dp.shutdown.register(on_shutdown)
32
-
33
  dp.include_routers(user.router, admin.router)
34
 
35
- async def main(request):
36
- return await AiohttpRequestHandler(dispatcher=dp, bot=bot)(request)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
37
 
38
  if __name__ == "__main__":
 
 
 
39
  app = web.Application()
40
- AiohttpRequestHandler.register(app, path=WEBHOOK_PATH, dispatcher=dp, bot=bot)
41
 
42
- import asyncio
43
- asyncio.run(app.run_app(host="0.0.0.0", port=7860))
 
 
 
1
+ from aiogram import Bot, Dispatcher
2
  from aiogram.enums import ParseMode
3
  from aiogram.client.default import DefaultBotProperties
4
+ from aiogram.types import Update
5
+ from aiogram.filters import Command
6
+ from aiogram.types import WebAppInfo, InlineKeyboardButton, InlineKeyboardMarkup, ReplyKeyboardMarkup, KeyboardButton
7
  from aiohttp import web
8
  from config import config
9
  from database import connect_db, close_db
10
  from handlers import user, admin
 
11
 
12
  WEBHOOK_HOST = "https://ibrohm-textilestore.hf.space"
13
  WEBHOOK_PATH = "/webhook"
14
  WEBAPP_URL = "https://ibrohm-textile-shop.hf.space"
15
 
 
 
 
 
 
 
 
16
  bot = Bot(
17
  token=config.BOT_TOKEN,
18
  default=DefaultBotProperties(parse_mode=ParseMode.HTML)
 
20
 
21
  dp = Dispatcher()
22
 
 
 
 
23
  dp.include_routers(user.router, admin.router)
24
 
25
+ async def on_startup():
26
+ await connect_db()
27
+ await bot.set_webhook(f"{WEBHOOK_HOST}{WEBHOOK_PATH}")
28
+
29
+ async def on_shutdown():
30
+ await bot.delete_webhook()
31
+ await close_db()
32
+
33
+ async def handle_update(request):
34
+ try:
35
+ data = await request.json()
36
+ update = Update.model_validate(data)
37
+ await dp.feed_update(bot, update)
38
+ return web.Response(text="ok")
39
+ except Exception as e:
40
+ print(f"Update error: {e}")
41
+ return web.Response(text="error")
42
+
43
+ async def health_check(request):
44
+ return web.Response(text="ok")
45
 
46
  if __name__ == "__main__":
47
+ dp.startup.register(on_startup)
48
+ dp.shutdown.register(on_shutdown)
49
+
50
  app = web.Application()
 
51
 
52
+ app.router.add_post(WEBHOOK_PATH, handle_update)
53
+ app.router.add_get("/", health_check)
54
+
55
+ web.run_app(app, host="0.0.0.0", port=7860)