Cuong2004 commited on
Commit
42fafc3
·
1 Parent(s): 7b8993a

Fix: Graceful Telegram bot startup - handle network errors

Browse files
Files changed (1) hide show
  1. app/services/telegram_bot.py +34 -23
app/services/telegram_bot.py CHANGED
@@ -11,41 +11,52 @@ class TelegramBot:
11
  _instance = None
12
  _app: Application = None
13
  _bot: Bot = None
 
14
 
15
  @classmethod
16
  async def start(cls):
17
- """Start the Telegram bot."""
18
  settings = get_settings()
19
 
20
  if not settings.telegram_bot_token:
21
- print("Telegram bot token not configured")
22
  return
23
 
24
- cls._bot = Bot(token=settings.telegram_bot_token)
25
- cls._app = Application.builder().token(settings.telegram_bot_token).build()
26
-
27
- # Add handlers
28
- cls._app.add_handler(CommandHandler("start", cls._handle_start))
29
- cls._app.add_handler(CommandHandler("help", cls._handle_help))
30
- cls._app.add_handler(CommandHandler("chatid", cls._handle_chatid))
31
- cls._app.add_handler(CommandHandler("top", cls._handle_top))
32
- cls._app.add_handler(CommandHandler("watchlist", cls._handle_watchlist))
33
-
34
- # Start polling in background
35
- await cls._app.initialize()
36
- await cls._app.start()
37
- await cls._app.updater.start_polling(drop_pending_updates=True)
38
-
39
- print("Telegram bot started with polling")
 
 
 
 
 
 
 
40
 
41
  @classmethod
42
  async def stop(cls):
43
  """Stop the Telegram bot."""
44
- if cls._app:
45
- await cls._app.updater.stop()
46
- await cls._app.stop()
47
- await cls._app.shutdown()
48
- print("Telegram bot stopped")
 
 
 
49
 
50
  @classmethod
51
  async def _handle_start(cls, update: Update, context: ContextTypes.DEFAULT_TYPE):
 
11
  _instance = None
12
  _app: Application = None
13
  _bot: Bot = None
14
+ _started = False
15
 
16
  @classmethod
17
  async def start(cls):
18
+ """Start the Telegram bot (graceful - won't crash on failure)."""
19
  settings = get_settings()
20
 
21
  if not settings.telegram_bot_token:
22
+ print("Telegram bot: Not configured (no token)")
23
  return
24
 
25
+ try:
26
+ cls._bot = Bot(token=settings.telegram_bot_token)
27
+ cls._app = Application.builder().token(settings.telegram_bot_token).build()
28
+
29
+ # Add handlers
30
+ cls._app.add_handler(CommandHandler("start", cls._handle_start))
31
+ cls._app.add_handler(CommandHandler("help", cls._handle_help))
32
+ cls._app.add_handler(CommandHandler("chatid", cls._handle_chatid))
33
+ cls._app.add_handler(CommandHandler("top", cls._handle_top))
34
+ cls._app.add_handler(CommandHandler("watchlist", cls._handle_watchlist))
35
+
36
+ # Start polling in background
37
+ await cls._app.initialize()
38
+ await cls._app.start()
39
+ await cls._app.updater.start_polling(drop_pending_updates=True)
40
+
41
+ cls._started = True
42
+ print("Telegram bot: Started with polling")
43
+ except Exception as e:
44
+ print(f"Telegram bot: Failed to start ({e.__class__.__name__})")
45
+ cls._app = None
46
+ cls._bot = None
47
+ cls._started = False
48
 
49
  @classmethod
50
  async def stop(cls):
51
  """Stop the Telegram bot."""
52
+ if cls._app and cls._started:
53
+ try:
54
+ await cls._app.updater.stop()
55
+ await cls._app.stop()
56
+ await cls._app.shutdown()
57
+ print("Telegram bot: Stopped")
58
+ except Exception as e:
59
+ print(f"Telegram bot: Error stopping ({e})")
60
 
61
  @classmethod
62
  async def _handle_start(cls, update: Update, context: ContextTypes.DEFAULT_TYPE):