Spaces:
Sleeping
Sleeping
Fix: Graceful Telegram bot startup - handle network errors
Browse files- 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
|
| 22 |
return
|
| 23 |
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
|
| 41 |
@classmethod
|
| 42 |
async def stop(cls):
|
| 43 |
"""Stop the Telegram bot."""
|
| 44 |
-
if cls._app:
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
|
|
|
|
|
|
|
|
|
| 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):
|