-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
68 lines (52 loc) · 1.8 KB
/
main.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# Imports
from aiogram.contrib.fsm_storage.memory import MemoryStorage
from imports import *
# Main function
def main() -> None:
"""
Bots "Heart"
Used to setup core-functionality
"""
logging.basicConfig(level=logging.INFO) # Logging
bot = Bot(token=env.API_TOKEN)
dp = Dispatcher(bot, storage=MemoryStorage())
dp.middleware.setup(LoggingMiddleware()) # Middleware setup
async def on_startup(self):
"""
Actions to perform on WebHook Startup:
"""
logging.warning("Starting webhook..")
await bot.delete_webhook()
await bot.set_my_commands(commands=set_cmd())
# Getting webhook info
logging.info("Preparing DB...")
await proceed_schemas(async_engine, BaseModel.metadata, User.__tablename__)
webhook = await bot.get_webhook_info()
# Resolve webhook if BadUrl
if webhook.url != env.WEBHOOK_URL:
logging.warning(f"Bad webhook url: {webhook.url}, resolving...")
if not webhook.url:
await bot.delete_webhook()
logging.warning("Webhook url deleted")
await bot.set_webhook(env.WEBHOOK_URL)
commands.setup(dp) # Setup CMDs
callback.setup(dp) # Setup callback
async def on_shutdown(self):
"""
Actions to perform on WebHook Shutdown
Note: self.delete_webhook() is not used due to startrup issues
When running on hosting
"""
logging.warning("Shutting down..")
logging.warning("Bye!")
start_webhook(
dispatcher=dp,
webhook_path=env.WEBHOOK_URL_PATH,
on_startup=on_startup,
on_shutdown=on_shutdown,
skip_updates=True,
host=env.WEBAPP_HOST,
port=env.WEBAPP_PORT,
)
if __name__ == "__main__":
main()