W1xced commited on
Commit
7441e7e
·
verified ·
1 Parent(s): 67bedca

Upload main.py

Browse files
Files changed (1) hide show
  1. main.py +12 -8
main.py CHANGED
@@ -37,12 +37,8 @@ def load_snippets():
37
 
38
  snippets = load_snippets()
39
 
40
- # Создаем сессию с принудительным IPv4
41
- session = AiohttpSession(
42
- connector=TCPConnector(family=socket.AF_INET, use_dns_cache=False)
43
- )
44
- bot = Bot(token=BOT_TOKEN, session=session)
45
  dp = Dispatcher()
 
46
 
47
  # ... (остальной код safe_format и обработчиков остается прежним) ...
48
 
@@ -80,6 +76,14 @@ async def inline_query_handler(query: types.InlineQuery):
80
  await query.answer(results[:50], cache_time=1)
81
 
82
  async def main():
 
 
 
 
 
 
 
 
83
  app = web.Application()
84
  app.router.add_get("/", health_check)
85
  runner = web.AppRunner(app)
@@ -87,17 +91,17 @@ async def main():
87
  site = web.TCPSite(runner, "0.0.0.0", 7860)
88
  await site.start()
89
 
90
- max_retries = 20 # Увеличим количество попыток
91
  for attempt in range(1, max_retries + 1):
92
  try:
93
  bot_info = await bot.get_me()
94
- logger.info(f"Connected to Telegram! Bot: @{bot_info.username}")
95
  break
96
  except Exception as e:
97
  if attempt == max_retries:
98
  logger.error("Failed to connect. Exiting.")
99
  return
100
- logger.warning(f"Attempt {attempt} failed, retrying... ({e})")
101
  await asyncio.sleep(5)
102
 
103
  await dp.start_polling(bot)
 
37
 
38
  snippets = load_snippets()
39
 
 
 
 
 
 
40
  dp = Dispatcher()
41
+ bot = None # Будет инициализирован в main()
42
 
43
  # ... (остальной код safe_format и обработчиков остается прежним) ...
44
 
 
76
  await query.answer(results[:50], cache_time=1)
77
 
78
  async def main():
79
+ global bot
80
+
81
+ # Создаем сессию здесь, когда цикл событий уже запущен
82
+ session = AiohttpSession(
83
+ connector=TCPConnector(family=socket.AF_INET, use_dns_cache=False)
84
+ )
85
+ bot = Bot(token=BOT_TOKEN, session=session)
86
+
87
  app = web.Application()
88
  app.router.add_get("/", health_check)
89
  runner = web.AppRunner(app)
 
91
  site = web.TCPSite(runner, "0.0.0.0", 7860)
92
  await site.start()
93
 
94
+ max_retries = 20
95
  for attempt in range(1, max_retries + 1):
96
  try:
97
  bot_info = await bot.get_me()
98
+ logger.info(f"Connected! Bot: @{bot_info.username}")
99
  break
100
  except Exception as e:
101
  if attempt == max_retries:
102
  logger.error("Failed to connect. Exiting.")
103
  return
104
+ logger.warning(f"Attempt {attempt} failed... ({e})")
105
  await asyncio.sleep(5)
106
 
107
  await dp.start_polling(bot)