W1xced commited on
Commit
0475441
·
verified ·
1 Parent(s): c9b0131

Upload main.py

Browse files
Files changed (1) hide show
  1. main.py +17 -19
main.py CHANGED
@@ -4,6 +4,7 @@ import yaml
4
  import os
5
  import re
6
  import html
 
7
  from aiogram import Bot, Dispatcher, types, F
8
  from aiogram.types import InlineQueryResultArticle, InputTextMessageContent
9
  from aiogram.filters import CommandStart
@@ -38,11 +39,8 @@ dp = Dispatcher()
38
 
39
  @dp.message(CommandStart())
40
  async def cmd_start(message: types.Message):
41
- try:
42
- bot_info = await bot.get_me()
43
- await message.answer(f"Hello! I am an inline bot. Type @{bot_info.username} in any chat to see my snippets.")
44
- except:
45
- await message.answer("Hello! I am an inline bot. Type my username in any chat to see my snippets.")
46
 
47
  @dp.inline_query()
48
  async def inline_query_handler(query: types.InlineQuery):
@@ -70,19 +68,19 @@ async def main():
70
  await runner.setup()
71
  await web.TCPSite(runner, "0.0.0.0", 7860).start()
72
 
73
- logger.info("Starting bot connection loop...")
74
- for attempt in range(1, 21):
75
- try:
76
- bot_info = await bot.get_me()
77
- logger.info(f"Connected! Starting bot @{bot_info.username}")
78
- await dp.start_polling(bot)
79
- break
80
- except Exception as e:
81
- logger.warning(f"Attempt {attempt}/20 failed: {e}")
82
- await asyncio.sleep(5)
83
 
84
  if __name__ == "__main__":
85
- try:
86
- asyncio.run(main())
87
- except (KeyboardInterrupt, SystemExit):
88
- logger.info("Bot stopped!")
 
 
 
 
 
 
 
 
4
  import os
5
  import re
6
  import html
7
+ import subprocess
8
  from aiogram import Bot, Dispatcher, types, F
9
  from aiogram.types import InlineQueryResultArticle, InputTextMessageContent
10
  from aiogram.filters import CommandStart
 
39
 
40
  @dp.message(CommandStart())
41
  async def cmd_start(message: types.Message):
42
+ bot_info = await bot.get_me()
43
+ await message.answer(f"Hello! I am an inline bot. Type @{bot_info.username} in any chat to see my snippets.")
 
 
 
44
 
45
  @dp.inline_query()
46
  async def inline_query_handler(query: types.InlineQuery):
 
68
  await runner.setup()
69
  await web.TCPSite(runner, "0.0.0.0", 7860).start()
70
 
71
+ bot_info = await bot.get_me()
72
+ logger.info(f"Bot started: @{bot_info.username}")
73
+ await dp.start_polling(bot)
 
 
 
 
 
 
 
74
 
75
  if __name__ == "__main__":
76
+ import sys
77
+ if "--worker" in sys.argv or os.name != 'nt' or os.environ.get("DOCKER_ENV"):
78
+ try:
79
+ asyncio.run(main())
80
+ except (KeyboardInterrupt, SystemExit):
81
+ logger.info("Bot stopped!")
82
+ else:
83
+ try:
84
+ subprocess.Popen([sys.executable, "manager.py"], creationflags=subprocess.CREATE_NEW_CONSOLE)
85
+ except:
86
+ asyncio.run(main())