forked from Forden/aiogram-bot-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot.py
258 lines (219 loc) · 8.75 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
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
import asyncio
from typing import TYPE_CHECKING
import aiojobs
import orjson
import tenacity
from aiogram import Bot, Dispatcher
from aiogram.client.default import DefaultBotProperties
from aiogram.client.telegram import TelegramAPIServer
from aiogram.fsm.storage.redis import DefaultKeyBuilder, RedisStorage
from aiohttp import web
from redis.asyncio import Redis
from aiogram_bot_template import handlers, utils, web_handlers
from aiogram_bot_template.data import config
from aiogram_bot_template.middlewares import StructLoggingMiddleware
if TYPE_CHECKING:
import asyncpg as asyncpg
import redis
import structlog
from aiogram.client.session.aiohttp import AiohttpSession
async def create_db_connections(dp: Dispatcher) -> None:
logger: structlog.typing.FilteringBoundLogger = dp["business_logger"]
logger.debug("Connecting to PostgreSQL", db="main")
try:
db_pool = await utils.connect_to_services.wait_postgres(
logger=dp["db_logger"],
host=config.PG_HOST,
port=config.PG_PORT,
user=config.PG_USER,
password=config.PG_PASSWORD,
database=config.PG_DATABASE,
)
except tenacity.RetryError:
logger.exception("Failed to connect to PostgreSQL", db="main")
exit(1)
else:
logger.debug("Succesfully connected to PostgreSQL", db="main")
dp["db_pool"] = db_pool
if config.USE_CACHE:
logger.debug("Connecting to Redis")
try:
redis_pool = await utils.connect_to_services.wait_redis_pool(
logger=dp["cache_logger"],
host=config.CACHE_HOST,
password=config.CACHE_PASSWORD,
port=config.CACHE_PORT,
database=0,
)
except tenacity.RetryError:
logger.exception("Failed to connect to Redis")
exit(1)
else:
logger.debug("Succesfully connected to Redis")
dp["cache_pool"] = redis_pool
dp["temp_bot_cloud_session"] = utils.smart_session.SmartAiogramAiohttpSession(
json_loads=orjson.loads,
logger=dp["aiogram_session_logger"],
)
if config.USE_CUSTOM_API_SERVER:
dp["temp_bot_local_session"] = utils.smart_session.SmartAiogramAiohttpSession(
api=TelegramAPIServer(
base=config.CUSTOM_API_SERVER_BASE,
file=config.CUSTOM_API_SERVER_FILE,
is_local=config.CUSTOM_API_SERVER_IS_LOCAL,
),
json_loads=orjson.loads,
logger=dp["aiogram_session_logger"],
)
async def close_db_connections(dp: Dispatcher) -> None:
if "temp_bot_cloud_session" in dp.workflow_data:
temp_bot_cloud_session: AiohttpSession = dp["temp_bot_cloud_session"]
await temp_bot_cloud_session.close()
if "temp_bot_local_session" in dp.workflow_data:
temp_bot_local_session: AiohttpSession = dp["temp_bot_local_session"]
await temp_bot_local_session.close()
if "db_pool" in dp.workflow_data:
db_pool: asyncpg.Pool = dp["db_pool"]
await db_pool.close()
if "cache_pool" in dp.workflow_data:
cache_pool: redis.asyncio.Redis = dp["cache_pool"] # type: ignore[type-arg]
await cache_pool.close()
def setup_handlers(dp: Dispatcher) -> None:
dp.include_router(handlers.user.prepare_router())
def setup_middlewares(dp: Dispatcher) -> None:
dp.update.outer_middleware(StructLoggingMiddleware(logger=dp["aiogram_logger"]))
def setup_logging(dp: Dispatcher) -> None:
dp["aiogram_logger"] = utils.logging.setup_logger().bind(type="aiogram")
dp["db_logger"] = utils.logging.setup_logger().bind(type="db")
dp["cache_logger"] = utils.logging.setup_logger().bind(type="cache")
dp["business_logger"] = utils.logging.setup_logger().bind(type="business")
async def setup_aiogram(dp: Dispatcher) -> None:
setup_logging(dp)
logger = dp["aiogram_logger"]
logger.debug("Configuring aiogram")
await create_db_connections(dp)
setup_handlers(dp)
setup_middlewares(dp)
logger.info("Configured aiogram")
async def aiohttp_on_startup(app: web.Application) -> None:
dp: Dispatcher = app["dp"]
workflow_data = {"app": app, "dispatcher": dp}
if "bot" in app:
workflow_data["bot"] = app["bot"]
await dp.emit_startup(**workflow_data)
async def aiohttp_on_shutdown(app: web.Application) -> None:
dp: Dispatcher = app["dp"]
for i in [app, *app._subapps]: # noqa: SLF001 # dirty
if "scheduler" in i:
scheduler: aiojobs.Scheduler = i["scheduler"]
scheduler._closed = True # noqa: SLF001
while scheduler.pending_count != 0:
dp["aiogram_logger"].info(
f"Waiting for {scheduler.pending_count} tasks to complete",
)
await asyncio.sleep(1)
workflow_data = {"app": app, "dispatcher": dp}
if "bot" in app:
workflow_data["bot"] = app["bot"]
await dp.emit_shutdown(**workflow_data)
async def aiogram_on_startup_webhook(dispatcher: Dispatcher, bot: Bot) -> None:
await setup_aiogram(dispatcher)
webhook_logger = dispatcher["aiogram_logger"].bind(
webhook_url=config.MAIN_WEBHOOK_ADDRESS,
)
webhook_logger.debug("Configuring webhook")
await bot.set_webhook(
url=config.MAIN_WEBHOOK_ADDRESS.format(
token=config.BOT_TOKEN,
bot_id=config.BOT_TOKEN.split(":")[0],
),
allowed_updates=dispatcher.resolve_used_update_types(),
secret_token=config.MAIN_WEBHOOK_SECRET_TOKEN,
)
webhook_logger.info("Configured webhook")
async def aiogram_on_shutdown_webhook(dispatcher: Dispatcher, bot: Bot) -> None:
dispatcher["aiogram_logger"].debug("Stopping webhook")
await close_db_connections(dispatcher)
await bot.session.close()
await dispatcher.storage.close()
dispatcher["aiogram_logger"].info("Stopped webhook")
async def aiogram_on_startup_polling(dispatcher: Dispatcher, bot: Bot) -> None:
if config.DROP_PREVIOUS_UPDATES:
await bot.delete_webhook(drop_pending_updates=True)
await setup_aiogram(dispatcher)
dispatcher["aiogram_logger"].info("Started polling")
async def aiogram_on_shutdown_polling(dispatcher: Dispatcher, bot: Bot) -> None:
dispatcher["aiogram_logger"].debug("Stopping polling")
await close_db_connections(dispatcher)
await bot.session.close()
await dispatcher.storage.close()
dispatcher["aiogram_logger"].info("Stopped polling")
async def setup_aiohttp_app( # noqa: RUF029
bot: Bot,
dp: Dispatcher,
) -> web.Application:
scheduler = aiojobs.Scheduler()
app = web.Application()
subapps: list[tuple[str, web.Application]] = [
("/tg/webhooks/", web_handlers.tg_updates_app),
]
for prefix, subapp in subapps:
subapp["bot"] = bot
subapp["dp"] = dp
subapp["scheduler"] = scheduler
app.add_subapp(prefix, subapp)
app["bot"] = bot
app["dp"] = dp
app["scheduler"] = scheduler
app.on_startup.append(aiohttp_on_startup)
app.on_shutdown.append(aiohttp_on_shutdown)
return app
def main() -> None:
aiogram_session_logger = utils.logging.setup_logger().bind(type="aiogram_session")
if config.USE_CUSTOM_API_SERVER:
session = utils.smart_session.SmartAiogramAiohttpSession(
api=TelegramAPIServer(
base=config.CUSTOM_API_SERVER_BASE,
file=config.CUSTOM_API_SERVER_FILE,
is_local=config.CUSTOM_API_SERVER_IS_LOCAL,
),
json_loads=orjson.loads,
logger=aiogram_session_logger,
)
else:
session = utils.smart_session.SmartAiogramAiohttpSession(
json_loads=orjson.loads,
logger=aiogram_session_logger,
)
bot = Bot(
config.BOT_TOKEN,
session=session,
default=DefaultBotProperties(parse_mode="HTML"),
)
dp = Dispatcher(
storage=RedisStorage(
redis=Redis(
host=config.FSM_HOST,
password=config.FSM_PASSWORD,
port=config.FSM_PORT,
db=0,
),
key_builder=DefaultKeyBuilder(with_bot_id=True),
),
)
dp["aiogram_session_logger"] = aiogram_session_logger
if config.USE_WEBHOOK:
dp.startup.register(aiogram_on_startup_webhook)
dp.shutdown.register(aiogram_on_shutdown_webhook)
web.run_app(
asyncio.run(setup_aiohttp_app(bot, dp)),
handle_signals=True,
host=config.MAIN_WEBHOOK_LISTENING_HOST,
port=config.MAIN_WEBHOOK_LISTENING_PORT,
)
else:
dp.startup.register(aiogram_on_startup_polling)
dp.shutdown.register(aiogram_on_shutdown_polling)
asyncio.run(dp.start_polling(bot))
if __name__ == "__main__":
main()