-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
210 lines (166 loc) · 5.3 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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
import asyncio
import logging
from datetime import datetime
import aiohttp_jinja2
import jinja2
from aiogram import Bot, Dispatcher, Router
from aiogram.client.session.aiohttp import AiohttpSession
from aiogram.enums import ParseMode
from aiogram.exceptions import TelegramUnauthorizedError
from aiogram.filters.state import State, StatesGroup
from aiogram.fsm.storage.memory import MemoryStorage
from aiogram.utils.i18n import I18n
from aiogram.utils.i18n.middleware import ConstI18nMiddleware
from aiogram.webhook.aiohttp_server import (
SimpleRequestHandler,
TokenBasedRequestHandler,
setup_application,
)
from aiohttp import web
from core.config import config
from core.filters import IsChatIsPrivate
from core.handlers import cheques, start
from core.utils.logger import init_logger
logger = logging.getLogger(__name__)
BASE_URL = config.BOT_URL
MAIN_BOT_TOKEN = config.BOT_TOKEN
WEB_SERVER_HOST = config.HOST
WEB_SERVER_PORT = config.PORT
MAIN_BOT_PATH = config.MAIN_BOT_PATH
OTHER_BOTS_PATH = config.OTHER_BOTS_PATH
OTHER_BOTS_URL = f"{BASE_URL}{OTHER_BOTS_PATH}"
MAIN_BOT_URL = f"{BASE_URL}{MAIN_BOT_PATH}"
class FSMFillFrom(StatesGroup):
get_bot_token = State()
def setup_routes(application):
from web.routes import setup_routes as setup_forum_routes
setup_forum_routes(application)
def setup_external_libraries(application: web.Application) -> None:
aiohttp_jinja2.setup(
application,
loader=jinja2.FileSystemLoader("./templates"),
)
def setup_app(application):
setup_external_libraries(application)
setup_routes(application)
async def periodic_scheduler(period, fu, *args, **kw):
while True:
await asyncio.sleep(period)
await fu(*args, **kw)
async def on_time_scheduler(start_time: datetime, fu, *args, **kw):
start_time = start_time.time()
while True:
current_time = datetime.now().time()
if (
current_time.hour * 60 * 60
+ current_time.minute * 60
+ current_time.second
) - (
start_time.hour * 60 * 60
+ start_time.minute * 60
+ start_time.second
) < 60:
await fu(*args, **kw)
await asyncio.sleep(60)
async def on_startup(dispatcher: Dispatcher, bot: Bot):
from core.service_provider.manager import provider_manager
try:
await bot.set_webhook(f"{BASE_URL}{MAIN_BOT_PATH}")
except TelegramUnauthorizedError as error:
logger.critical("Main bot token revoked! Can't set webhook!")
logger.exception(error)
await bot.session.close()
loop = asyncio.get_event_loop()
loop.create_task(
periodic_scheduler(
config.CHECK_ORDER_STATUS_INTERVAL,
provider_manager.check_orders,
)
)
loop.create_task(
periodic_scheduler(
config.AUTO_STARTING_ORDERS_INTERVAL,
provider_manager.activate_orders,
)
)
loop.create_task(
on_time_scheduler(
# config.UPDATE_SERVICES_INTERVAL,
datetime(year=2023, month=1, day=1, hour=0, minute=0, second=0),
provider_manager.update_services,
)
)
def init_routers(main_dispatcher: Dispatcher):
from core.handlers import (
admin,
common,
my_bots,
my_orders,
new_order,
order_info,
referrals,
wallet,
)
private_router = Router(name="private_chat_router")
private_router.message.filter(IsChatIsPrivate())
private_router.callback_query.filter(IsChatIsPrivate())
private_router.include_routers(
start.start_router,
admin.admin_router,
new_order.order_router,
referrals.referral_router,
my_bots.my_bots_router,
cheques.check_router,
wallet.balance_router,
my_orders.my_orders_router,
order_info.order_info_router,
)
main_dispatcher.include_routers(
private_router,
common.common_router,
)
def main():
init_logger()
i18n = I18n(
path="locales",
default_locale="ru_RU",
domain="messages",
)
I18n.set_current(i18n)
session = AiohttpSession()
bot_settings = {
"session": session,
"parse_mode": ParseMode.HTML,
}
bot = Bot(token=MAIN_BOT_TOKEN, **bot_settings)
storage = MemoryStorage()
i18n_middleware = ConstI18nMiddleware(
i18n=i18n,
locale="ru_RU",
)
main_dispatcher = Dispatcher(storage=storage)
main_dispatcher.update.middleware(i18n_middleware)
multibot_dispatcher = Dispatcher(storage=storage)
multibot_dispatcher.update.middleware(i18n_middleware)
init_routers(main_dispatcher)
main_dispatcher.startup.register(on_startup)
app = web.Application()
setup_app(app)
SimpleRequestHandler(
dispatcher=main_dispatcher,
bot=bot,
).register(app, path=MAIN_BOT_PATH)
TokenBasedRequestHandler(
dispatcher=main_dispatcher,
bot_settings=bot_settings,
).register(app, path=OTHER_BOTS_PATH)
setup_application(app, main_dispatcher, bot=bot)
setup_application(app, multibot_dispatcher)
app.router.add_static(
"/static/",
path="./static",
name="static",
)
web.run_app(app, host=WEB_SERVER_HOST, port=WEB_SERVER_PORT)
if __name__ == "__main__":
main()