-
Notifications
You must be signed in to change notification settings - Fork 21
/
bot.py
46 lines (35 loc) · 1.24 KB
/
bot.py
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
import logging
import config
from aiogram import Bot, Dispatcher
from aiogram.enums import ParseMode
from aiogram.fsm.storage.memory import MemoryStorage
from aiogram.webhook.aiohttp_server import SimpleRequestHandler, setup_application
from aiohttp import web
from config import TOKEN, WEBHOOK_URL, ADMIN_ID_LIST
from db import create_db_and_tables
bot = Bot(TOKEN, parse_mode=ParseMode.HTML)
dp = Dispatcher(storage=MemoryStorage())
async def on_startup(bot: Bot):
await create_db_and_tables()
await bot.set_webhook(WEBHOOK_URL)
for admin in ADMIN_ID_LIST:
try:
await bot.send_message(admin, 'Bot is working')
except Exception as e:
logging.warning(e)
async def on_shutdown(dp):
logging.warning('Shutting down..')
await bot.delete_webhook()
await dp.storage.close()
await dp.storage.wait_closed()
logging.warning('Bye!')
def main() -> None:
dp.startup.register(on_startup)
app = web.Application()
webhook_requests_handler = SimpleRequestHandler(
dispatcher=dp,
bot=bot
)
webhook_requests_handler.register(app, path=config.WEBHOOK_PATH)
setup_application(app, dp, bot=bot)
web.run_app(app, host=config.WEBAPP_HOST, port=config.WEBAPP_PORT)