-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.py
138 lines (101 loc) · 4.18 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import os
import asyncio
import logging
from config import App
from views.middlewares.antispam import ThrottlingMiddleware
from views.middlewares.middleware import MessageMiddleware
from views import (
translate,
user,
words,
)
from aiohttp import web
from aiogram import Bot, Dispatcher, F, Router
from aiogram.fsm.storage.redis import RedisStorage
from aiogram.fsm.storage.memory import MemoryStorage
from aiogram.webhook.aiohttp_server import SimpleRequestHandler, setup_application
logger = logging.getLogger(__name__)
BOT_TOKEN = os.getenv('BOT_TOKEN')
WEBHOOK_HOST = os.getenv('WEBHOOK_HOST')
WEBHOOK_PATH = os.getenv('WEBHOOK_PATH')
WEBHOOK_URL = f"{WEBHOOK_HOST}{WEBHOOK_PATH}"
WEBAPP_HOST = os.getenv('WEBAPP_HOST')
WEBAPP_PORT = os.getenv('WEBAPP_PORT')
WEBAPP_PATH = os.getenv('WEBAPP_PATH')
async def on_startup(dispatcher: Dispatcher, bot: Bot):
await bot.delete_webhook(drop_pending_updates=True)
# await bot.set_webhook(WEBHOOK_URL, certificate=types.FSInputFile('/opt/homebrew/etc/ssl/certs/self-signed.pem'))
await bot.set_webhook(WEBHOOK_URL)
async def on_shutdown(dispatcher: Dispatcher, bot: Bot):
logging.warning('Shutting down..')
await bot.delete_webhook()
await dispatcher.storage.close()
await dispatcher.storage.wait_closed()
logging.warning('Bye!')
# def prod_main():
# storage = RedisStorage.from_url('redis://localhost:6379/0')
# bot_settings = {"parse_mode": "HTML"}
# bot = Bot(token=BOT_TOKEN, **bot_settings)
# dp = Dispatcher(storage=MemoryStorage())
# dp.message.filter(F.chat.type == "private")
# dp.startup.register(on_startup)
# dp.shutdown.register(on_shutdown)
# ignore_middleware_router = Router()
# ignore_middleware_router.include_router(character.router)
# app_router = Router()
# app_router.include_router(location.router)
# app_router.include_router(fight.router)
# app_router.include_router(inventory.router)
# app_router.include_router(shop.router)
# app_router.include_router(auction_house.router)
# app_router.include_router(npc.router)
# app_router.include_router(quests.router)
# app_router.include_router(mail.router)
# app_router.include_router(bank.router)
# app_router.include_router(items.router)
# app_router.include_router(fight_settings.router)
# app_router.include_router(character_details.router)
# app_router.include_router(arena.router)
# app_router.include_router(gathering.router)
# app_router.message.middleware(MessageMiddleware())
# app_router.callback_query.middleware(MessageMiddleware())
# dp.include_router(app_router)
# dp.include_router(ignore_middleware_router)
# dp.message.outer_middleware(ThrottlingMiddleware(storage))
# app = web.Application()
# SimpleRequestHandler(dispatcher=dp, bot=bot).register(app, path=WEBAPP_PATH)
# setup_application(app, dp, bot=bot)
# try:
# web.run_app(app, host=WEBAPP_HOST, port=WEBAPP_PORT)
# except Exception as ex:
# logger.error(f"[Exception] {ex}", exc_info=True)
# finally:
# app.shutdown()
async def local_main():
bot = Bot(token=BOT_TOKEN)
storage = RedisStorage.from_url(f"{App.config('REDIS_URI')}/0")
try:
dp = Dispatcher(storage=MemoryStorage())
# TODO: allow only private chats
# dp.message.filter(F.chat.type == "private")
app_router = Router()
app_router.include_router(user.router)
app_router.include_router(words.router)
app_router.include_router(translate.router)
app_router.message.middleware(MessageMiddleware())
app_router.callback_query.middleware(MessageMiddleware())
dp.include_router(app_router)
dp.message.outer_middleware(ThrottlingMiddleware(storage))
# await bot.delete_webhook(drop_pending_updates=True)
logger.info("Bot has started succesfully")
await dp.start_polling(bot)
except Exception as ex:
logger.error(f"Exception: {ex}", exc_info=True)
finally:
await bot.session.close()
if __name__ == '__main__':
asyncio.run(local_main())
# if ENV.startswith('dev'):
# prod_main()
# else:
# asyncio.run(local_main())