From f27df7b1b83281fda58990df8a38020fd2d3482c Mon Sep 17 00:00:00 2001 From: EugeneBee Date: Mon, 26 May 2025 10:59:37 +0300 Subject: [PATCH] Bot start/stop --- src/main.py | 45 +++++++++++++++++++++++++++++---------------- 1 file changed, 29 insertions(+), 16 deletions(-) diff --git a/src/main.py b/src/main.py index 1913b19..fba6f09 100644 --- a/src/main.py +++ b/src/main.py @@ -27,7 +27,7 @@ import strings import options -async def set_commands(): +async def setCommands(): commands = [BotCommand(command='start', description='Старт'), BotCommand(command='help', description='Инструкция'), BotCommand(command='info', description='Информация о стратегиях'), @@ -52,8 +52,8 @@ bot = Bot( default=DefaultBotProperties(parse_mode=ParseMode.HTML), ) -strategy_router = Router() -stop_router = Router() +strategyRouter = Router() +stopRouter = Router() @dp.message(Command("start")) @@ -87,13 +87,13 @@ async def commandInfo(message: Message) -> None: await message.answer(msgText) -@strategy_router.message(Command("strategy"), F.chat.id.in_(whitelist.chatIDs)) +@strategyRouter.message(Command("strategy"), F.chat.id.in_(whitelist.chatIDs)) async def commandStrategy(message: Message, state: FSMContext): await message.answer(strings.strategyCommand + '\n' + strings.askPair) await state.set_state(startForm.pair) -@strategy_router.message(F.text, startForm.pair) -async def capture_start_pair(message: Message, state: FSMContext): +@strategyRouter.message(F.text, startForm.pair) +async def captureStartPair(message: Message, state: FSMContext): await state.update_data(pair=message.text) data = await state.get_data() @@ -110,8 +110,8 @@ async def capture_start_pair(message: Message, state: FSMContext): else: await state.set_state(startForm.params) -@strategy_router.message(F.text, startForm.params) -async def capture_params(message: Message, state: FSMContext): +@strategyRouter.message(F.text, startForm.params) +async def captureParams(message: Message, state: FSMContext): await state.update_data(params=message.text) data = await state.get_data() @@ -161,13 +161,13 @@ async def capture_params(message: Message, state: FSMContext): await state.clear() -@stop_router.message(Command("stop"), F.chat.id.in_(whitelist.chatIDs)) +@stopRouter.message(Command("stop"), F.chat.id.in_(whitelist.chatIDs)) async def commandStop(message: Message, state: FSMContext): await message.answer(strings.stopCommand + '\n' + strings.askPair) await state.set_state(stopForm.pair) -@stop_router.message(F.text, stopForm.pair) -async def capture_stop_pair(message: Message, state: FSMContext): +@stopRouter.message(F.text, stopForm.pair) +async def captureStopPair(message: Message, state: FSMContext): await state.update_data(pair=message.text) data = await state.get_data() @@ -181,13 +181,26 @@ async def capture_stop_pair(message: Message, state: FSMContext): await state.clear() -async def start_bot(): - await set_commands() +async def startBot(): + await setCommands() + try: + for i in whitelist.chatIDs: + await bot.send_message(chat_id=i, text=strings.startBot) + except Exception as e: + generalLogger.error(e) + +async def stopBot(): + try: + for i in whitelist.chatIDs: + await bot.send_message(chat_id=i, text=strings.stopBot) + except Exception as e: + generalLogger.error(e) async def main() -> None: - dp.include_router(strategy_router) - dp.include_router(stop_router) - dp.startup.register(start_bot) + dp.include_router(strategyRouter) + dp.include_router(stopRouter) + dp.startup.register(startBot) + dp.shutdown.register(stopBot) await dp.start_polling(bot)